SHOW:
|
|
- or go back to the newest paste.
| 1 | - | long previousMillis = 0; |
| 1 | + | unsigned long previousMillis = 0; |
| 2 | - | long stoptime = 2000; |
| 2 | + | unsigned long stoptime = 2000; |
| 3 | unsigned long currentMillis = millis(); | |
| 4 | ||
| 5 | ||
| 6 | int led = 13; | |
| 7 | int ledstate = LOW; | |
| 8 | ||
| 9 | boolean timerOn = false; // know when to check for the timer | |
| 10 | boolean toogleLED = true; // set a flag so the switch only happens once | |
| 11 | ||
| 12 | void setup(){
| |
| 13 | - | void loop(){
|
| 13 | + | |
| 14 | - | previousMillis = currentMillis; |
| 14 | + | |
| 15 | - | if(ledstate == LOW){
|
| 15 | + | |
| 16 | - | Serial.println("Turning on LED, Lets see if it turns off in 2 seconds");
|
| 16 | + | |
| 17 | - | digitalWrite(led, HIGH); |
| 17 | + | Serial.println("...Program start\n");
|
| 18 | - | ledstate = HIGH; |
| 18 | + | |
| 19 | ||
| 20 | void loop() {
| |
| 21 | - | else if(currentMillis - previousMillis > stoptime) {
|
| 21 | + | currentMillis = millis(); // get current time |
| 22 | ||
| 23 | - | digitalWrite(led, LOW ); |
| 23 | + | digitalWrite(led, ledstate); // always update the pin |
| 24 | ||
| 25 | if((currentMillis - previousMillis >= stoptime) && (timerOn == true)) {
| |
| 26 | // we hit the interval time, stoptime | |
| 27 | Serial.println("Timer has expired, Turning off LED");
| |
| 28 | ledstate = LOW; | |
| 29 | timerOn = false; | |
| 30 | } | |
| 31 | ||
| 32 | if ((ledstate == LOW) && (toogleLED == true)) {
| |
| 33 | Serial.println("Turning on LED, Lets see if it turns off in 2 seconds");
| |
| 34 | ledstate = HIGH; | |
| 35 | previousMillis = currentMillis; // store CURRENT time to compare against | |
| 36 | toogleLED = false; | |
| 37 | timerOn = true; | |
| 38 | } | |
| 39 | } |