Advertisement
baldengineer

millis LED 2 second example

Apr 19th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unsigned long previousMillis = 0;  
  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.   Serial.begin(9600); // Debug will be disabled in final versions
  14.   pinMode(led, OUTPUT);
  15.   digitalWrite(led, LOW);
  16.   delay(5000);
  17.   Serial.println("...Program start\n");
  18. }
  19.  
  20. void loop() {
  21.   currentMillis = millis();  // get current time
  22.  
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement