Go Down

### Topic: Code of the LED cube project. (Read 2369 times)previous topic - next topic

#### erickmoraes

##### Jul 14, 2012, 03:32 am
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!

#### nickgammon

#1
##### Jul 14, 2012, 03:45 am
Code: [Select]
`B100, B000, B000, B000, B000, B000, B000, B000, B000,`

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

Quote
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:

Code: [Select]
`15     <--- decimal0xF    <--- hexadecimal0b1111 <--- binaryB1111  <--- binary expressed another way`
Please post technical questions on the forum, not by personal message. Thanks!

#### PaulS

#2
##### Jul 14, 2012, 03:48 am
Quote
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).

Quote
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.

Quote
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?

Code: [Select]
`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.

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

See number 3.

#### erickmoraes

#3
##### Jul 14, 2012, 04:04 am

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.

#### erickmoraes

#4
##### Jul 14, 2012, 06:29 am
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?

#### nickgammon

#5
##### Jul 14, 2012, 07:04 am
At present, with digital pins, anything not LOW is considered to be HIGH.

Specifically the test in digitalWrite is:

Code: [Select]
` if (val == LOW) { *out &= ~bit; } else { *out |= bit; }`
Please post technical questions on the forum, not by personal message. Thanks!

#### erickmoraes

#6
##### Jul 14, 2012, 07:39 pm
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?

#### PaulS

#7
##### Jul 14, 2012, 09:45 pm
Quote
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.