/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
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.
===============================================
*/
#include <IRremote.hpp>
/*
Pin mapping table for different platforms
Platform IR input IR output Tone Core/Pin schema
--------------------------------------------------------------
DEFAULT/AVR 2 3 4 Arduino
ATtinyX5 0|PB0 4|PB4 3|PB3 ATTinyCore
ATtiny167 3|PA3 2|PA2 7|PA7 ATTinyCore
ATtiny167 9|PA3 8|PA2 5|PA7 Digispark original core
ATtiny84 |PB2 |PA4 |PA3 ATTinyCore
ATtiny88 3|PD3 4|PD4 9|PB1 ATTinyCore
ATtiny3217 18|PA1 19|PA2 20|PA3 MegaTinyCore
ATtiny1604 2 3|PA5 %
ATtiny816 14|PA1 16|PA3 1|PA5 MegaTinyCore
ATtiny1614 8|PA1 10|PA3 1|PA5 MegaTinyCore
SAMD21 3 4 5
ESP8266 14|D5 12|D6 %
ESP32 15 4 27
ESP32-C3 6 7
BluePill PA6 PA7 PA3
APOLLO3 11 12 5
RP2040 3|GPIO15 4|GPIO16 5|GPIO17
*/
#define IR_RECEIVE_PIN 2
void setup() {
Serial.begin(115200); Serial.println();
IrReceiver.begin(IR_RECEIVE_PIN);
}
void loop() {
if (IrReceiver.decode()) {
// got a new signal
// IrReceiver.decodedIRData is a structure you can access to read what got received
// you can see the structure at https://github.com/Arduino-IRremote/Arduino-IRremote/blob/c6c7f17f587eabab0833fe0d048dba85251a9735/src/IRProtocol.h#L107-L130
// it looks something like this (https://github.com/Arduino-IRremote/Arduino-IRremote?tab=readme-ov-file#receiving-ir-codes)
/*
struct IRData {
decode_type_t protocol; // UNKNOWN, NEC, SONY, RC5, PULSE_DISTANCE, ...
uint16_t address; // Decoded address
uint16_t command; // Decoded command
uint16_t extra; // Used for Kaseikyo unknown vendor ID. Ticks used for decoding Distance protocol.
uint16_t numberOfBits; // Number of bits received for data (address + command + parity) - to determine protocol length if different length are possible.
uint8_t flags; // IRDATA_FLAGS_IS_REPEAT, IRDATA_FLAGS_WAS_OVERFLOW etc. See IRDATA_FLAGS_* definitions
IRRawDataType decodedRawData; // Up to 32 (64 bit for 32 bit CPU architectures) bit decoded raw data, used for sendRaw functions.
uint32_t decodedRawDataArray[RAW_DATA_ARRAY_SIZE]; // 32 bit decoded raw data, to be used for send function.
irparams_struct *rawDataPtr; // Pointer of the raw timing data to be decoded. Mainly the data buffer filled by receiving ISR.
};
*/
// the most useful attribute of the structure is the Decoded command, it's an uint16_t you access with IrReceiver.decodedIRData.command
// for the wokwi remote control, commands looks like this
switch (IrReceiver.decodedIRData.command) {
case 162: Serial.println("POWER"); break;
case 226: Serial.println("MENU"); break;
case 34: Serial.println("TEST"); break;
case 2: Serial.println("PLUS"); break;
case 194: Serial.println("BACK"); break;
case 224: Serial.println("PREV."); break;
case 168: Serial.println("PLAY"); break;
case 144: Serial.println("NEXT"); break;
case 104: Serial.println("num: 0"); break;
case 152: Serial.println("MINUS"); break;
case 176: Serial.println("key: C"); break;
case 48: Serial.println("num: 1"); break;
case 24: Serial.println("num: 2"); break;
case 122: Serial.println("num: 3"); break;
case 16: Serial.println("num: 4"); break;
case 56: Serial.println("num: 5"); break;
case 90: Serial.println("num: 6"); break;
case 66: Serial.println("num: 7"); break;
case 74: Serial.println("num: 8"); break;
case 82: Serial.println("num: 9"); break;
default:
Serial.print(IrReceiver.decodedIRData.command);
Serial.println("=> unknown button");
}
// Prepare for the next IR frame
IrReceiver.resume();
}
}