Pages: [1]   Go Down
Author Topic: Code of the LED cube project.  (Read 1097 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Logged

Deixa ardUer!

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18819
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
15     <--- decimal
0xF    <--- hexadecimal
0b1111 <--- binary
B1111  <--- binary expressed another way
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Deixa ardUer!

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Deixa ardUer!

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18819
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Specifically the test in digitalWrite is:

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


Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Deixa ardUer!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right.

I really thank you all.
Logged

Deixa ardUer!

Pages: [1]   Go Up
Jump to: