├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── Adafruit_NECremote.cpp ├── Adafruit_NECremote.h ├── README.txt └── examples └── listener └── listener.pde /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /Adafruit_NECremote.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is an example for the Adafruit NEC Remote Control 3 | 4 | Designed specifically to work with the Adafruit NEC Remote Control 5 | ----> http://www.adafruit.com/products/389 6 | and IR Receiver Sensor 7 | ----> http://www.adafruit.com/products/157 8 | 9 | These devices use IR to communicate, 1 pin is required to 10 | interface 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit and open-source hardware by purchasing 13 | products from Adafruit! 14 | 15 | Written by Limor Fried/Ladyada for Adafruit Industries. 16 | BSD license, all text above must be included in any redistribution 17 | ****************************************************/ 18 | 19 | #include "Adafruit_NECremote.h" 20 | #include "pins_arduino.h" 21 | #include "wiring_private.h" 22 | 23 | 24 | Adafruit_NECremote::Adafruit_NECremote(uint8_t pin) { 25 | _irpin = pin; 26 | _irpinport = digitalPinToPort(_irpin); 27 | _irpinmask = digitalPinToBitMask(_irpin); 28 | 29 | timedout = false; 30 | timing = false; 31 | maxwaiting = 0; 32 | } 33 | 34 | int16_t Adafruit_NECremote::listen(int16_t maxwaitseconds) { 35 | uint16_t p1, p2, timer; 36 | 37 | p1 = p2 = 0; 38 | timer = 0; 39 | maxwaiting = maxwaitseconds; 40 | maxwaiting *= 1000; // to milliseconds 41 | maxwaiting *= 1000; // to microseconds 42 | 43 | if (maxwaitseconds) timing = true; 44 | timedout = false; 45 | 46 | // wait for starting pulses 47 | while ( ((p1 < 7000/RESOLUTION) || (p1 > 10000/RESOLUTION)) && 48 | ((p2 < 3000/RESOLUTION) || (p2 > 5000/RESOLUTION))) { 49 | p1 = measurePulse(0); 50 | if (timing) { 51 | maxwaiting -= p1; 52 | if (maxwaiting < 0) { 53 | timedout = true; // timed out waiting 54 | } 55 | } 56 | if (timedout) return -1; 57 | 58 | p2 = measurePulse(1); 59 | if (timing) { 60 | maxwaiting -= p2; 61 | if (maxwaiting < 0) { 62 | timedout = true; // timed out waiting 63 | } 64 | } 65 | if (timedout) return -1; 66 | //Serial.println(p1); 67 | //Serial.println(p2); 68 | } 69 | 70 | //Serial.println("NEC"); 71 | // get 'data' 72 | uint8_t data[4] = {0, 0, 0, 0}; 73 | int8_t b; 74 | for (uint8_t i=0; i<32; i++) { 75 | b = readNECbit(); 76 | if (timedout) { 77 | return -1; 78 | } 79 | if (b < 0) { 80 | if (i == 0) 81 | return -3; // repeat! 82 | else 83 | return -1; 84 | } 85 | data[i/8] |= b << (i%8); 86 | } 87 | /* 88 | Serial.print(data[0], HEX); Serial.print(" "); 89 | Serial.print(data[1], HEX); Serial.print(" "); 90 | Serial.print(data[2], HEX); Serial.print(" "); 91 | Serial.println(data[3], HEX); 92 | */ 93 | 94 | if ((data[0] == (~data[1] & 0xFF)) && (data[2] == (~data[3] & 0xFF))) 95 | return data[2]; 96 | 97 | if ((data[0] == 0) && (data[1] == 0xBF) && (data[2] == (~data[3] & 0xFF))) 98 | return data[2]; 99 | 100 | return -2; 101 | } 102 | 103 | 104 | uint16_t Adafruit_NECremote::measurePulse(boolean state) { 105 | uint16_t pulse = 0; 106 | uint8_t statemask; 107 | 108 | if (state) { 109 | statemask = _irpinmask; 110 | } else { 111 | statemask = 0; 112 | } 113 | 114 | volatile uint8_t *_irpinreg = portInputRegister(_irpinport); 115 | // wait for the state to change 116 | // while (((IRpin_PIN >> IRpin) & 0x1) != state) { 117 | while ((*_irpinreg & _irpinmask) != statemask) { 118 | delayMicroseconds(RESOLUTION); 119 | if (timing) { 120 | maxwaiting -= RESOLUTION; 121 | if (maxwaiting < 0) { 122 | timedout = true; 123 | return 0; 124 | } 125 | } 126 | } 127 | 128 | // in the proper state, keep track 129 | while ((*_irpinreg & _irpinmask) == statemask) { 130 | delayMicroseconds(RESOLUTION); 131 | pulse++; 132 | if (timing) { 133 | maxwaiting -= RESOLUTION; 134 | if (maxwaiting < 0) { 135 | timedout = true; 136 | return 0; 137 | } 138 | } 139 | } 140 | return pulse; 141 | } 142 | 143 | 144 | uint8_t Adafruit_NECremote::readNECbit(void) { 145 | int16_t p1, p2; 146 | 147 | p1 = measurePulse(0); 148 | if (timedout) return -1; 149 | p2 = measurePulse(1); 150 | if (timedout) return -1; 151 | 152 | if ((p1 < 400/RESOLUTION) || (p1 > 700/RESOLUTION)) 153 | return -1; 154 | if ((p2 > 400/RESOLUTION) && (p2 < 700/RESOLUTION)) { 155 | return 0; // valid bit 156 | } 157 | if ((p2 > 1400/RESOLUTION) && (p2 < 1800/RESOLUTION)) { 158 | return 1; // valid bit 159 | } 160 | return -1; 161 | } 162 | -------------------------------------------------------------------------------- /Adafruit_NECremote.h: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is an example for the Adafruit NEC Remote Control 3 | 4 | Designed specifically to work with the Adafruit NEC Remote Control 5 | ----> http://www.adafruit.com/products/389 6 | and IR Receiver Sensor 7 | ----> http://www.adafruit.com/products/157 8 | 9 | These devices use IR to communicate, 1 pin is required to 10 | interface 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit and open-source hardware by purchasing 13 | products from Adafruit! 14 | 15 | Written by Limor Fried/Ladyada for Adafruit Industries. 16 | BSD license, all text above must be included in any redistribution 17 | ****************************************************/ 18 | 19 | #if ARDUINO >= 100 20 | #include "Arduino.h" 21 | #else 22 | #include "WProgram.h" 23 | #endif 24 | 25 | // what our timing resolution should be, larger is better 26 | // as its more 'precise' - but too large and you wont get 27 | // accurate timing 28 | #define RESOLUTION 20 29 | 30 | class Adafruit_NECremote { 31 | public: 32 | Adafruit_NECremote(uint8_t pin); 33 | int16_t listen(int16_t maxwaitseconds = 0); 34 | 35 | private: 36 | uint8_t _irpin, _irpinport, _irpinmask; 37 | 38 | boolean timedout, timing; 39 | int32_t maxwaiting; 40 | 41 | uint16_t measurePulse(boolean state); 42 | uint8_t readNECbit(); 43 | }; 44 | 45 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | This is a library for the Adafruit NEC Remote Control 2 | 3 | Designed specifically to work with the Adafruit NEC Remote Control 4 | ----> http://www.adafruit.com/products/389 5 | and IR Receiver Sensor 6 | ----> http://www.adafruit.com/products/157 7 | 8 | These devices use IR to communicate, 1 pin is required to 9 | interface 10 | Adafruit invests time and resources providing this open source code, 11 | please support Adafruit and open-source hardware by purchasing 12 | products from Adafruit! 13 | 14 | Written by Limor Fried/Ladyada for Adafruit Industries. 15 | BSD license, all text above must be included in any redistribution 16 | 17 | To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_NECremote. Check that the Adafruit_NECremote folder contains Adafruit_NECremote.cpp and Adafruit_NECremote.h 18 | 19 | Place the Adafruit_NECremote library folder your /libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. -------------------------------------------------------------------------------- /examples/listener/listener.pde: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is an example for the Adafruit NEC Remote Control 3 | 4 | Designed specifically to work with the Adafruit NEC Remote Control 5 | ----> http://www.adafruit.com/products/389 6 | and IR Receiver Sensor 7 | ----> http://www.adafruit.com/products/157 8 | 9 | These devices use IR to communicate, 1 pin is required to 10 | interface 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit and open-source hardware by purchasing 13 | products from Adafruit! 14 | 15 | Written by Limor Fried/Ladyada for Adafruit Industries. 16 | BSD license, all text above must be included in any redistribution 17 | ****************************************************/ 18 | 19 | 20 | #include "Adafruit_NECremote.h" 21 | 22 | // Connect a 38KHz remote control sensor to the pin below 23 | #define IRpin 4 24 | 25 | Adafruit_NECremote remote(IRpin); 26 | 27 | void setup(void) { 28 | Serial.begin(115200); 29 | Serial.println("Ready to decode IR!"); 30 | } 31 | 32 | int lastcode = -1; 33 | 34 | void loop(void) { 35 | // You can set the listen() time out to 'n' seconds 36 | int c = remote.listen(5); // seconds to wait before timing out! 37 | // Or you can wait 'forever' for a valid code 38 | //int c = remote.listen(); // Without a #, it means wait forever 39 | 40 | if (c >= 0) { 41 | Serial.print("Received code #"); 42 | Serial.println(c, DEC); 43 | lastcode = c; 44 | } else if (c == -3) { 45 | Serial.print("Repeat ("); 46 | Serial.print(lastcode); 47 | Serial.println(")"); 48 | } else if (c == -2) { 49 | Serial.println("Data error"); 50 | } else { 51 | Serial.println("Timed out waiting!"); 52 | } 53 | } --------------------------------------------------------------------------------