0

My code:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool buttonState = 0;         // variable for reading the pushbutton status
bool ledState = 0;
void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
  buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
 {        
  ledState = !ledState;
 }
  if (ledState == HIGH) 
 {             
   digitalWrite(ledPin, HIGH); // turn LED on: 
 }
   else 
 {
    digitalWrite(ledPin, LOW); // turn LED off:
 }
}

So this is supposed to toggle a led's light on and off. currently it does do that but its not always the case. Currently, if the button is pressed on the arduino board, it has a 50-50 chance of changing its state. What part of the code is leading to this outcome?

  • Do you have a pullup (or pulldown) resistor on your button? If not, then use pinMode(buttonPin, INPUT_PULLUP); then invert button logic, i.e. value is LOW when pushed and HIGH otherwise. – jfpoilpret Feb 23 '17 at 16:11
1

In addition to the debouncing suggested by the other answers, you have a further problem: your loop() will whiz through in a very short time, and run again while your finger is still on the button, turning the light off. And then run again, turning it back on again, and so on.

You need to wait for the button to be released before registering another change.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

bool ledState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
  bool buttonState = digitalRead(buttonPin);// read the state of the pushbutton value:
  if (buttonState == HIGH) 
  {        
    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);

    ledState = !ledState;
    if (ledState == HIGH) 
    {             
      digitalWrite(ledPin, HIGH); // turn LED on: 
    }
    else 
    {
      digitalWrite(ledPin, LOW); // turn LED off:
    }

    // Wait for the button to be released
    while (digitalRead(buttonPin)) {}

    // Poor-man's debounce. There are much better ways of doing this!
    delay(20);
}
1

If you're connecting the button from pin 2 to ground and not using an external pull-up, it can lead to trouble. Either use an external pull-up or set the internal one:

pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:

Also, you need to debounce the button.

0

The mechanical switches bounces. That's all. It's too fast for you to see it but MCU is much faster, so output state is switched many times before contacts are stabilized and no more bounces are causing "turning on and off".

The term you are looking for is debouncing

  • So how may I be able to stop this debounce? is it something I must change from software or Hardware? – Utsav Feb 23 '17 at 7:26
  • If you look for the term debouncing (I don't know, maybe on google), you'll found out there are several ways how to do it. Usual way is "confirmation" of state after about 10ms. – KIIV Feb 23 '17 at 7:29
  • @Utsav Or in this case you can add delay(50) and mentioned INPUT_PULLUP if you don't have external one and it should work just fine – KIIV Feb 23 '17 at 7:33

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.