#include #include // Necessary IR Globals int RECV_PIN = 10; IRrecv irrecv(RECV_PIN); IRsend irsend; decode_results results; uint8_t message[8]; int index = 0; boolean com = false; int irReceive = 0; void setup() { Serial.begin(9600); } void loop() { if (irReceive > millis() && irrecv.decode(&results)) { irRead(&results); irrecv.resume(); } else { while(Serial.available()) { uint8_t x = Serial.read(); if(index > 0 || (x >= 80 && x <= 95)) { message[index++] = x; if(index > 7){ decodeMessage(); index = 0; } } } } } void decodeMessage() { int cmd = message[0]; int pin = (message[1] - 97 < 0 || message[1] - 97 > 19)? -1 : message[1] - 97 ; if(com) { uint8_t val[6]; memcpy(val, message + 2, 6); switch (cmd) { //case 80: break; case 81: setMode(val[0], pin); break; case 82: digitalW(val[0], pin); break; case 83: digitalR(pin); break; //case 84: break; //case 85: break; //case 86: break; case 87: sendRCTristate(val, pin); break; //case 88: break; case 89: irrecv.enableIRIn(); irReceive = millis() + 10000; break; default: break; } } else if(cmd == 90) { com = true; setMode(0, pin); digitalWrite(pin, HIGH); Serial.println("Ready!"); } else { Serial.println("Arduino not configured for communication!"); } } void setMode(uint8_t val, int pin) { if(pin == -1) { Serial.println("badpin"); return; } if (val == 0) { pinMode(pin, OUTPUT); } else { pinMode(pin, INPUT); } } void digitalW(uint8_t val, int pin) { if(pin == -1) { Serial.println("badpin"); return; } pinMode(pin, OUTPUT); if(val == 0) { digitalWrite(pin, LOW); } else { digitalWrite(pin, HIGH); } } void digitalR(int pin) { if(pin == -1) { Serial.println("badpin"); return; } pinMode(pin, INPUT); int digraw = digitalRead(pin); Serial.println(String(pin + '::' + digraw)); } void sendRCTristate (uint8_t val[], int pin) { if(pin == -1) { Serial.println("badpin"); return; } String triStateCode = ""; RCSwitch rc = RCSwitch(); rc.enableTransmit(pin); for(int i = 0; i < 6; i++) { String triStatePart = String(val[i], HEX); triStatePart = (triStatePart.length() < 2) ? String("0" + triStatePart) : triStatePart; triStateCode.concat(triStatePart); } char triState[triStateCode.length() + 1]; triStateCode.toUpperCase(); triStateCode.toCharArray(triState, triStateCode.length() + 1); rc.sendTriState(triState); delay(500); Serial.print("RC-Tristate send: "); Serial.println(triState); } void irRead(decode_results *results) { int codeType = results->decode_type; if (codeType == UNKNOWN) { Serial.println("Unknown IR Protocol!"); } else { if (codeType == NEC) { if (results->value == REPEAT) { return; } } unsigned long codeValue = results->value; int codeLen = results->bits; char m[22]; sprintf(m, "%04d::%04d::%09lu", codeType,codeLen,codeValue); Serial.println(m); } }