Arduino Programming – State change
How to program a state change functionality. ‘State change detection’ is a method to see when a button is pressed or released. You can use it to fire a different action each press of a button.
For example the “Play/Pause” button on a cd-player behaves like that. The first press is play, the next press is pause, the next play again etc. Depending the state the action is different ( play > pause, pause > play ).
In this tutorial I explain also the basic code structure ( setup, loop ) of Arduino script.
Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables ).
Download the Arduino ( .pde ) files : arduino_tutorial_statechange.zip
OnOff.pde :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /*
LED on when you press a button.
LED off when you release a button.
created 01-12-2009 by kasperkamperman.com
*/
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT); // initialize the button pin as a output
}
void loop() {
// read the pushbutton input pin
buttonState = digitalRead(buttonPin);
// turns LED on if the buttonState=HIGH or off if the buttonState=LOW
digitalWrite(ledPin, buttonState);
}
State_change.pde :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 /*
Set a state of a variable when you press a pushbutton ( the
button went from off to on ).
created 01-12-2009 by kasperkamperman.com
based on example 'State change detection' by Tom Igoe
*/
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int ledState = 0; // remember current led state
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT); // initialize the button pin as a output
}
void loop() {
// read the pushbutton input pin
buttonState = digitalRead(buttonPin);
// check if the button is pressed or released
// by comparing the buttonState to its previous state
if (buttonState != lastButtonState) {
// change the state of the led when someone pressed the button
if (buttonState == 1) {
if(ledState==1) ledState=0;
else ledState=1;
}
// remember the current state of the button
lastButtonState = buttonState;
}
// turns LED on if the ledState=1 or off if the ledState=0
digitalWrite(ledPin, ledState);
// adding a small delay prevents reading the buttonState to fast
// ( debouncing )
delay(20);
}
If you like to work with more than two states, do something like below. And use a switch or if statement later to perform some action on a certain stateNum value.
1
2
3
4 if (buttonState == 1) {
stateNum++;
if(stateNum>2) stateNum=0;
}
Art&Technology related bookmarks