[go: up one dir, main page]

0% found this document useful (0 votes)
25 views4 pages

Slot3 - State Change Detection For Pushbuttons

The document discusses how to detect state changes of a pushbutton using Arduino. It explains how to read the button state, count button pushes, and toggle an LED every four button pushes. The circuit connects a button through a resistor to an Arduino pin and an LED to another pin. The code reads the button, compares to the last state, increments a counter on state change, and turns the LED on/off based on the counter value modulo four.

Uploaded by

Minh Thiện
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Slot3 - State Change Detection For Pushbuttons

The document discusses how to detect state changes of a pushbutton using Arduino. It explains how to read the button state, count button pushes, and toggle an LED every four button pushes. The circuit connects a button through a resistor to an Arduino pin and an LED to another pin. The code reads the button, compares to the last state, increments a counter on state change, and turns the LED on/off based on the counter value modulo four.

Uploaded by

Minh Thiện
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

State Change Detection (Edge Detection) for

pushbuttons
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. In this tutorial we learn how to
check the state change, we send a message to the Serial Monitor with the relevant
information and we count four state changes to turn on and off an LED.

Hardware Required
 Arduino or Genuino Board
 momentary button or switch
 10k ohm resistor
 hook-up wires
 breadboard

Circuit

Connect three wires to the board. The first goes from one leg of the pushbutton
through a pull-down resistor (here 10k ohm) 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.

Schematic
Code
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.

// 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 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 went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// 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);
}

You might also like