| 1 |
/* |
| 2 |
State change detection (edge detection) |
| 3 |
|
| 4 |
Often, you don't need to know the state of a digital input all the time, but |
| 5 |
you just need to know when the input changes from one state to another. |
| 6 |
For example, you want to know when a button goes from OFF to ON. This is called |
| 7 |
state change detection, or edge detection. |
| 8 |
|
| 9 |
This example shows how to detect when a button or button changes from off to on |
| 10 |
and on to off. |
| 11 |
|
| 12 |
The circuit: |
| 13 |
- pushbutton attached to pin 2 from +5V |
| 14 |
- 10 kilohm resistor attached to pin 2 from ground |
| 15 |
- LED attached from pin 13 to ground through 220 ohm resistor (or use the |
| 16 |
built-in LED on most Arduino boards) |
| 17 |
|
| 18 |
created 27 Sep 2005 |
| 19 |
modified 30 Aug 2011 |
| 20 |
by Tom Igoe |
| 21 |
|
| 22 |
This example code is in the public domain. |
| 23 |
|
| 24 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection |
| 25 |
*/ |
| 26 |
|
| 27 |
// this constant won't change: |
| 28 |
const int buttonPin = 2; // the pin that the pushbutton is attached to |
| 29 |
const int ledPin = 13; // the pin that the LED is attached to |
| 30 |
|
| 31 |
// Variables will change: |
| 32 |
int buttonPushCounter = 0; // counter for the number of button presses |
| 33 |
int buttonState = 0; // current state of the button |
| 34 |
int lastButtonState = 0; // previous state of the button |
| 35 |
|
| 36 |
void setup() { |
| 37 |
// initialize the button pin as a input: |
| 38 |
pinMode(buttonPin, INPUT); |
| 39 |
// initialize the LED as an output: |
| 40 |
pinMode(ledPin, OUTPUT); |
| 41 |
// initialize serial communication: |
| 42 |
Serial.begin(9600); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
void loop() { |
| 47 |
// read the pushbutton input pin: |
| 48 |
buttonState = digitalRead(buttonPin); |
| 49 |
|
| 50 |
// compare the buttonState to its previous state |
| 51 |
if (buttonState != lastButtonState) { |
| 52 |
// if the state has changed, increment the counter |
| 53 |
if (buttonState == HIGH) { |
| 54 |
// if the current state is HIGH then the button went from off to on: |
| 55 |
buttonPushCounter++; |
| 56 |
Serial.println("on"); |
| 57 |
Serial.print("number of button pushes: "); |
| 58 |
Serial.println(buttonPushCounter); |
| 59 |
} else { |
| 60 |
// if the current state is LOW then the button went from on to off: |
| 61 |
Serial.println("off"); |
| 62 |
} |
| 63 |
// Delay a little bit to avoid bouncing |
| 64 |
delay(50); |
| 65 |
} |
| 66 |
// save the current state as the last state, for next time through the loop |
| 67 |
lastButtonState = buttonState; |
| 68 |
|
| 69 |
|
| 70 |
// turns on the LED every four button pushes by checking the modulo of the |
| 71 |
// button push counter. the modulo function gives you the remainder of the |
| 72 |
// division of two numbers: |
| 73 |
if (buttonPushCounter % 4 == 0) { |
| 74 |
digitalWrite(ledPin, HIGH); |
| 75 |
} else { |
| 76 |
digitalWrite(ledPin, LOW); |
| 77 |
} |
| 78 |
|
| 79 |
} |