-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pushbutton as a toggle.cpp
66 lines (54 loc) · 1.67 KB
/
Pushbutton as a toggle.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
int LEDState=0;
int LEDPin=8;
int buttonPIN=12;
int buttonNew;
int buttonOld=1; //initialize it to 1, but why?
int dt = 100;
void setup() {
Serial.begin(9600);
pinMode=(LEDPin, OUTPUT);
pinMode=(buttonPin, INPUT);
}
void loop() {
buttonNew=digitalRead(buttonPin);
if(buttonOld == 0 && buttonNew == 1){
if (LEDState == 0){
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
else{
digitalWrite(LEDPin, LOW);
LEDState = 0;
}
}
// he doesn't explain this one well at all:
// whatever the value of buttonNew was, *now* goes into buttonOld
// that's what turns the cycle over.
// buttonOld initialized at 1
// buttonNew starts at 0 (maybe?), because there's nothing there.
// might be an implicit Boolean at work here:
// - first: a digital read of "nothing"
// - skips "if" statement
// - oldButton set to "0"
// - first button press, making newButton into 1
// - with oldButton==0 and newButton==1, you can get into loop
// The most transparent approach in the code would have been to use a flag to
// connect both the "on" and "off" states (as shown by another solution in the comments).
// It also would have been helpful if he had stated that "off" positions on the pushbuttons
// are read as "0" as a matter of course--at least it seems that way.
//Yet another idea would have been to turn the digitalRead as a variable--unfortunately, I can't find that solution in the comments.
buttonOld=buttonNew;
delay(dt);
// Note: this might be the painful, long way of doing it:
/*
else:
if (LEDState == 1){
digitalWrite(LEDPin, LOW);
LEDState = 0;
}
else{
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
*/
}