1

I'm working on a small game on arduino but I'm facing some really odd behavior. When I run this code all of my leds flash on and off in less than a second even though delay(10000); should stop them for 10 seconds. However if I remove the while(game_running) block they go back to flashing every 10 seconds even though that block of code never actually runs.

int yellow = 12;
int green = 10;
int red = 9;
int blue = 11;

int debug = 13;

int sequence[1000];
int level = 0;

int game_running = false;

void setup() {                
  pinMode(yellow, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);

  pinMode(debug, OUTPUT);
}

void loop() {


  // Pre Game light flash
  digitalWrite(yellow, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(red, HIGH);
  digitalWrite(blue, HIGH);
  digitalWrite(debug,HIGH);
  delay(10000);
  digitalWrite(debug,LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
  digitalWrite(blue, LOW);
  delay(10000);

  game_running = false;
  level = 0;

  while(game_running) {

    sequence[level] = random(4); // Select next color

    // Blink LEDs
    for (int i=0; i < level; i++) {

       if(sequence[i] == 0) {
         digitalWrite(yellow, HIGH);
         delay(1000); 
         digitalWrite(yellow, LOW);
         delay(1000);
       } else if(sequence[i] == 1) {
         digitalWrite(green, HIGH);
         delay(1000); 
         digitalWrite(green, LOW);
         delay(1000);
       } else if(sequence[i] == 2) {
         digitalWrite(red, HIGH);
         delay(1000); 
         digitalWrite(red, LOW);
         delay(1000);
       } else {
         digitalWrite(blue, HIGH);
         delay(1000); 
         digitalWrite(blue, LOW);
         delay(1000);
       }
    }
    level += 1;
  }       
}
  • 3
    You're using the Uno? Your array sequence with 1000 entries eats up 2,000 bytes of RAM. (To say nothing of your other variables.) It might be causing some issues. – lemontwist Jun 24 '17 at 10:26
  • I have no problems running your code on my board, of course I changed the sequence array to have just 100 entries. Maybe that was a problem? – lemontwist Jun 24 '17 at 10:27
  • @lemontwistI changed the 1000 to 100 and now it runs as expected. Guess it run out of memory and removing the extra code freed some up? – Qwertie Jun 24 '17 at 10:43
  • Probably was the case. Memory management is very important. Microcontrollers have much more limited memory than PCs! – lemontwist Jun 24 '17 at 10:50
2

I will add an official answer just so that others can see it if they run into similar problems.

The issue is with the int sequence[1000] array, which eats up 2,000 bytes of data memory. The Uno only has 2,048 bytes of data memory so the instability is due to this memory hog of an array. Use a smaller number of array entries or switch to char (if you can) to use half the bytes of an int.

  • Compiler output: Sketch uses 1724 bytes (5%) of program storage space. Maximum is 32256 bytes. Global variables use 2017 bytes (98%) of dynamic memory, leaving 31 bytes for local variables. Maximum is 2048 bytes. Low memory available, stability problems may occur. – Jot Jun 24 '17 at 12:19
0

offtopic (but cannot put it in a comment):

I do not see any problem with your code (maybe it's in the circuit?)

You can rewrite the 10 s pregame flash as:

// Pre Game light flash
setAllLights(HIGH);
setAllLights(LOW);

void setAllLight(int state)
{
  digitalWrite(yellow, state);
  digitalWrite(green, state);
  digitalWrite(red, state);
  digitalWrite(blue, state);
  digitalWrite(debug, state);
  delay(10000);
}

You can even optimize this further by creating an array:

int[] lights = { yellow, green, red, blue, debug}

void SetAllLights(int state)
{
   for (int color = 0; color < sizeof(lights) / sizeof(int); color++)
   {
       digitalWrite(lights[color], state);
   }
}

You can rewrite the inside of the loop like:

switch (sequence[i]):
   case 0:
      flash(yellow);
      break;

   case 1:
      flash(green);
      break;

   case 2:
      flash(red);
      break;

   default:
      flash(blue);
      break;
}

void flash(int colorPin)
{
   digitalWrite(colorPin, HIGH);
   delay(1000); 
   digitalWrite(colorPin, LOW);
   delay(1000);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.