Pages: [1]   Go Down
Author Topic: 3by3 led cube control by matrix not working  (Read 1175 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I have made a  Led cube got it working i have programed it but i wanna move to the next step of saving the image of each pattern as a byte array.....
 i couldn't use byte because it only accepts 8 values and i have 9 leds.. I wrote the arrays as ints instead but when i upload it nothing happens to the cube..(power shows on mega just the cube doesnt work).

If there is a way to still do it with bytes that may help if it has any advantages working with bytes..

i tried to write some code but nothing happens here it is...

Code:
int levelPins[] = {22,23,24};
int colPins[] = {30,31,32,33,34,35,36,37,38};

int first[3][9] = {
111000000,
011100000,
001110000,
};

void setup () {
  //set all output
  for (int i=0; i<3; i++) {
    pinMode(levelPins[i], OUTPUT);
  }
  for (int j=0; j<9; j++) {
    pinMode(colPins[j], OUTPUT);

  }
}

void show (int image[3][9]) {
 for (int level=0; level<3; level++) {
  digitalWrite(levelPins[level], LOW);
  
   for (int col=0; col < 9; col++) {
    int pixel = image[level][col];
    
  if (pixel == 1) {digitalWrite(colPins[col], HIGH);}
 
 delay(30);
 digitalWrite(colPins[col], LOW);
       } // col for
   digitalWrite(levelPins[level], HIGH);
   } // level for
 } // end of show


void loop() {

show(first);
  }
Here is a video of the working led cube without images saved as arrays.. that code is somewhere in ...http://arduino.cc/forum/index.php/topic,69174.0.html
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 152
Posts: 5757
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int first[3][9] = {
111000000,
011100000,
001110000,
};

This isn't an array of three rows of nine ints, it's an array of three ints, try this:

Code:
int first[3] = {
111000000,
011100000,
001110000,
};

Then you need to modify your "show()" function to look at the the individual bits in each int:

Code:
void show (int image[3]) {
 for (int level=0; level<3; level++) {
  digitalWrite(levelPins[level], LOW);
   
   for (int col=0; col < 9; col++) {
    int bitmask = 1<<col;
    int pixel = image[level]&bitMask;

    if (pixel != 0) {digitalWrite(colPins[col], HIGH);}
...
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int first[3][9] = {
111000000,
011100000,
001110000,
};

This isn't an array of three rows of nine ints, it's an array of three ints, try this:


So will declaring ,
111000000,
011100000,
001110000,
without the [9] make 3 arrays size 9? what was wrong with the first 1 i dnt get it but i will try ur method, i just want to understand what im doing. Thx for the reply.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

also i dont understand this bit 
Code:
for (int col=0; col < 9; col++) {
    int bitmask = 1<<col;
    int pixel = image[level]&bitMask;
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That part of the code takes the value in image[level] and splits it into it's individual bits and places them one by one in the variable pixel.
The << is a shift operation and the & is a logical AND.
The logical AND with the bit mask sets all the bits to zero apart from the bits in the bit mask.
The << shifts the bitmask one place to the left each time round the loop.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
111000000,
011100000,
001110000,
Why are some constants in decimal, and others in octal?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
111000000,
011100000,
001110000,
Why are some constants in decimal, and others in octal?

Umm... I have no idea what you are talking about so i guess i got some reading to do.

What i was trying to achieve is each line represents a level on my cube each 1 or 0 represents the led in that levels status.
so "011100000" represents 1st led off second on third on fourth on... ect..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so "011100000" represents 1st led off second on third on fourth on... ect.
In C, a constant starting with a zero is taken to be an octal value, in this case, the equivalent decimal value is 2 392 064.

If you want a binary constant, prefix the number with "0b".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so "011100000" represents 1st led off second on third on fourth on... ect.
In C, a constant starting with a zero is taken to be an octal value, in this case, the equivalent decimal value is 2 392 064.

If you want a binary constant, prefix the number with "0b".

I just realized what i was doing wrong with my original code..... 
i declared my array wrong
Code:
int first[3][9] = {
111000000,
011100000,
001110000,
};

Should have been ...
Code:
int first[3][9] = {
{1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0, 0},
};

if i want declare them as bytes i should do this?
Code:
int first[3]= {
0b111000000,
0b011100000,
0b001110000,
};

do i have to change the array type to byte? also i tried this method but used the "B" prefix and it only let me use upto 8 numbers.. i have 9 leds so thats a problem.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
used the "B" prefix and it only let me use upto 8 numbers
If you need 9 bits, use "0b" as the prefix.
See reply #7.
The "B" prefix is an Arduino convenience.
« Last Edit: August 26, 2011, 12:18:50 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: