SHOW:
|
|
- or go back to the newest paste.
| 1 | // code is not verified to work. just illustrating some changes to be made. | |
| 2 | ||
| 3 | int delaytime = 2000; | |
| 4 | - | boolean statechanged(false); |
| 4 | + | |
| 5 | - | boolean lastbutton(LOW); |
| 5 | + | |
| 6 | bool statechanged = false; // notice how this is different? | |
| 7 | bool lastbutton = LOW; | |
| 8 | - | void setup() |
| 8 | + | bool currentButton = LOW; |
| 9 | ||
| 10 | - | pinMode(ledpin, OUTPUT); |
| 10 | + | |
| 11 | - | pinMode(switchpin, INPUT); |
| 11 | + | void setup() |
| 12 | {
| |
| 13 | pinMode(ledpin, OUTPUT); | |
| 14 | pinMode(switchpin, INPUT); | |
| 15 | } | |
| 16 | - | |
| 16 | + | |
| 17 | - | if (digitalRead(switchpin) == HIGH && lastbutton == LOW) |
| 17 | + | |
| 18 | {
| |
| 19 | currentButton = digitalRead(switchpin); | |
| 20 | if (currentButton == HIGH && lastbutton == LOW) | |
| 21 | {
| |
| 22 | statechanged = true; | |
| 23 | lastbutton = HIGH; | |
| 24 | - | lastbutton = digitalRead(switchpin); |
| 24 | + | |
| 25 | else | |
| 26 | - | |
| 26 | + | |
| 27 | - | |
| 27 | + | lastbutton = currentButton; // calling digitalRead() like could have a problem if the pin's state changed since you last checked |
| 28 | } | |
| 29 | ||
| 30 | ||
| 31 | digitalWrite(ledpin, HIGH); | |
| 32 | - | |
| 32 | + | |
| 33 | digitalWrite(ledpin, LOW); | |
| 34 | - | {
|
| 34 | + | |
| 35 | - | delaytime = delaytime - 499; |
| 35 | + | |
| 36 | - | } |
| 36 | + | |
| 37 | - | else |
| 37 | + | |
| 38 | - | {
|
| 38 | + | delaytime = delaytime - 499; |
| 39 | - | delaytime = 2000; |
| 39 | + | } |
| 40 | - | } |
| 40 | + | else |
| 41 | {
| |
| 42 | delaytime = 2000; | |
| 43 | } | |
| 44 | } |