Wednesday, December 7, 2011

Arduino Push Button ON/OFF Example


Someone posted a request for this in the comments. After a quick search I couldn't find a simple example on how to do this so here's one I made based on the Debounce example sketch in the Arduino IDE.

arduino codeDebouncePushButtonOnOff

/* 
 Debounce
 
 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 created 21 November 2006
 by David A. Mellis
 modified 3 Jul 2009
 by Limor Fried
 
This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// 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 ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

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

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

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

 

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    
   // this is all that's new to the code 
   // toggles the ledState variable each time the button is pressed 
      if (buttonState == HIGH) {
      ledState = !ledState;
      Serial.println(ledState);
    } 
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  

  
  // set the LED using the state of the button:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

6 comments:

fatmi said...

Very clear instruction. Electronic students can be helped through.

what is arduino

Anonymous said...

Seems like there is a problem with

if (buttonState == HIGH) {

being that buttonState never changes.. Instead this should be

if (reading == HIGH) {

Besides that it works great! Thanks

Matt Deplaris said...

Works great for me!

Matt D

Justin Mullis said...

I attempted modifying this to control a relay rather than a LED, by changing a few values, but copied and pasted parts to duplicate the system but with additional parts. I have a 4ch relay shield, and 4 push buttons, in the example ill provide below youll notice what ive done. can you point me in the right direction here??



const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin1 = 3; //Additional button for Relay #1
const int ledPin = 7; // the number of the LED pin
const int ledPin1 = 6; // Pin assigned to second relay addition

// Variables will change:
int ledState = LOW; // the current state of the output pn WAS set to HIGH for default
int ledState1 = LOW;
int buttonState; // the current reading from the input pin
int buttonState1;
int lastButtonState = LOW; // the previous reading from the input pin
int lastButtonState1 = LOW; // Addition, plus all lines without comments


long lastDebounceTime = 0;
long lastDebounceTime1 = 0;
long debounceDelay = 50;

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

void loop() {
int reading = digitalRead(buttonPin);
int reading1 = digitalRead(buttonPin1);


// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
if (buttonState == HIGH) {
ledState = !ledState;
Serial.println(ledState);
}
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPin1, ledState1);
lastButtonState1 = reading;
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime1 = millis();

if (buttonState == HIGH) {
ledState1 = !ledState1;
Serial.println(ledState1);
}
}

if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState1 = reading;
}
digitalWrite(ledPin1, ledState1);
lastButtonState = reading1;
}

Anonymous said...

Hi Dan,
I know this is a bit old but all the same... very well explained. Thanks for taking the time and effort! :)

I have been playing around with it to handle 2 or more Buttons/LEDs, it is not working yet but I still have high hopes.

Edward M. Meshuris said...

So this code is pretty bad. Ill just touch on the fact that there more comments than code here. That is usually a code smell.
What makes this code bad is that the the digital write call happens on every loop.
Also, good job on copying and pasting from Arduino's website.
I'm willing to bet you won't change your code or allow this comment to get on your site.
Ciao