Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Lilnija_29012017 NucleoF042K6_IRReceiver
examples/IRrelay/IRrelay.ino@0:70c8e56bac45, 2016-01-23 (annotated)
- Committer:
- yuhki50
- Date:
- Sat Jan 23 06:16:48 2016 +0000
- Revision:
- 0:70c8e56bac45
import https://github.com/z3t0/Arduino-IRremote e3ec11d
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yuhki50 | 0:70c8e56bac45 | 1 | /* |
| yuhki50 | 0:70c8e56bac45 | 2 | * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv |
| yuhki50 | 0:70c8e56bac45 | 3 | * An IR detector/demodulator must be connected to the input RECV_PIN. |
| yuhki50 | 0:70c8e56bac45 | 4 | * Version 0.1 July, 2009 |
| yuhki50 | 0:70c8e56bac45 | 5 | * Copyright 2009 Ken Shirriff |
| yuhki50 | 0:70c8e56bac45 | 6 | * http://arcfn.com |
| yuhki50 | 0:70c8e56bac45 | 7 | */ |
| yuhki50 | 0:70c8e56bac45 | 8 | |
| yuhki50 | 0:70c8e56bac45 | 9 | #include <IRremote.h> |
| yuhki50 | 0:70c8e56bac45 | 10 | |
| yuhki50 | 0:70c8e56bac45 | 11 | int RECV_PIN = 11; |
| yuhki50 | 0:70c8e56bac45 | 12 | int RELAY_PIN = 4; |
| yuhki50 | 0:70c8e56bac45 | 13 | |
| yuhki50 | 0:70c8e56bac45 | 14 | IRrecv irrecv(RECV_PIN); |
| yuhki50 | 0:70c8e56bac45 | 15 | decode_results results; |
| yuhki50 | 0:70c8e56bac45 | 16 | |
| yuhki50 | 0:70c8e56bac45 | 17 | // Dumps out the decode_results structure. |
| yuhki50 | 0:70c8e56bac45 | 18 | // Call this after IRrecv::decode() |
| yuhki50 | 0:70c8e56bac45 | 19 | // void * to work around compiler issue |
| yuhki50 | 0:70c8e56bac45 | 20 | //void dump(void *v) { |
| yuhki50 | 0:70c8e56bac45 | 21 | // decode_results *results = (decode_results *)v |
| yuhki50 | 0:70c8e56bac45 | 22 | void dump(decode_results *results) { |
| yuhki50 | 0:70c8e56bac45 | 23 | int count = results->rawlen; |
| yuhki50 | 0:70c8e56bac45 | 24 | if (results->decode_type == UNKNOWN) { |
| yuhki50 | 0:70c8e56bac45 | 25 | Serial.println("Could not decode message"); |
| yuhki50 | 0:70c8e56bac45 | 26 | } |
| yuhki50 | 0:70c8e56bac45 | 27 | else { |
| yuhki50 | 0:70c8e56bac45 | 28 | if (results->decode_type == NEC) { |
| yuhki50 | 0:70c8e56bac45 | 29 | Serial.print("Decoded NEC: "); |
| yuhki50 | 0:70c8e56bac45 | 30 | } |
| yuhki50 | 0:70c8e56bac45 | 31 | else if (results->decode_type == SONY) { |
| yuhki50 | 0:70c8e56bac45 | 32 | Serial.print("Decoded SONY: "); |
| yuhki50 | 0:70c8e56bac45 | 33 | } |
| yuhki50 | 0:70c8e56bac45 | 34 | else if (results->decode_type == RC5) { |
| yuhki50 | 0:70c8e56bac45 | 35 | Serial.print("Decoded RC5: "); |
| yuhki50 | 0:70c8e56bac45 | 36 | } |
| yuhki50 | 0:70c8e56bac45 | 37 | else if (results->decode_type == RC6) { |
| yuhki50 | 0:70c8e56bac45 | 38 | Serial.print("Decoded RC6: "); |
| yuhki50 | 0:70c8e56bac45 | 39 | } |
| yuhki50 | 0:70c8e56bac45 | 40 | Serial.print(results->value, HEX); |
| yuhki50 | 0:70c8e56bac45 | 41 | Serial.print(" ("); |
| yuhki50 | 0:70c8e56bac45 | 42 | Serial.print(results->bits, DEC); |
| yuhki50 | 0:70c8e56bac45 | 43 | Serial.println(" bits)"); |
| yuhki50 | 0:70c8e56bac45 | 44 | } |
| yuhki50 | 0:70c8e56bac45 | 45 | Serial.print("Raw ("); |
| yuhki50 | 0:70c8e56bac45 | 46 | Serial.print(count, DEC); |
| yuhki50 | 0:70c8e56bac45 | 47 | Serial.print("): "); |
| yuhki50 | 0:70c8e56bac45 | 48 | |
| yuhki50 | 0:70c8e56bac45 | 49 | for (int i = 0; i < count; i++) { |
| yuhki50 | 0:70c8e56bac45 | 50 | if ((i % 2) == 1) { |
| yuhki50 | 0:70c8e56bac45 | 51 | Serial.print(results->rawbuf[i]*USECPERTICK, DEC); |
| yuhki50 | 0:70c8e56bac45 | 52 | } |
| yuhki50 | 0:70c8e56bac45 | 53 | else { |
| yuhki50 | 0:70c8e56bac45 | 54 | Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); |
| yuhki50 | 0:70c8e56bac45 | 55 | } |
| yuhki50 | 0:70c8e56bac45 | 56 | Serial.print(" "); |
| yuhki50 | 0:70c8e56bac45 | 57 | } |
| yuhki50 | 0:70c8e56bac45 | 58 | Serial.println(""); |
| yuhki50 | 0:70c8e56bac45 | 59 | } |
| yuhki50 | 0:70c8e56bac45 | 60 | |
| yuhki50 | 0:70c8e56bac45 | 61 | void setup() |
| yuhki50 | 0:70c8e56bac45 | 62 | { |
| yuhki50 | 0:70c8e56bac45 | 63 | pinMode(RELAY_PIN, OUTPUT); |
| yuhki50 | 0:70c8e56bac45 | 64 | pinMode(13, OUTPUT); |
| yuhki50 | 0:70c8e56bac45 | 65 | Serial.begin(9600); |
| yuhki50 | 0:70c8e56bac45 | 66 | irrecv.enableIRIn(); // Start the receiver |
| yuhki50 | 0:70c8e56bac45 | 67 | } |
| yuhki50 | 0:70c8e56bac45 | 68 | |
| yuhki50 | 0:70c8e56bac45 | 69 | int on = 0; |
| yuhki50 | 0:70c8e56bac45 | 70 | unsigned long last = millis(); |
| yuhki50 | 0:70c8e56bac45 | 71 | |
| yuhki50 | 0:70c8e56bac45 | 72 | void loop() { |
| yuhki50 | 0:70c8e56bac45 | 73 | if (irrecv.decode(&results)) { |
| yuhki50 | 0:70c8e56bac45 | 74 | // If it's been at least 1/4 second since the last |
| yuhki50 | 0:70c8e56bac45 | 75 | // IR received, toggle the relay |
| yuhki50 | 0:70c8e56bac45 | 76 | if (millis() - last > 250) { |
| yuhki50 | 0:70c8e56bac45 | 77 | on = !on; |
| yuhki50 | 0:70c8e56bac45 | 78 | digitalWrite(RELAY_PIN, on ? HIGH : LOW); |
| yuhki50 | 0:70c8e56bac45 | 79 | digitalWrite(13, on ? HIGH : LOW); |
| yuhki50 | 0:70c8e56bac45 | 80 | dump(&results); |
| yuhki50 | 0:70c8e56bac45 | 81 | } |
| yuhki50 | 0:70c8e56bac45 | 82 | last = millis(); |
| yuhki50 | 0:70c8e56bac45 | 83 | irrecv.resume(); // Receive the next value |
| yuhki50 | 0:70c8e56bac45 | 84 | } |
| yuhki50 | 0:70c8e56bac45 | 85 | } |