Arduino | tsop1738 |
+5V | Vs |
GND | GND |
11 | OUT |
/* * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv * An IR detector/demodulator must be connected to the input RECV_PIN. * Version 0.1 July, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> int RECV_PIN = 11; int RELAY_PIN = 4; IRrecv irrecv(RECV_PIN); decode_results results; // Dumps out the decode_results structure. // Call this after IRrecv::decode() // void * to work around compiler issue //void dump(void *v) { // decode_results *results = (decode_results *)v void dump(decode_results *results) { int count = results->rawlen; if (results->decode_type == UNKNOWN) { Serial.println("Could not decode message"); } else { if (results->decode_type == NEC) { Serial.print("Decoded NEC: "); } else if (results->decode_type == SONY) { Serial.print("Decoded SONY: "); } else if (results->decode_type == RC5) { Serial.print("Decoded RC5: "); } else if (results->decode_type == RC6) { Serial.print("Decoded RC6: "); } Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): "); for (int i = 0; i < count; i++) { if ((i % 2) == 1) { Serial.print(results->rawbuf[i]*USECPERTICK, DEC); } else { Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); } Serial.print(" "); } Serial.println(""); } void setup() { pinMode(RELAY_PIN, OUTPUT); pinMode(13, OUTPUT); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } int on = 0; unsigned long last = millis(); void loop() { if (irrecv.decode(&results)) { // If it's been at least 1/4 second since the last // IR received, toggle the relay if (millis() - last > 250) { on = !on; digitalWrite(RELAY_PIN, on ? HIGH : LOW); digitalWrite(13, on ? HIGH : LOW); dump(&results); } last = millis(); irrecv.resume(); // Receive the next value } }
โค้ดเวอร์ชั่น 2 ใช้งานจริงโรงรถที่บ้าน
/* * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv * An IR detector/demodulator must be connected to the input RECV_PIN. * Version 0.1 July, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> int RECV_PIN = 11; int RELAY_PIN = 4; int on = 0; IRrecv irrecv(RECV_PIN); decode_results results; // Dumps out the decode_results structure. // Call this after IRrecv::decode() // void * to work around compiler issue //void dump(void *v) { // decode_results *results = (decode_results *)v void dump(decode_results *results) { int count = results->rawlen; long value1 = results->value; if (results->decode_type == UNKNOWN) { Serial.println("Could not decode message"); } else { if (results->decode_type == NEC) { Serial.print("Decoded NEC: "); // Serial.println(results->value); if((value1==183887895)||(value1==3249146358)||(value1==3777251511)){ on = !on; digitalWrite(RELAY_PIN, on ? HIGH : LOW); digitalWrite(13, on ? HIGH : LOW); Serial.println(on); } } else if (results->decode_type == SONY) { Serial.print("Decoded SONY: "); // Serial.println(results->value); if(value1==2704){ on = !on; digitalWrite(RELAY_PIN, on ? HIGH : LOW); digitalWrite(13, on ? HIGH : LOW); Serial.println(on); } } else if (results->decode_type == RC5) { Serial.print("Decoded RC5: "); } else if (results->decode_type == RC6) { Serial.print("Decoded RC6: "); } // Serial.println(results->value); Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): "); for (int i = 0; i < count; i++) { if ((i % 2) == 1) { Serial.print(results->rawbuf[i]*USECPERTICK, DEC); } else { Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); } Serial.print(" "); } Serial.println(""); } void setup() { pinMode(RELAY_PIN, OUTPUT); pinMode(13, OUTPUT); Serial.begin(9600); digitalWrite(RELAY_PIN,HIGH); digitalWrite(13,HIGH); irrecv.enableIRIn(); // Start the receiver } unsigned long last = millis(); void loop() { if (irrecv.decode(&results)) { // If it's been at least 1/4 second since the last // IR received, toggle the relay if (millis() - last > 250) { // on = !on; // digitalWrite(RELAY_PIN, on ? HIGH : LOW); // digitalWrite(13, on ? HIGH : LOW); dump(&results); } last = millis(); irrecv.resume(); // Receive the next value } }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น