0

I have this code and it is a reaction light game. When you press the button on pin 11 it starts the game and the lights should osculate back and forth. The main goal is to press the button when it hits the middle led and the osculating will slow down. I cannot get the leds to turn on help?

    int ledPin;
    int direction;
    int isChanging;
    unsigned long lastChange;

void setup() {
   //set pins 2 to 10 to be output
  // and LEDs off
   int pin;
    for (pin = 2; pin <= 10; pin++) {
       pinMode(pin, OUTPUT);
       digitalWrite(pin, LOW);
    }
     //set pin 11 to be input
     pinMode(11, INPUT);

     ledPin = 2;
     direction = 1;
     lastChange = millis();
  }

 void loop() {
    if (isChanging) {
    if (millis() - lastChange > 500) {
        //turn off old LED
        digitalWrite(ledPin, LOW);
        //reset time
        lastChange = millis();
        //change the led
        ledPin = ledPin + direction;
        if (ledPin > 10) {
          direction = -1;
        ledPin = 9;
      } else (ledPin < 2); {
        direction = +1;
        ledPin = 3;
     }

       //turn on new LED
        digitalWrite(ledPin, HIGH);
     }
       //check for button
       if (digitalRead(11)) {
        isChanging = 0;
     }
     }
    else {
       //wait for them to let go of the button
       while (digitalRead(11)) {
     }
       //wait for it to be pressed again
       while (!digitalRead(11)) {
     }
       //wait for them to let go of the button
       while (digitalRead(11)) {
     }
    }
   }`
1
  • Are you using an external pullup for your button pin?
    – chrisl
    Commented Mar 2, 2018 at 10:29

1 Answer 1

1

Your are not explicitly initializing the variable isChanging, so the compiler sets it to 0. This means, that your code will directly go to the last else statement and cannot at any time reach the LED code, because you are not changing the variable in the else statement.

But I don't really understand, why you were using this variable in the first place. Try to use purely non-blocking code. For checking the button you can do something like this:

boolean button_pressed = false;
unsigned long button_timestamp = 0;

void loop(){
  ...
  if(!button_pressed && digitalRead(11)){
    button_pressed = true;
    button_timestamp = millis();
  }
  if(button_pressed && millis() - button_timestamp > 50){
    if(digitalRead(11)){ // real button press
      button_pressed = false;
      // check, if the middle LED is ON and act accordingly
      // or, if the game hasn't started yet, start it
    } else { // just a bounce of the button, not a real button press
      button_pressed = false;
    }
  }

  // LED CODE
  if (millis() - lastChange > 500) {
    ...
  }
}

Note, that this code will not wait for the button to be released, but reacting, when the button was held down for 50ms. You can change this value, to a one, that is more fitting your used button, but it is a good start point.

Also in your LED code, you have to replace the 500 in the millis statement with a variable, since you want to change the speed.

For handling starting the game with the same button, you can use a state variable, which contains a value of means "Game Stopped" or "Game Started".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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