Go Down

Topic: Strange problem writing to a 2D boolean array (Read 666 times) previous topic - next topic

Xaldibik



I'm trying to build a sort of basic "animation" system using a 2D array of booleans which will act as some sort of timeline. The rows (first dimension) distinguish the LEDs that will be turned on. The columns are sort of like the "frames". As the arduino runs its loop, it incrememnts the current frame, until looping at a specified frame. This loop point is the size of the second dimension of the 2D array:
Code: [Select]

const byte BLUE_1 = 4;
const byte GREEN_1 = 3;
const byte RED_1 = 2;
const byte BLUE_2 = 7;
const byte GREEN_2 = 6;
const byte RED_2 = 5;
const byte BLUE_3 = 10;
const byte GREEN_3 = 9;
const byte RED_3 = 8;
const int NUM_LEDS = 9;
byte LEDS[NUM_LEDS] = { RED_1 , GREEN_1, BLUE_1, RED_2, GREEN_2, BLUE_2, RED_3, GREEN_3, BLUE_3 };
const unsigned int LOOP = 2000;
boolean timeline[NUM_LEDS][LOOP];

void setup()
{
Serial.begin(9600);
outputAllPins();
}

void loop()
{
ledOn(LEDS[0]);
}



It seems to compile fine, which makes sense because the Arduino Uno has 32 kB of space, and 2000*9 = 18000, plus some extra variables and such. However, when I test "writing" to this animation timeline:

Code: [Select]

void setup()
{
Serial.begin(9600);
outputAllPins();

for(int led = 0; led < NUM_LEDS; led++)
{
for(int frame = 0; frame < LOOP; frame++)
{
timeline[led][frame] = true;
}
}
}



All of a sudden everything goes weird. If LOOP is a low enough number (less than 200), the first LED turns on and everything works fine. Past 207, nothing turns on. Between 203 and 207, everything goes all funny. Different LEDs come on besides the one connected to pin 2, and they flicker around and nothing seems to make sense.

I am using an Arduino Uno w/ an Atemga 328p. I have 9 LEDs connected from pin 2-10. The LEDs are then connected to a resistor then to ground.

Here's a copy of the full code.

Code: [Select]

const byte BLUE_1 = 4;
const byte GREEN_1 = 3;
const byte RED_1 = 2;
const byte BLUE_2 = 7;
const byte GREEN_2 = 6;
const byte RED_2 = 5;
const byte BLUE_3 = 10;
const byte GREEN_3 = 9;
const byte RED_3 = 8;
const int NUM_LEDS = 9;
byte LEDS[NUM_LEDS] = { RED_1 , GREEN_1, BLUE_1, RED_2, GREEN_2, BLUE_2, RED_3, GREEN_3, BLUE_3 };
const unsigned int LOOP = 2000;
boolean timeline[NUM_LEDS][LOOP];

void setup()
{
Serial.begin(9600);
outputAllPins();

for(int led = 0; led < NUM_LEDS; led++)
{
for(int frame = 0; frame < LOOP; frame++)
{
timeline[led][frame] = true;
}
}
}

void loop()
{
ledOn(LEDS[0]);
}

void outputAllPins()
{
for(int i=0; i<NUM_LEDS; i++) outputPin(LEDS[i]);
}

void outputPin(byte pin)
{
pinMode(pin, OUTPUT);
}

void ledOff(byte pin)
{
digitalWrite(pin, LOW);
}

void ledOn(byte pin)
{
digitalWrite(pin, HIGH);
}


Any help would be much appreciated, I'm very confused here.

PaulS

Quote
I am using an Arduino Uno w/ an Atemga 328p.

Which has 2K of SRAM.

Code: [Select]
boolean timeline[NUM_LEDS][LOOP];
No way are you going to fit 18000 bytes in 2000 memory locations.

Go Up