Code of the LED cube project.

Hey, everyone.

I was taking a look at this project, of a 3X3 Led cube (http://arduino.cc/playground/Main/LEDCube3x3), and had some doubts concerning the code. I tried to found the answers in the Reference, but couldn't understand it anyways.

The code can be directly accessed here: http://www.instructables.com/id/The-Arduino-LED-Cube/step5/Programming-Time/

I understand the meaning of each part, but still have some troubles with these issues:

1 - At the formation of the "PatternTable", what is the function of the letter "B", before each sequence defining a column of leds?

2 - When the pattern created should be displayed, the following command is used: "memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );"
It looks like this command copies each line of the PatternTable to be displayed. What happens then to the letters "B"?

3 - The basic element of the PatternTable has the format BXXX. In which part of the code is this element split, so that the information in each "X" may be obtained?

4 - What is the meaning of this syntax of the function digitalWrite: "digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );".

Hopefully, somebody here has already built the cube and studied the code.

Thank you!

B100, B000, B000, B000, B000, B000, B000, B000, B000,

They are basically binary numbers. Numbers to the base 2.

What happens then to the letters "B"?

It's just part of the number definition. You may as well ask what happens to the "foot" if someone six foot high walks into the room. Nothing happens to them. The "B" is just the number base.

These are equivalent:

15     <--- decimal
0xF    <--- hexadecimal
0b1111 <--- binary
B1111  <--- binary expressed another way

1 - At the formation of the "PatternTable", what is the function of the letter "B", before each sequence defining a column of leds?

B is an Arduino adaptation of 0b, meaning that the value is binary (base 2), not decimal (base 10).

2 - When the pattern created should be displayed, the following command is used: "memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );"
It looks like this command copies each line of the PatternTable to be displayed. What happens then to the letters "B"?

Nothing. The value stored in memory is always in binary. The B told the compiler to interpret the literal as a binary value when parsing it. The resulting value was stored in a memory address, in binary, with no conversion from decimal required.

3 - The basic element of the PatternTable has the format BXXX. In which part of the code is this element split, so that the information in each "X" may be obtained?

digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );

The value 1 is left shifted (multiplied by 2) some number of times (to create 1, 2, 4, 8, etc.) and used a mask to extract one bit from PatternBuf[n]. That one bit is one of the original digits in the binary number.

4 - What is the meaning of this syntax of the function digitalWrite: "digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );".

See number 3.

I thank you a lot for the ready answer.

I hadn't understood it was a binary number because the type of the array was a char, so it didn't even come to my mind. Thank you.

Ok, let me be dummy again.

digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) )

Supposing that:

LEDPin[ledpin++] = Y;
PatternBuf[patbufidx] = B111;
ledcol = 0;

Thus:

PatternBuf[patbufidx] & (1 << ledcol) = B111 & 001 = 0001

Then we'd have:

digitalWrite( Y, 1); //Ok!


For the next run, we'd have ledcol = 1. So,

PatternBuf[patbufidx] & (1 << ledcol) = B111 & 010 = 010

Then we'd have:

digitalWrite( Y, 010);

What does it mean, writting B010 to a pin? Is it high or low?

At present, with digital pins, anything not LOW is considered to be HIGH.

Specifically the test in digitalWrite is:

	if (val == LOW) {
		*out &= ~bit;
	} else {
		*out |= bit;
	}

Oh, now it all made sense.

One last question, if you may.

The way this code is structured, it is not possible having two active planes simultaneously, right?

The way this code is structured, it is not possible having two active planes simultaneously, right?

The way that the code is structured, no. Getting rid of the code that turns the other planes off would solve that problem, though.

Right.

I really thank you all.