Arduino

Sketch Using A Simply Delay Debouncer

// ===============================================================
// Turn an LED on and off with a debounced button
//
// When the button is pressed:
//   a. if the LED is off turn it on
//   b. if the LED is on turn it off
// ---------------------------------------------------------------
// State definitions:
//   0 - LED is off, button is LOW,  waiting for button to go HIGH
//   1 - LED is on,  button is HIGH, waiting for button to go LOW
//   2 - LED is on,  button is LOW,  waiting for button to go HIGH
//   3 - LED is off, button is HIGH, waiting for button to go LOW
//
//   Note: Initially the LED is off and the button is LOW
// ===============================================================

const int led    = 13;            // LED pin
const int button = 7;             // button pin
const int bounce = 10;            // button bounce delay (milliseconds)

int val    = 0;                   // button value 
int state  = 0;                   // state

void setup()
{
   pinMode(led,OUTPUT);           // set pin to output
   pinMode(button,INPUT);         // set pin to input
   digitalWrite(led,LOW);         // LED off
}

void loop()
{
   val = digitalRead(button);     // get button state (HIGH or LOW)

   switch(state)
   {
   case 0:                        // wait for button to go HIGH
      if (val == HIGH)
      {
         state = 1;               // change state
         digitalWrite(led,HIGH);  // LED on
         delay(bounce);           // de-bouncing
      }
      break;
   case 1:                        // wait for buttonto go LOW
      if (val == LOW)
      {
         state = 2;               // change state
         delay(bounce);           // de-bouncing
      }
      break;
   case 2:                        // wait for button to go HIGH
      if (val == HIGH)
      {
         state = 3;               // change state
         digitalWrite(led,LOW);   // LED off
         delay(bounce);           // de-bouncing
      }
      break;
   case 3:                        // wait for button to go LOW
      if (val == LOW)
      {
         state = 0;               // change state
         delay(bounce);           // de-bouncing
      }
      break;
   default:                       // should never get here but just in case
      state = 0;                  // change to initial state
      digitalWrite(led,LOW);      // LED off
      delay(bounce);              // de-bouncing
   } 
}

Sketch Using The Bounce Library

// ===============================================================
// Turn an LED on and off with a debounced button
// (use the C++ Bounce library)
//
// When the button is pressed:
//   a. if the LED is off turn it on
//   b. if the LED is on turn it off
// ---------------------------------------------------------------
// State definitions:
//   0 - LED is off
//   1 - LED is on
//
//   Note: Initially the LED is off and the button is low
// ===============================================================

#include <Bounce.h>

#define LED    13                 // LED pin
#define BUTTON 7                  // button pin

// create a Bounce object for the button

Bounce bouncer = Bounce(BUTTON,5);

int state;                        // LED state - on or off

void setup()
{
   pinMode(LED,OUTPUT);           // set pin to output
   pinMode(BUTTON,INPUT);         // set pin to input
   digitalWrite(LED,LOW);         // set LED pin low
   state = 0;                     // state - LED off
}

void loop()
{
   if (bouncer.update() && bouncer.read() == LOW)
   {
      if (state == 0)
      {
         state = 1;
         digitalWrite(LED,HIGH);   // LED on
      }
      else
      {
         state = 0;
         digitalWrite(LED,LOW);    // LED off
      }
   }
}