Search code examples
arduinoledjoystickarduino-c++

Understanding the logic behind the code for controlling an 8x8 dot matrix with a joystick


I found this code online for controlling a max7219 8x8 matrix with an Arduino, and I am struggling to understand a few things about the code. Here is the whole code:

#include <LedControl.h>

int UD = 0;
int LR = 0; //Setting up controller//
LedControl lc=LedControl(8,10,9,1); //10 is to CLOCK, 9 = CS, 8=DIN//

void setup() {
 Serial.begin(9600);
 lc.shutdown(0,false); // turn off power saving, enables display
 lc.setIntensity(0,1); // sets brightness (0~15 possible values)
 lc.clearDisplay(0); // clear screen
}

void loop() {
 UD = analogRead(A0);
 LR = analogRead(A1);
 char x_translate = map(LR, 1021, 0, 7, 0); //map(value, fromLow, fromHigh, toLow, toHigh)
 char y_translate = map(UD, 1021, 0, 0, 7);

 Serial.print("UD = ");
 Serial.print(UD, DEC);
 Serial.print(", LR = ");
 Serial.print(LR, DEC);
 Serial.print(", x = ");
 Serial.print(x_translate, DEC);
 Serial.print(", y = ");
 Serial.println(y_translate, DEC);
 // not in shutdown mode
 lc.clearDisplay(0);
 lc.setLed(0,x_translate,y_translate,true);
 delay(150); //adjust delay to get your joystick correct//
}

My first question has to do with what Serial.begin(9600) means.

My biggest question is about translating the analog input of the joystick into lighting up the LED's on the matrix:

char x_translate = map(LR, 1021, 0, 7, 0); //map(value, fromLow, fromHigh, toLow, toHigh)
char y_translate = map(UD, 1021, 0, 0, 7);

Where does 1021 come from, and why is it the fromLow value, when it seems to be the maximum of whatever range it scales? And where do the 7's come from, and why are they in the positions they're in?

Lastly, what the hell is happening with all the Serial.print() statements? What is Serial, and what is DEC?

As you can tell I'm pretty new to C++ so any help here would be appreciated, thanks.


Solution

  • From the Arduino manual:

    Serial.begin(baud)

    Sets the data rate in bits per second (baud) for serial data transmission.

    So in your case its 9600 baud.

    Serial.print()

    Prints data to the serial port as human-readable ASCII text. An optional second parameter specifies the base (format) to use; permitted values are BIN(binary, or base 2), OCT(octal, or base 8), DEC(decimal, or base 10), HEX(hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example-

    Serial.print(78, BIN) gives "1001110"

    Serial.print(78, OCT) gives "116"

    Serial.print(78, DEC) gives "78"

    Serial.print(78, HEX) gives "4E"

    Serial.print(1.23456, 0) gives "1"

    Serial.print(1.23456, 2) gives "1.23"

    Serial.print(1.23456, 4) gives "1.2345"

    map(value, fromLow, fromHigh, toLow, toHigh)

    Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

    value: the number to map. fromLow: the lower bound of the value’s current range.

    fromHigh: the upper bound of the value’s current range.

    toLow: the lower bound of the value’s target range.

    toHigh: the upper bound of the value’s target range.

    The value 1021 was picked by whoever wrote that code because he wanted to map the range [1021-0] to [7-0], most likely because the maximum joystick elongation yielded the analog value 1021.