Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* millis() example to use IR buttons to start and stop a slow blinking (turn signal) LED
- james@baldengineer.com
- http://www.baldengineer.com/millis-cookbook.html
- */
- #include <IRremote.h>
- #include <IRremoteInt.h>
- #define irPin 2 //IR receiver on pin 2
- int led1 = 3; //LED that turns on and off using IR remote button.
- int turn = 12; //turn indicator blinker. Should start blinking at 0.7hz after key on remote is pressed
- bool blinking = false; //defines when blinking should occur
- unsigned long blinkInterval = 1420; // number of milliseconds for blink (1/0.7)
- unsigned long currentMillis; // variables to track millis()
- unsigned long previousMillis;
- IRrecv irrecv(irPin);
- decode_results results;
- void setup() {
- Serial.begin(9600);
- irrecv.enableIRIn();
- pinMode(led1, OUTPUT);
- pinMode(turn, OUTPUT);
- }
- void loop() {
- // this code blinks the LED
- if (blinking) {
- currentMillis = millis(); // better to store in variable, for less jitter
- if ((unsigned long)(currentMillis - previousMillis) >= blinkInterval) { // enough time passed yet?
- digitalWrite(turn, !digitalRead(turn)); // shortcut to toggle the LED
- previousMillis = currentMillis; // sets the time we wait "from"
- }
- } else {
- digitalWrite(turn, LOW); // force LED off when not blinking
- }
- if (irrecv.decode(&results)) {
- switch (results.value) {
- case 0x837: //IR remote key to turn LED1 ON
- digitalWrite(led1, HIGH);
- break;
- case 0x37: //IR remote key to turn LED1 OFF
- digitalWrite(led1, LOW);
- break;
- case 0x834: //I want to use this key to start blinking ‘turn’ LED at 0.7hz
- blinking = true;
- break;
- case 0x34: //I want to use this key to stop blinking ‘turn’ LED
- blinking = false;
- break;
- }
- irrecv.resume();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement