Arduino-IRremote/examples/ReceiveDemo/ReceiveDemo.ino

206 lines
9.0 KiB
Arduino
Raw Normal View History

2010-01-23 14:08:26 +08:00
/*
* ReceiveDemo.cpp
2020-12-31 11:51:54 +08:00
*
2022-04-27 01:13:03 +08:00
* Demonstrates receiving IR codes with the IRremote library and the use of the Arduino tone() function with this library.
2021-03-09 08:31:31 +08:00
* If debug button is pressed (pin connected to ground) a long output is generated.
2020-12-31 11:51:54 +08:00
*
2021-03-03 02:59:41 +08:00
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
2020-12-31 11:51:54 +08:00
*
2021-03-06 08:21:46 +08:00
************************************************************************************
* MIT License
*
2022-01-29 17:44:14 +08:00
* Copyright (c) 2020-2022 Armin Joachimsmeyer
2021-03-06 08:21:46 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************************
2010-01-23 14:08:26 +08:00
*/
2021-04-19 20:06:29 +08:00
2021-03-06 08:21:46 +08:00
#include <Arduino.h>
2010-01-23 14:08:26 +08:00
/*
* Specify which protocol(s) should be used for decoding.
* If no protocol is defined, all protocols are active.
2021-10-18 01:29:20 +08:00
* This must be done before the #include <IRremote.hpp>
*/
2021-05-02 04:24:04 +08:00
//#define DECODE_LG
//#define DECODE_NEC
//#define DECODE_DISTANCE
// etc. see IRremote.hpp
2021-03-09 08:31:31 +08:00
//
2022-08-17 16:54:56 +08:00
#if RAMEND <= 0x4FF || (defined(RAMSIZE) && RAMSIZE < 0x4FF)
#define RAW_BUFFER_LENGTH 150 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100.
2022-08-17 16:54:56 +08:00
#define EXCLUDE_EXOTIC_PROTOCOLS // saves around 650 bytes program memory if all other protocols are active
#elif RAMEND <= 0x8FF || (defined(RAMSIZE) && RAMSIZE < 0x8FF)
#define RAW_BUFFER_LENGTH 600 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100.
#else
#define RAW_BUFFER_LENGTH 750 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100.
#endif
2022-04-05 10:47:32 +08:00
//#define NO_LED_FEEDBACK_CODE // saves 92 bytes program memory
//#define EXCLUDE_UNIVERSAL_PROTOCOLS // Saves up to 1000 bytes program memory.
//#define EXCLUDE_EXOTIC_PROTOCOLS // saves around 650 bytes program memory if all other protocols are active
2021-02-08 22:53:03 +08:00
// MARK_EXCESS_MICROS is subtracted from all marks and added to all spaces before decoding,
// to compensate for the signal forming of different IR receiver modules.
//#define MARK_EXCESS_MICROS 20 // 20 is recommended for the cheap VS1838 modules
//#define RECORD_GAP_MICROS 12000 // Activate it for some LG air conditioner protocols
//#define DEBUG // Activate this for lots of lovely debug output from the decoders.
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
2021-10-18 01:29:20 +08:00
#include <IRremote.hpp>
2010-01-23 14:08:26 +08:00
#if defined(APPLICATION_PIN)
2021-03-09 08:31:31 +08:00
#define DEBUG_BUTTON_PIN APPLICATION_PIN // if low, print timing for each received data set
#else
#define DEBUG_BUTTON_PIN 6
#endif
2020-05-29 06:24:53 +08:00
void setup() {
2022-04-05 10:47:32 +08:00
#if FLASHEND >= 0x3FFF // For 16k flash or more, like ATtiny1604. Code does not fit in program memory of ATtiny85 etc.
pinMode(DEBUG_BUTTON_PIN, INPUT_PULLUP);
#endif
2020-07-02 02:46:29 +08:00
Serial.begin(115200);
2022-04-05 10:47:32 +08:00
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/|| defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217)
2021-04-08 07:51:03 +08:00
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
2020-05-29 06:24:53 +08:00
#endif
// Just to know which program is running on my Arduino
2021-01-02 22:24:51 +08:00
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
2020-05-29 06:24:53 +08:00
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println(F("Enabling IRin..."));
2022-01-29 17:44:14 +08:00
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
2021-01-30 07:50:58 +08:00
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial);
2022-04-05 10:47:32 +08:00
Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
2022-04-05 10:47:32 +08:00
#if FLASHEND >= 0x3FFF // For 16k flash or more, like ATtiny1604. Code does not fit in program memory of ATtiny85 etc.
2021-03-09 08:31:31 +08:00
Serial.print(F("Debug button pin is "));
Serial.println(DEBUG_BUTTON_PIN);
// infos for receive
Serial.print(RECORD_GAP_MICROS);
Serial.println(F(" us is the (minimum) gap, after which the start of a new IR packet is assumed"));
Serial.print(MARK_EXCESS_MICROS);
Serial.println(F(" us are subtracted from all marks and added to all spaces for decoding"));
2021-06-16 07:02:18 +08:00
#endif
2010-01-23 14:08:26 +08:00
}
void loop() {
2020-12-31 11:51:54 +08:00
/*
* Check if received data is available and if yes, try to decode it.
* Decoded result is in the IrReceiver.decodedIRData structure.
2021-01-30 07:50:58 +08:00
*
* E.g. command is in IrReceiver.decodedIRData.command
* address is in command is in IrReceiver.decodedIRData.address
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
2020-12-31 11:51:54 +08:00
*/
if (IrReceiver.decode()) {
2021-04-13 03:41:07 +08:00
Serial.println();
2021-05-02 04:24:04 +08:00
#if FLASHEND >= 0x3FFF // For 16k flash or more, like ATtiny1604
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) {
Serial.println(F("Overflow detected"));
Serial.println(F("Try to increase the \"RAW_BUFFER_LENGTH\" value of " STR(RAW_BUFFER_LENGTH) " in " __FILE__));
// see also https://github.com/Arduino-IRremote/Arduino-IRremote#compile-options--macros-for-this-library
2021-10-18 01:10:40 +08:00
# if !defined(ESP8266) && !defined(NRF5)
/*
* do double beep
*/
2021-10-18 01:10:40 +08:00
# if !defined(ESP32)
IrReceiver.stop(); // ESP32 uses another timer for tone()
# endif
2021-03-06 08:21:46 +08:00
tone(TONE_PIN, 1100, 10);
delay(50);
tone(TONE_PIN, 1100, 10);
delay(50);
2021-10-18 01:10:40 +08:00
# if !defined(ESP32)
IrReceiver.start(100000); // to compensate for 100 ms stop of receiver. This enables a correct gap measurement.
2021-10-18 01:10:40 +08:00
# endif
# endif
} else {
// Print a short summary of received data
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN || digitalRead(DEBUG_BUTTON_PIN) == LOW) {
// We have an unknown protocol, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
2020-12-31 11:51:54 +08:00
}
2021-01-04 11:54:03 +08:00
2021-10-24 18:38:35 +08:00
// tone on esp8266 works once, then it disables the successful IrReceiver.start() / timerConfigForReceive().
# if !defined(ESP8266) && !defined(NRF5)
if (IrReceiver.decodedIRData.protocol != UNKNOWN && digitalRead(DEBUG_BUTTON_PIN) != LOW) {
/*
* If no debug mode or a valid protocol was received, play tone, wait and restore IR timer.
* Otherwise do not play a tone to get exact gap time between transmissions and not running into repeat frames while wait for tone to end.
2021-08-08 03:25:44 +08:00
* This will give the next CheckForRecordGapsMicros() call a chance to eventually propose a change of the current RECORD_GAP_MICROS value.
*/
2021-10-18 01:10:40 +08:00
# if !defined(ESP32)
IrReceiver.stop(); // ESP32 uses another timer for tone()
# endif
tone(TONE_PIN, 2200, 8);
# if !defined(ESP32)
delay(8);
IrReceiver.start(8000); // to compensate for 8 ms stop of receiver. This enables a correct gap measurement.
2021-10-18 01:10:40 +08:00
# endif
}
# endif
2021-05-02 04:24:04 +08:00
#else
// Print a minimal summary of received data
IrReceiver.printIRResultMinimal(&Serial);
#endif // FLASHEND
2021-01-30 07:50:58 +08:00
/*
* !!!Important!!! Enable receiving of the next value,
* since receiving has stopped after the end of the current received data packet.
*/
IrReceiver.resume();
2020-12-31 11:51:54 +08:00
/*
2021-01-30 07:50:58 +08:00
* Finally check the received data and perform actions according to the received address and commands
2020-12-31 11:51:54 +08:00
*/
if (IrReceiver.decodedIRData.address == 0) {
if (IrReceiver.decodedIRData.command == 0x10) {
// do something
} else if (IrReceiver.decodedIRData.command == 0x11) {
// do something else
}
2020-12-31 11:51:54 +08:00
}
} // if (IrReceiver.decode())
/*
* Your code here
* For all users of the FastLed library, use this code for strip.show() to improve receiving performance (which is still not 100%):
* if (IrReceiver.isIdle()) {
* strip.show();
* }
*/
}