You are on page 1of 3

SELECTOR SWITCH WITH ARDUINO

A fellow Arduino guy from Texas asked about how to set up a multi-mode selector switch using
a single pushbutton and 3 states (not including the boot up state). What follows is this
discussion that we had becoming a back and forth collaboration of ideas until I got off work and
decided to see if I could make it more robust and simpler at the same time.
Lets talk about the original code it essentially worked with some quirks.
The original code would continuously set all the pins every loop, even though it didnt need
to
It also tried to process the loop even if the mode was equal to zero (IE; No button presses
have occurred yet and in that case, all LEDs should be off)
Lastly, it got weird when mode was incremented to 4 and set back to 1 and in here is the
behavior that I wanted to eliminate. It would change to mode 1 and 2 and 3 when the key was
PRESSED but would only go back to mode 1 when button was released when in mode 3. This
is just not the way to make all the button presses seem similar. All the other buttons react on
the button press, not the release.
So it was SWITCH/CASE to the rescue. With switch case statements I could handle all the
mode switching and deal with mode 1 and any other mode that was not 2 or 3 with the default
statement. This allowed it to have all button mode migration to appear to behave the same.
The code also makes sure that pins do not get changed when they dont need to be IE; every
loop traversal.
On to the final code example

1
2
3
4
5
6
7
8
9
10
11
12
13
14

/*
Using a single switch to select between 3 modes
*/
// Schematic: http://www.pwillard.com/files/mode4.jpg
//===============================================================
// Global Variables & Constants
//===============================================================
const
const
const
const
int
int
int
int

int
int
int
int

ledPinOne = 2; // LED1 ANODE


ledPinTwo = 4; // LED2 ANODE
ledPinThree = 7; // LED3 ANODE
modePin = 13; // Active HIGH, held low by 4.7K

mode = 0; // Selector State (Initial state = ALL OFF)


val = 0; // Pin 13 HIGH/LOW Status
butState = 0; // Last Button State
modeState = 0; // Last Mode State

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled


//===============================================================
// SETUP
//===============================================================
void setup () {
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(modePin, INPUT);
if (debug){
Serial.begin(9600);
Serial.print("Initial Mode: ");
Serial.println(mode);
Serial.print("Setup Complete\n");
}
}
//===============================================================
// Main Loop
//===============================================================
void loop() {
val = digitalRead(modePin);
// If we see a change in button state, increment mode value
if (val != butState && val == HIGH){
mode++;
}
butState = val; // Keep track of most recent button state
// No need to keep setting pins *every* loop
if (modeState != mode){
// If no keys have been pressed yet don't execute
// the switch code below
// if (mode != 0) {
switch ( mode ) {
//case 1 is actually handled below as default
case 2:
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
showState();
break;
case 3:
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
showState();
break;
default:
mode = 1;
// loop back to 1 by default, seems redundant but
// it also handles the "mode is > 3" problem
digitalWrite(ledPinThree, LOW);
digitalWrite(ledPinOne, HIGH);
showState();

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

break;
} // end switch
// } // end of "if mode = 0" check
} // end of ModeState check
modeState = mode; // Keep track of mode recent mode value
delay(10); // slow the loop just a bit for debounce
}
//===============================================================
// Subroutine
//===============================================================
void showState() {
if (debug){
Serial.print("Mode: ");
Serial.println(mode);
}
}

You might also like