-4

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.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • 1
    ‚Serial‘ means it uses serial transmision protocol. DEC means decimal. – dboy May 13 '20 at 05:31
  • 1
    *As you can tell I'm pretty new to C++* -- FYI -- The questions you're asking really have little to do with the C++ language, and more to do with the Arduino API functions. An expert in C++ who has not done anything with Arduino could not answer the questions you've asked. – PaulMcKenzie May 13 '20 at 06:26
  • 2
    please don't ask questions that can be answered by referring to the Arduino manual – Piglet May 13 '20 at 06:57
  • And the DEC means that the person who wrote this probably didn't know very well what they were doing. DEC is the default for print statements. When you see that it is usually a sign that someone isn't that fluent in Arduino coding. Every single one of them can be deleted without changing anything. – Delta_G May 13 '20 at 14:41

1 Answers1

0

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.

Community
  • 1
  • 1
Piglet
  • 27,501
  • 3
  • 20
  • 43