Tutorial.ButtonStateChange History

Hide minor edits - Show changes to markup

May 02, 2012, at 03:36 AM by Scott Fitzgerald -
Changed line 41 from:
to:
November 16, 2011, at 04:06 AM by Scott Fitzgerald -
Changed line 41 from:
to:
September 30, 2011, at 03:03 AM by Scott Fitzgerald -
Changed line 41 from:
to:
September 23, 2010, at 10:29 PM by Christian Cerrito -
Changed lines 3-4 from:

Button State Change Detection (Edge Detection)

to:

Button State Change Detection (Edge Detection)

September 23, 2010, at 09:10 PM by Christian Cerrito -
Changed lines 29-32 from:

to:

September 16, 2010, at 10:04 PM by Tom Igoe -
Changed line 41 from:
to:
August 24, 2010, at 11:31 PM by Christian Cerrito -
Deleted line 56:
August 24, 2010, at 11:17 PM by Christian Cerrito -
Changed lines 43-57 from:
to:
August 24, 2010, at 10:55 PM by Christian Cerrito -
Changed line 39 from:
to:
August 24, 2010, at 10:55 PM by Christian Cerrito -
Changed lines 7-18 from:
to:

Hardware Required

  • Arduino Board
  • momentary button or switch
  • 10K ohm resistor
  • breadboard
  • hook-up wire

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Deleted lines 24-29:

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

August 24, 2010, at 08:09 AM by Christian Cerrito -
Changed line 36 from:
to:
February 24, 2010, at 04:21 AM by Tom Igoe -
Changed line 34 from:
to:
Changed lines 36-59 from:
 /*
   State change detection (edge detection)
  	
  Often, you don't need to know the state of a digital input all the time,
  but you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON.  This is called
  state change detection, or edge detection.
  
  This example shows how to detect when a button or button changes from off to on
  and on to off.
  	
  The circuit:
  * pushbutton attached to pin 2 from +5V
  * 10K resistor attached to pin 2 from ground
  * LED attached from pin 13 to ground (or use the built-in LED on
    most Arduino boards)
  
  created  27 Sep 2005
  modified 17 Jun 2009
  by Tom Igoe
  	
  http://arduino.cc/en/Tutorial/ButtonStateChange
  
  */
to:
Deleted lines 37-94:
 // this constant won't change:
 const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 const int ledPin = 13;       // the pin that the LED is attached to

 // Variables will change:
 int buttonPushCounter = 0;   // counter for the number of button presses
 int buttonState = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button

 void setup() {
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT);
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
 }

 void loop() {
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) {
       // if the current state is HIGH then the button
       // wend from off to on:
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:  ");
       Serial.println(buttonPushCounter, DEC);
     } 
     else {
       // if the current state is LOW then the button
       // wend from on to off:
       Serial.println("off"); 
     }

     // save the current state as the last state, 
     //for next time through the loop
     lastButtonState = buttonState;
   }

   // turns on the LED every four button pushes by 
   // checking the modulo of the button push counter.
   // the modulo function gives you the remainder of 
   // the division of two numbers:
   if (buttonPushCounter % 4 == 0) {
     digitalWrite(ledPin, HIGH);
   } else {
    digitalWrite(ledPin, LOW);
   }

 }

December 30, 2009, at 05:20 PM by Tom Igoe -
Added lines 73-74:
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);
August 27, 2009, at 08:45 PM by Tom Igoe -
Changed lines 17-19 from:
to:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:18 PM by Tom Igoe -
Changed lines 31-32 from:
to:
July 05, 2009, at 07:17 PM by Tom Igoe -
Added lines 31-56:
Changed lines 58-65 from:

[@ /*

  State change detection (edge detection)

 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
to:
 // this constant won't change:
 const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 const int ledPin = 13;       // the pin that the LED is attached to
Changed lines 62-69 from:
 This example shows how to detect when a button or button changes from off to on
 and on to off.

 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in resistor on
   (most Arduino boards)
to:
 // Variables will change:
 int buttonPushCounter = 0;   // counter for the number of button presses
 int buttonState = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button
Changed lines 67-71 from:
 created  27 Sep 2005
 modified 17 Jun 2009
 by Tom Igoe

 http://arduino.cc/en/Tutorial/ButtonStateChange
to:
 void setup() {
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT);
   // initialize serial communication:
   Serial.begin(9600);
 }
Changed lines 74-130 from:
 */

// this constant won't change: const int buttonPin = 2; // the pin that the pushbutton is attached to const int ledPin = 13; // the pin that the LED is attached to

// Variables will change: int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button

void setup() {

  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }

    // save the current state as the last state, 
    //for next time through the loop
    lastButtonState = buttonState;
  }

  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }

} @]

to:
June 25, 2009, at 02:16 PM by Tom Igoe -
Changed lines 16-26 from:

The circuit

The schematic

click the image to enlarge

Attach:Button_schem2.png Δ Δ

to:

Schematic

click the image to enlarge

June 25, 2009, at 02:15 PM by Tom Igoe -
Changed lines 19-21 from:

The schematic

to:

The schematic

click the image to enlarge

Attach:Button_schem2.png Δ Δ

June 25, 2009, at 12:57 AM by Tom Igoe -
Changed line 19 from:
to:
June 25, 2009, at 12:56 AM by Tom Igoe -
Changed line 19 from:
to:
June 25, 2009, at 12:56 AM by Tom Igoe -
Changed line 19 from:
to:
June 25, 2009, at 12:54 AM by Tom Igoe -
Changed lines 16-17 from:
to:

The circuit

The schematic

June 25, 2009, at 12:33 AM by Tom Igoe -
Deleted lines 103-111:

June 24, 2009, at 11:13 PM by Tom Igoe -
Added lines 1-113:

Examples > Digital I/O

Button State Change Detection (Edge Detection)

Once you've got a pushbutton working, you often want to do some action based on how many times the button is pushed. To do this, you need to know when the button changes state from off to on, and count how many times this change of state happens. This is called state change detection or edge detection.

Connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-down resistor (here 10 KOhms) to ground. The second goes from the corresponding leg of the pushbutton to the 5 volt supply. The third connects to a digital i/o pin (here pin 2) which reads the button's state.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to voltage, so that we read a HIGH. (The pin is still connected to ground, but the resistor resists the flow of current, so the path of least resistance is to +5V.)

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, not connected to either voltage or ground. It will more or less randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

Circuit

The sketch below continually reads the button's state. It then compares the button's state to its state the last time through the main loop. If the current button state is different from the last button state and the current button state is high, then the button changed from off to on. The sketch then increments a button push counter.

The sketch also checks the button push counter's value, and if it's an even multiple of four, it turns the LED on pin 13 ON. Otherwise, it turns it off.

Code

/*
  State change detection (edge detection)

 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.

 This example shows how to detect when a button or button changes from off to on
 and on to off.

 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in resistor on
   (most Arduino boards)

 created  27 Sep 2005
 modified 17 Jun 2009
 by Tom Igoe

 http://arduino.cc/en/Tutorial/ButtonStateChange

 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }

    // save the current state as the last state, 
    //for next time through the loop
    lastButtonState = buttonState;
  }

  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }

}










Share