Go Down

### Topic: Weird (Read 652 times)previous topic - next topic

#### asp55

##### Sep 15, 2008, 12:28 pm
I built a simple 8x8 matrix that's being driven straight from the arduino.
No problem with the construction, and off the bat: seemingly no problem with the code

The code takes an array of 8x8 arrays and displays them on the grid. (The length of the array of arrays is defined by a constant integer)

If there are 14 or fewer entries, no problem. 15 or more and it goes wonky. The only thing I can think of that even possibly explains this would be if after reading entry 14 in the array (well, 13 counting from 0) it starts reading junk from ram instead of 15 and so on...but I can't figure out why it would do that.

I would include videos of the matrix in action...but apparently I'm too new to the forum. Excellent.

#### asp55

#1
##### Sep 15, 2008, 12:29 pm
This is the code where it starts getting wonky.

The code for operating smoothly is exactly the same, except the constant numPatterns is set to 14, and there is one less entry in the patterns array
Code: [Select]
`const int numPatterns = 15;int anPins[8] = {18,17,16,15,14,2,3,4};int cathPins[8] = {5, 6, 7, 8, 9, 10, 11, 12};byte patterns[numPatterns][8][8] = {{ \{0,0,0,0,0,0,0,1}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,0,1,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,1,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,1,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \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,0,0,0,0}, \{0,0,0,0,0,1,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,0,0,0}, \{0,0,0,0,1,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,0,0,0}, \{0,0,0,1,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,0,0,0}, \{0,0,1,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \},{ \{0,0,0,0,0,0,0,0}, \{0,1,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \{0,0,0,0,0,0,0,0}, \}};void setup() {    for (int i = 0; i < 8; i++) {    pinMode(anPins[i], OUTPUT);    pinMode(cathPins[i], OUTPUT);  }  for (int i = 0; i < 8; i++) {    digitalWrite(anPins[i], LOW);    digitalWrite(cathPins[i], HIGH);  }}void loop() {  for(int i = 0; i < numPatterns; i++) {    pattern(patterns[i], 200);  }}void pattern(byte pat[8][8],unsigned long inc){  unsigned long T = millis() + inc;  while(millis() < T) {    for (int i = 0; i < 8; i++) {      digitalWrite(cathPins[i], LOW);      for (int j = 0; j < 8; j++) {        digitalWrite(anPins[j], pat[i][j]);      }      blank();      digitalWrite(cathPins[i], HIGH);    }  }}void blank() {  for (int i = 0; i < 8; i++) {    digitalWrite(anPins[i], LOW);  }}`

#### asp55

#2
##### Sep 15, 2008, 12:31 pm
You can also see videos of the matrix in action to see what I'm talking about on vimeo.

vimeo.com/1736220 <- 14 array entries
vimeo.com/1736215 <- 15 array entries

#### mem

#3
##### Sep 15, 2008, 01:25 pm

You have run out of RAM. Each entry in the table is 64 bytes. 64 *14 = 896 bytes which is more then the arduino has available after it starts up your sketch.

You can make the potential capacity of the array eight times larger by changing your array to handle bits instead of bytes. A search should turn up some example arduino code on this forum. One thread that had an example is here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1214715130;start=all

#### asp55

#4
##### Sep 15, 2008, 08:22 pm
That makes perfect sense...though I thought the atmega 168 had 1k. Still, my original code was extremely wasteful, thanks!

I've modified it as such:
Code: [Select]
`const int numPatterns = 16;int anPins[8] = {18,17,16,15,14,2,3,4};int cathPins[8] = {5, 6, 7, 8, 9, 10, 11, 12};byte patterns[numPatterns][8] = {{ \B00000001, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000010, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000100, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00001000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00010000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00100000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B01000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B10000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00000001, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00000010, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00000100, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00001000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00010000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B00100000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B01000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \},{ \B00000000, \B10000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000, \B00000000 \}};void setup() {    for (int i = 0; i < 8; i++) {    pinMode(anPins[i], OUTPUT);    pinMode(cathPins[i], OUTPUT);  }  for (int i = 0; i < 8; i++) {    digitalWrite(anPins[i], LOW);    digitalWrite(cathPins[i], HIGH);  }}void loop() {  for(int i = 0; i < numPatterns; i++) {    pattern(patterns[i], 500);  }}void pattern(byte pat[8],unsigned long inc){  unsigned long T = millis() + inc;  while(millis() < T) {    for (int i = 0; i < 8; i++) {      digitalWrite(cathPins[i], LOW);        digitalWrite(anPins[0], pat[i] & B00000001);        digitalWrite(anPins[1], pat[i] & B00000010);        digitalWrite(anPins[2], pat[i] & B00000100);        digitalWrite(anPins[3], pat[i] & B00001000);        digitalWrite(anPins[4], pat[i] & B00010000);        digitalWrite(anPins[5], pat[i] & B00100000);        digitalWrite(anPins[6], pat[i] & B01000000);        digitalWrite(anPins[7], pat[i] & B10000000);            digitalWrite(cathPins[i], HIGH);      blank();    }  }}void blank() {  for (int i = 0; i < 8; i++) {    digitalWrite(anPins[i], LOW);  }}`

While this did require a little more code in the pattern function, it reduced the overhead per pattern from 64 bytes to 8.

Thanks mem!

#### mem

#5
##### Sep 15, 2008, 08:30 pmLast Edit: Sep 15, 2008, 08:32 pm by mem Reason: 1
happy to help

The Atmega168 does have 1k RAM but some of that is used for the stack and for static variables in the arduino runtime code.

Go Up