byte relayPin[] = {0, 1, 2, 3, 4};
byte relays = sizeof(relayPin) / sizeof(relayPin[0]);
byte relay;
byte irPin = 5;
void setup() {
pinMode(irPin, INPUT);
for (int i = 0; i < relays; i++)
pinMode(relayPin[i], OUTPUT);
}
void loop() {
long key = getIrKey();
switch (key) {
case 12488: relay = 0; break; // button 0
case 6368: relay = 1; break; // button 1
case 31358: relay = 2; break; // button 2
case 4328: relay = 3; break; // button 3
case 14528: relay = 4; break; // button 4
case 20408: // button "C" for clear
for (int i = 0; i < relays; i++) {
if (digitalRead(relayPin[i])) // if the pin is high
digitalWrite(relayPin[i], LOW); // make it low
}
key = 0; // do not toggle the pin
break;
default: key = 0; break; // do not toggle a pin if no key pressed
}
if (key) {
digitalWrite(relayPin[relay], !digitalRead(relayPin[relay]));
}
}
int getIrKey() {
int key = 0, temp;
int len = pulseIn(irPin, LOW);
if (len > 3500) {
for (int i = 1; i <= 32; i++) {
temp = pulseIn(irPin, HIGH);
if (temp > 1000)
key = key + (1 << (i - 17));
}
}
if (key < 0 )
key = -key;
return key;
}
/*
+-- --+
PCINT5/-RESET/ADC0/dW/PB5 |1 * 8| VCC
PCINT3/XTAL1/CLKI/-OC1B/ADC3/PB3 |2 7| PB2/SCK/USCK/SCL/ADC1/T0/INT0/PCINT2
PCINT4/XTAL2/CLKO/OC1B/ADC2/PB4 |3 6| PB1/MISO/DO/AIN1/OC0B/OC1A/PCINT1
GND |4 5| PB0/MOSI/DI/SDA/AIN0/OC0A/-OC1A/AREF/PCINT0
+-----+
*/