School Project Need Help

I am a beginner to arduino, but I do have previous experience with programming. I am working on a project for school which is like an arcade game. I am trying to make it so that the leds will light up in an order and when the button is pressed those same leds will flash whenever the button is pressed.

Anyway, here is the code...

[///variables
//button variables
const int button = 13;
int buttonState = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;

//led variables
//LED array
const int ledPins[5] = {10,7,4,3,2};
boolean ledON = false;

//misc variables
int timer = 0;

void setup ()
{
  pinMode(button, INPUT);
  
  for(int i = 0; i < 4; i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(button);
  if (last != current)
  {
    delay(5);
    current = digitalRead(button);
  }
  return current;
}

void loop ()
{
  idle();
  while (digitalRead(button) == HIGH)
  {
    win();
    digitalWrite(button,LOW);
  }
}

//There is no winner yet
void idle ()
{
  for (int i = 0; i < 4; i++)
  {
    digitalWrite(ledPins[i], HIGH);
    delay(1000);
    digitalWrite(ledPins[i], LOW);
  }
  for(int i = 4; i > 0; i--)
  {
    digitalWrite(ledPins[i], HIGH);
    delay(1000);
    digitalWrite(ledPins[i], LOW);
  }
}

//You have won!
void win ()
{
  for(int i; i < 5; i++)
  {
    if(i < 5)
    {
      digitalWrite(ledPins[0], HIGH);
      digitalWrite(ledPins[1], HIGH);
      digitalWrite(ledPins[2], HIGH);
      digitalWrite(ledPins[3], HIGH);
      digitalWrite(ledPins[4], HIGH);
      delay(200);
      digitalWrite(ledPins[0], LOW);
      digitalWrite(ledPins[1], LOW);
      digitalWrite(ledPins[2], LOW);
      digitalWrite(ledPins[3], LOW);
      digitalWrite(ledPins[4], LOW);
      delay(200);
    }
    digitalWrite(button, LOW);
  }
}]

Is there a question ?

Oh right, I can't figure out how to make it flash when the button is pressed. If I hold the button down until the idle function is finished then it will go to the win loop. So how can I make it happen immediately after the button is pressed?

for(int i = 0; i < 4; i++)

This needs to be: for(int i = 0; i < 5; i++) you got 5 leds 0,1,2,3,4. <4 only gets you the 3. Think of it as "do while i<4".
Same goes for for(int i = 4; i > 0; i--)

digitalWrite(button,LOW);
Don't do this. The button is pressed, applying 5v to the digital INPUT pin. When you write to it, you are telling the Arduino to force the pin low. This can create a dead-nuts short on the pin. It could burn out the pin. Fortunately, since the pin is defined as INPUT, it probably won't. In any case, doing a digitalWrite to a pushbutton, won't turn it off. Lifting your finger turns it off.

Where do you save last? You compare against it, but I can't see that you ever store a value in it.

digitalWrite(button, LOW); You did this twice.

When you enter the idle() function, there are at least 8 or 10 seconds of delays in it. It won't leave the function until every instruction is completed. Only then can it read your button press. You will have to redesign your program to jump into a function which will turn on a LED and save the current millis() when you go back into it, check the millis() to see if 1000 milliseconds have passed. If not, fall out of it. When you see that millis passed are <= 1000 ms, turn off the LED, save current millis and do the same to the next LED.

Or something around those lines.

Luck, John

Thank you OldSalt/John. That makes sense, but I do not fully understand what millis is used for. I will do some reading on it and then try to make a code for it.

Doing what you want to do, with instantaneous response from the button press, requires that you not tie up the computer with delays. About the only way I know to do this is with keeping track of milliseconds passed. The code to implement this is exponentially more difficult than what you were doing.

This, of course requires that you bounce in and out of all of your functions and fall back to the loop. When you turn on a LED, the computer will probably check if it's time to turn off the LED a million times before it actually gets to do it. On the other hand, it wasn't doing anything anyhow.

There is an example program in the standard Arduino Examples called blinkWithoutDelay that may help you figure it out.

More luck, John

Here is a post by Robin2.

http://forum.arduino.cc/index.php?topic=223286.0

He wrote a brilliant sketch to control multiple LED's asynchronously without delays. It uses the concepts that you will need.

John

digitalWrite(button,LOW);
Don't do this. The button is pressed, applying 5v to the digital INPUT pin. When you write to it, you are telling the Arduino to force the pin low.

No, it is turning off the built-in pullup.
That's allowed, though what it is meant to achieve, I can't imagine.

Draw a schematic and photograph it with a cell phone and upload it. We need to know how your buttons are wired.

AWOL:
No, it is turning off the built-in pullup.
That's allowed, though what it is meant to achieve, I can't imagine.

Thanks for the info, AWOL. I'm still learning Arduino, so the info helps.
John