/* * IRtraffic - control a walk / don't walk sign. * * An IR detector/demodulator must be connected to the input 11. * Relays must be connected to outputs 3 and 4. * * This uses the IRremote library: http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html * Copyright 2010 Ken Shirriff * http://arcfn.com */ #include #define RECV_PIN 11 #define WALK_PIN 3 #define DONT_PIN 4 #if 0 #define OFF_CODE 0x1CB92 // Stop on Sony DVD remote #define WALK_CODE 0xB92 // 1 on Sony DVD remote #define BLINK_CODE 0x80b92 // 2 on Sony DVD remote #define DONT_CODE 0x40b92 // 3 on Sony DVD remote #define CYCLE_CODE 0x4CB92 // Play on Sony DVD remote #else #define OFF_CODE 0x20df10ef #define WALK_CODE 0xa10cd00f #define BLINK_CODE 0xa10c8807 #define DONT_CODE 0xa10cc807 #define CYCLE_CODE 0xa10c6c03 #endif #define MODE_OFF 0 #define MODE_WALK 1 #define MODE_BLINK 2 #define MODE_DONT 3 #include "WProgram.h" void setup(); void loop(); IRrecv irrecv(RECV_PIN); decode_results results; int mode = MODE_OFF; boolean cycle = true; // auto-cycle mode // For timing, see http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay long nextCycleMillis = 0; // time for changing states long nextBlinkMillis = 0; // time for blinking don't walk boolean blinkOn = false; void setup() { pinMode(WALK_PIN, OUTPUT); pinMode(DONT_PIN, OUTPUT); irrecv.enableIRIn(); // Start the receiver Serial.begin(9600); } void loop() { // Process the IR input, if any if (irrecv.decode(&results)) { if (results.value == WALK_CODE) { Serial.write("Walk\n"); mode = MODE_WALK; cycle = false; } else if (results.value == DONT_CODE) { Serial.write("Don't walk\n"); mode = MODE_DONT; cycle = false; } else if (results.value == OFF_CODE) { Serial.write("Off\n"); mode = MODE_OFF; cycle = false; } else if (results.value == BLINK_CODE) { Serial.write("Blinking don't walk\n"); mode = MODE_BLINK; blinkOn = true; nextBlinkMillis = millis() + 500; // 500 ms blink cycle = false; } else if (results.value == CYCLE_CODE) { Serial.write("Cycle\n"); nextCycleMillis = millis() + 5000; // delay 5 seconds cycle = true; mode = MODE_WALK; } else { Serial.print("unexpected value: "); Serial.println(results.value, HEX); } irrecv.resume(); // Resume decoding (necessary!) } // If cycling, advance to the next mode if appropriate if (cycle && millis() >= nextCycleMillis) { if (mode == MODE_WALK) { mode = MODE_BLINK; blinkOn = false; nextBlinkMillis = millis() + 500; // 500 ms blink nextCycleMillis = millis() + 5000; // delay 5 seconds } else if (mode == MODE_BLINK) { mode = MODE_DONT; nextCycleMillis = millis() + 5000; // delay 5 seconds } else { mode = MODE_WALK; nextCycleMillis = millis() + 5000; // delay 5 seconds } } // If blinking, flip blink flag if appropriate if (mode == MODE_BLINK && millis() >= nextBlinkMillis) { blinkOn = !blinkOn; nextBlinkMillis = millis() + 500; // 500 ms blink } // Set the outputs according to the mode if (mode == MODE_WALK) { digitalWrite(DONT_PIN, LOW); digitalWrite(WALK_PIN, HIGH); } else if (mode == MODE_DONT || (mode == MODE_BLINK && blinkOn)) { digitalWrite(DONT_PIN, HIGH); digitalWrite(WALK_PIN, LOW); } else { digitalWrite(DONT_PIN, LOW); digitalWrite(WALK_PIN, LOW); } }