1

I'm fairly new to Arduino and am trying to use a push button to toggle an LED on and off using a momentary press of the button. This code is lifted straight off the tutorial on the Arduino website but is not working for me. It seems to send the LED on a loop - switching between on and off by itself, and pressing the button pauses the cycle.

Any ideas as to why it is doing that?

int buttonPin = 2;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

}

void loop()
{


  reading = digitalRead(buttonPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
if (reading == HIGH  && previous == LOW && millis() - time > debounce) {
  if (state == HIGH)
    state = LOW;
  else 
    state = HIGH;
time = millis();    
}

else 
  if (state == LOW)
    state = LOW;
  else 
    state = HIGH;
  digitalWrite(ledPin, state);
 //Serial.println(buttonPin);
  // 
 Serial.println(reading);
previous = reading;
} 
3
  • 1
    Please give a link to that tutorial. You have chosen the wrong example sketch, there are a number of problems with it. I like the Bounce2 library: github.com/thomasfredericks/Bounce2/wiki That library is in the Arduino IDE in the library manager.
    – Jot
    Commented Nov 14, 2017 at 18:10
  • You should really, really use "{" and "}" for all your if-else-while-for, even if the block will contains only one statement. It's easy to misread if-else without proper brackets.
    – user31481
    Commented Nov 14, 2017 at 22:57
  • Just cheak your "If …else "logic ,it seems to get a mistake.
    – Joey
    Commented Nov 15, 2017 at 8:14

4 Answers 4

4

I tested your sketch and it works. I simplified it; it's the same logic, but in a more concise way:

void loop()
{
    reading = digitalRead(buttonPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if(reading == HIGH  && previous == LOW && millis() - time > debounce) {
        state = !state;
        time = millis();
    }

    digitalWrite(ledPin, state);
    Serial.println(reading);
    previous = reading;
}

setup is still the same.

1
pinMode(buttonPin, INPUT);

Do you have an external pull-down resistor? Without one, the pin is floating when the button isn't pressed so it might read anything and you may be picking up spurious noise.

It is much more common to wire a button so that it connects to ground and reads LOW when pressed. If you wire it that way, you can use the internal pull-up resistors by using pinMode(pin, INPUT_PULLUP) and you won't need any external resistor for the button.

0

The problem is in the "else" part. Just remove it and you are ready to go ! Also make sure that in the circuit, the digital read would be low, when not pressing button.

0

My one line answer

If (isPressed(BTN)) digitalWrite(LED, ~digitalRead(LED));

Is all you need to flip an led when a button is pressed.

2
  • 1
    But "'isPressed' was not declared in this scope". Some #include is missing.
    – user31481
    Commented Nov 14, 2017 at 23:00
  • The correct code: if(digitalRead(button)) digitalWrite(led, !digitalRead(led));
    – JDG
    Commented Jan 4, 2020 at 5:38

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.