Advertisement
computermuseo

Untitled

Oct 5th, 2016
1,878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <IRremote.h>
  2.  
  3. int RECV_PIN = 11;
  4. int RELAY_PIN = 4;
  5.  
  6. IRrecv irrecv(RECV_PIN);
  7. decode_results results;
  8.  
  9. // Dumps out the decode_results structure.
  10. // Call this after IRrecv::decode()
  11. // void * to work around compiler issue
  12. //void dump(void *v) {
  13. // decode_results *results = (decode_results *)v
  14. void dump(decode_results *results) {
  15. int count = results->rawlen;
  16. if (results->decode_type == UNKNOWN) {
  17. Serial.println("Could not decode message");
  18. }
  19. else {
  20. if (results->decode_type == NEC) {
  21. Serial.print("Decoded NEC: ");
  22. }
  23. else if (results->decode_type == SONY) {
  24. Serial.print("Decoded SONY: ");
  25. }
  26. else if (results->decode_type == RC5) {
  27. Serial.print("Decoded RC5: ");
  28. }
  29. else if (results->decode_type == RC6) {
  30. Serial.print("Decoded RC6: ");
  31. }
  32. Serial.print(results->value, HEX);
  33. Serial.print(" (");
  34. Serial.print(results->bits, DEC);
  35. Serial.println(" bits)");
  36. }
  37. Serial.print("Raw (");
  38. Serial.print(count, DEC);
  39. Serial.print("): ");
  40.  
  41. for (int i = 0; i < count; i++) {
  42. if ((i % 2) == 1) {
  43. Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  44. }
  45. else {
  46. Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  47. }
  48. Serial.print(" ");
  49. }
  50. Serial.println("");
  51. }
  52.  
  53. void setup()
  54. {
  55. pinMode(RELAY_PIN, OUTPUT);
  56. pinMode(13, OUTPUT);
  57. Serial.begin(9600);
  58. irrecv.enableIRIn(); // Start the receiver
  59. }
  60.  
  61. int on = 0;
  62. unsigned long last = millis();
  63.  
  64. void loop() {
  65. if (irrecv.decode(&results)) {
  66. // If it's been at least 1/4 second since the last
  67. // IR received, toggle the relay
  68. if (millis() - last > 250) {
  69. on = !on;
  70. digitalWrite(RELAY_PIN, on ? HIGH : LOW);
  71. digitalWrite(13, on ? HIGH : LOW);
  72. dump(&results);
  73. }
  74. last = millis();
  75. irrecv.resume(); // Receive the next value
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement