Search code examples
arduinoarduino-unoarduino-c++

I'm trying to make a button turn the LED on and off, but it just stays off instead


I started with code to make an LED turn on while the button is pressed. That worked. But then I tried to tweek it so that the button would act like on 'on-off' swtich, where you only press it once to switch between states.

The led works with the older code (below) so I don't think its a problem with wiring. FYI i skipped the setup function, its the same as when I made it blink.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int switcher = 0;

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (switcher = 0) {
      switcher = 1;
      delay(500);
    }
    else if (switcher = 1) {
      switcher = 0;
      delay(500);
    }
  }
  if (switcher == 1) {
    digitalWrite(ledPin, HIGH);
  }
  /*
  else {
    digitalWrite(ledPin, LOW);
  }
  */

}

Solution

  • Delay is only used if you want the whole system to stop. It is also used for learning purposes in the beginning of an Arduino career. In a real application you'll use a delay library or use timing. If you use delay in an application then you can't read the button HIGH event, that's means the button can only be read in the 501ms exactly after 500ms of delay, you'll have an 1ms window or less, that is nearly impossible for a human to time. Anyway you should look in to the "Blink Without Delay" example from Arduino.

    Also you must use a pull up resistor for buttons or declare a INPUT_PULLUP for the pinMode in the setup, to avoid bouncing, see example below.

    This is how you solve the code:

    // defined constants in Arduino don’t take up any program memory space on the chip.
    #define buttonPin 2;
    #define ledPin 13;
    
    // bytes are half the size of int's, but restricted to a max value of 255
    byte value;
    byte oldValue = 0;
    byte state = 0;
    
    void setup()
    {
      pinMode(buttonPin , INPUT_PULLUP);
      pinMode(ledPin, OUTPUT);
    }
    
    void loop()
    {
      value = digitalRead(buttonPin );
      if(value && !oldValue) // same as if(button == high && oldValue == low)
      {
        //we have a new button press
        if(state == 0) // if the state is off, turn it on
        {
          digitalWrite(ledPin, HIGH);
          state = 1;
        }
        else // if the state is on, turn it off
        {
          digitalWrite(ledPin, LOW);
          state = 0;
        }
        oldValue = 1;
      }
      else if(!value && oldValue) // same as if(button == low && oldValue == high)
      {
        // the button was released
        oldValue = 0;
      }
    }