baldengineer

Turn Signal millis() blink example

Apr 30th, 2015
330
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* millis() example to use IR buttons to start and stop a slow blinking (turn signal) LED
  2. james@baldengineer.com
  3. http://www.baldengineer.com/millis-cookbook.html
  4. */
  5.  
  6. #include <IRremote.h>
  7. #include <IRremoteInt.h>
  8. #define irPin 2 //IR receiver on pin 2
  9.  
  10.  
  11. int led1 = 3; //LED that turns on and off using IR remote button.
  12. int turn = 12; //turn indicator blinker. Should start blinking at 0.7hz after key on remote is pressed
  13.  
  14. bool blinking = false;  //defines when blinking should occur
  15. unsigned long blinkInterval = 1420;  // number of milliseconds for blink (1/0.7)
  16.  
  17. unsigned long currentMillis; // variables to track millis()
  18. unsigned long previousMillis;
  19.  
  20. IRrecv irrecv(irPin);
  21. decode_results results;
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   irrecv.enableIRIn();
  26.   pinMode(led1, OUTPUT);
  27.   pinMode(turn, OUTPUT);
  28. }
  29. void loop() {
  30.  
  31.   // this code blinks the LED
  32.   if (blinking) {    
  33.     currentMillis = millis();  // better to store in variable, for less jitter
  34.     if ((unsigned long)(currentMillis - previousMillis) >= blinkInterval) {  // enough time passed yet?
  35.       digitalWrite(turn, !digitalRead(turn));  // shortcut to toggle the LED
  36.       previousMillis = currentMillis;  // sets the time we wait "from"
  37.     }
  38.   } else {
  39.     digitalWrite(turn, LOW); // force LED off when not blinking
  40.   }
  41.  
  42.   if (irrecv.decode(&results)) {
  43.     switch (results.value) {
  44.  
  45.       case 0x837: //IR remote key to turn LED1 ON
  46.         digitalWrite(led1, HIGH);
  47.         break;
  48.  
  49.       case 0x37: //IR remote key to turn LED1 OFF
  50.         digitalWrite(led1, LOW);
  51.         break;
  52.  
  53.       case 0x834: //I want to use this key to start blinking ‘turn’ LED at 0.7hz
  54.         blinking = true;
  55.         break;
  56.  
  57.       case 0x34: //I want to use this key to stop blinking ‘turn’ LED
  58.         blinking = false;
  59.         break;
  60.  
  61.     }
  62.     irrecv.resume();
  63.   }
  64. }
RAW Paste Data