simple matrix

hi,

this is my fist topic and i am very new to arduino but i am looking forward to learn it. i am a max/msp user for the last 10 years and the change from that is quit hard...

if somebody can point me in the right direction, i would be very thankful!

here my project:

9 led's (3x3 matrix)

7 4 1 8 5 2 9 6 3

i like to have 3 rows (7 4 1, 8 5 2, 9 6 3) and 3 columns ( 1 2 3, 4 5 6, 7 8 9) which are randomly turn on and off.

i guess i have to store the row and column numbers somewhere and randomly read those as outputs high / Low.

1 = (1 2 3) 2 = (4 5 6) 3 = (7 8 9) 4 = (7 4 1) 5 = (8 5 2) 6 = (9 6 3)

but...i don't know where to start....i try to understand the Arrays example but i think is the wrong way..

any starting point would be very nice !

thanks pe

I just had some fun and wrote the code that initially popped into my mind.

It uses a twodimensional array, and might seem a bit complex, but I tried to comment it in such a way that it could be understood.

[UNTESTED CODE]

//http://arduino.cc/en/Reference/Const
const int LIGHT_DELAY = 1000;
const byte MATRIX_WIDTH = 3;
const byte MATRIX_HEIGHT = 3;

//hold the pin numbers of the matrix
const byte matrix[MATRIX_HEIGHT][MATRIX_WIDTH] = { 
      { 2, 3, 4  }, //matrix[0][0] , matrix[0][1] , matrix[0][2] 
      { 5, 6, 7  }, //matrix[1][0] , matrix[1][1] , matrix[1][2] 
      { 8, 9, 10 }, //matrix[2][0] , matrix[2][1] , matrix[2][2] 
};

void setup(){
      //set pins as OUTPUT
      for (byte y=0; y<MATRIX_HEIGHT; y++){
            for (byte x=0; x<MATRIX_WIDTH; x++){
                  pinMode( matrix[y][x] , OUTPUT );
            }
      }
      
      //http://arduino.cc/en/Reference/RandomSeed
      randomSeed(analogRead(0));
}

void loop(){
      //http://arduino.cc/en/Reference/Random
      boolean doLightColumn = random(1);
      if (doLightColumn){
            //select a random index
            byte idx = random(MATRIX_WIDTH);
            //light the column at that index
            lightColumn( idx );
      } else {
            //select a random index
            byte idx = random(MATRIX_HEIGHT);
            //light the row at that index
            lightRow( idx );
      }
      //make actions visible
      delay(LIGHT_DELAY);
}

void lightColumn( byte idx ){
      matrixOff();
      for (byte y=0; y<MATRIX_HEIGHT; y++){
            digitalWrite( matrix[y][idx] , HIGH );
      }
}

void lightRow( byte idx ){
      matrixOff();
      for (byte x=0; x<MATRIX_HEIGHT; x++){
            digitalWrite( matrix[idx][x] , HIGH );
      }
}

void matrixOff(){
      for (byte y=0; y<MATRIX_HEIGHT; y++){
            for (byte x=0; x<MATRIX_WIDTH; x++){
                  digitalWrite( matrix[y][x] , LOW );
            }
      }
}

You could also have six singledimension arrays, one for each column and one for each row. But I think the code will get much dirtier that way.
And now, if you want to have a 4x4 matrix (or 5x3 for that matter) you only need to change the MATRIX_WIDTH MATRIX_HEIGHT constants as well as the matrix array definition.

Happy coding! :slight_smile:

AlphaBeta, 1000 thanks !

i realy want to go into this new world. so exciting !

but now i try to understand whats going on in the code

best, pe

I was just wondering recently whether you could have 2d arrays! Hehe, but then I found this post. Can you have 3d arrays (even 4d?)?

sciguy: C(++) allows you to have n dimensional arrays, though I've never found a use for anything above 3d.

Well, say you have three 3-3-3 cubes of LEDs. Then you could use a 4d array to control these.
Or maybe a 3-3-3 cube of RGB LEDs. Maybe then a 4d array could be useful.

For that sort of thing I would be inclined to make a class for a cube of LEDs and then have an array of them, or a class for an RGB LED and have a 3d array of them. Personal preference, but I find it makes the code a lot easier to make sense of.

For that sort of thing I would be inclined to make a class for a cube of LEDs and then have an array of them, or a class for an RGB LED and have a 3d array of them. Personal preference, but I find it makes the code a lot easier to make sense of.

AMEN!

hi

question: i want to have a delay of 13min between the changes. the problem is that the code runs a fist time complet (means with the 13 min delay) before the leds are the first time blinking.

where i have to place the (LIGHTOFF_DELAY) to run the leds at the beginning when i turn on the arduino ?

//http://arduino.cc/en/Reference/Const
const long LIGHT_DELAY = 110000;
const long LIGHTOFF_DELAY = 780000;
const byte MATRIX_WIDTH = 6;
const byte MATRIX_HEIGHT = 6;

//hold the pin numbers of the matrix
const byte matrix[MATRIX_HEIGHT][MATRIX_WIDTH] = {
{ 2, 3, 4 }, //matrix[0][0] , matrix[0][1] , matrix[0][2]
{ 5, 6, 7 }, //matrix[1][0] , matrix[1][1] , matrix[1][2]
{ 8, 9, 10 }, //matrix[2][0] , matrix[2][1] , matrix[2][2]
{ 2, 5, 8 }, //matrix[0][0] , matrix[0][1] , matrix[0][2]
{ 3, 6, 9 }, //matrix[1][0] , matrix[1][1] , matrix[1][2]
{ 4, 7, 10 }, //matrix[2][0] , matrix[2][1] , matrix[2][2]
};

void setup(){
//set pins as OUTPUT
for (byte y=0; y<MATRIX_HEIGHT; y++){
for (byte x=0; x<MATRIX_WIDTH; x++){
pinMode( matrix[y] , OUTPUT );
}
}

//http://arduino.cc/en/Reference/RandomSeed
randomSeed(analogRead(0));
}

void loop(){
//http://arduino.cc/en/Reference/Random
boolean doLightColumn = random(1);
if (doLightColumn){
//select a random index
byte idx = random(MATRIX_WIDTH);
//light the column at that index
lightColumn( idx );
} else {
//select a random index
byte idx = random(MATRIX_HEIGHT);
//light the row at that index
lightRow( idx );

}
//make actions visible
delay(LIGHT_DELAY);
}

void lightColumn( byte idx ){
matrixOff();
for (byte y=0; y<MATRIX_HEIGHT; y++){
digitalWrite( matrix[y][idx] , HIGH );
}
}

void lightRow( byte idx ){
matrixOff();
for (byte x=0; x<MATRIX_HEIGHT; x++){
digitalWrite( matrix[idx] , HIGH );

}

}

void matrixOff(){
for (byte y=0; y<MATRIX_HEIGHT; y++){
for (byte x=0; x<MATRIX_WIDTH; x++){
digitalWrite( matrix[y] , LOW );

}

}

delay(LIGHTOFF_DELAY);

}

You currently have delay(LIGHTOFF_DELAY); called in the matrixOff() function which is called at the start of both lightColumn() and lightRow(), meaning that this delay is started before the LEDs are light.

If I am right about what you are trying to do you probably want to remove that call to delay(LIGHTOFF_DELAY); and instead add:

matrixOff();
delay(LIGHTOFF_DELAY);

to the end of loop(). If you want the lights to stay on for the 13 minutes then you can leave out the above lines and just change LIGHT_DELAY to 13 minutes.

thanks zed0, it works perfect now.

slowly i start to understand how the code is working...

hello! is it possible to program blink sequences and let the row blink after an other? (left -> right, right -> left, bottom -> top, and all at one time)

thank you!