#define DECODE_NEC // Includes Apple and Onkyo. To enable all protocols , just comment/disable this line.
#include <Arduino.h>
int IR_RECEIVE_PIN = 12;
/*
* This include defines the actual pin number for pins like IR_RECEIVE_PIN, IR_SEND_PIN for many different boards and architectures
*/
#include <IRremote.hpp> // include the library
const int ledPWM = 3;
int pinLedPWM[ledPWM] = {5, 6, 9};
int pinBuzzerPWM = 3;
const int buttonsNumbers = 10;
int buttonsCommand[buttonsNumbers] = {0x68, 0x30, 0x18, 0x7A, 0x10, 0x38, 0x5A, 0x42, 0x4A, 0x52};
const int buttonsMusic = 5;
int musicCommands[buttonsMusic] = {0xA8, 0x2, 0x98, 0xE0, 0x90}; // Play, Vol +, Vol -, Previous, Next
const int buttonsOthers = 3;
int othersCommands[buttonsOthers] = {0xE2, 0x22, 0xC2}; // Menu, Test and back
int onOffCommand = 0xA2;
bool fadeUp = true;
int fadeConstant = 0;
void setup() {
Serial.begin(115200);
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void allLedOn(int brightness){
for(int pin=0; pin<ledPWM; pin++){
analogWrite(pinLedPWM[pin], brightness);
}
}
void allOff(){
for(int pin=0; pin<ledPWM; pin++){
digitalWrite(pinLedPWM[pin], LOW);
}
digitalWrite(pinBuzzerPWM, LOW);
}
void fadeBrightness(){
for(int jkl=0; jkl<256; jkl++){
for(int led = 0; led< ledPWM; led++){
analogWrite(pinLedPWM[led], jkl);
delay(2);
}
}
for(int jkl=0; jkl<256; jkl++){
for(int led = 0; led< ledPWM; led++){
analogWrite(pinLedPWM[led], 255-jkl);
delay(2);
}
}
}
bool checkButton(int command){
if (IrReceiver.decode()){
IrReceiver.resume();
if(IrReceiver.decodedIRData.command != command){
Serial.println("True");
return true;
}else{
Serial.println("False");
return false;
}
}else{
return false;
}
}
void loop() {
/*
* Check if received data is available and if yes, try to decode it.
* Decoded result is in the IrReceiver.decodedIRData structure.
*
* 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
*/
if (IrReceiver.decode()) {
/*
* Print a summary of received data
*/
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
// We have an unknown protocol here, print extended info
IrReceiver.printIRResultRawFormatted(&Serial, true);
IrReceiver.resume(); // Do it here, to preserve raw data for printing with printIRResultRawFormatted()
} else {
IrReceiver.resume(); // Early enable receiving of the next IR frame
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);
}
Serial.println();
/*
* Finally, check the received data and perform actions according to the received command
*/
if (IrReceiver.decodedIRData.command == onOffCommand){
allOff();
}
else if (IrReceiver.decodedIRData.command == buttonsCommand[0]) {
digitalWrite(5, HIGH);
} else if (IrReceiver.decodedIRData.command == buttonsCommand[1]) {
digitalWrite(6, HIGH);
} else if (IrReceiver.decodedIRData.command == buttonsCommand[2]) {
digitalWrite(9, HIGH);
} else if (IrReceiver.decodedIRData.command == buttonsCommand[3]) {
Serial.println("Buzzer");
} else if (IrReceiver.decodedIRData.command == buttonsCommand[4]) {
Serial.println(checkButton(buttonsCommand[4]));
while(!checkButton(buttonsCommand[4])){
Serial.println("Fade on");
allLedOn(fadeConstant);
delay(10);
if(fadeUp){
fadeConstant++;
}else{
fadeConstant--;
}
if (fadeConstant == 255){
fadeUp = false;
}else if(fadeConstant == 0){
fadeUp = true;
}
}
allOff();
} else if (IrReceiver.decodedIRData.command == buttonsCommand[5]) {
Serial.println("5");
} else if (IrReceiver.decodedIRData.command == buttonsCommand[6]) {
Serial.println("6");
} else if (IrReceiver.decodedIRData.command == buttonsCommand[7]) {
Serial.println("7");
} else if (IrReceiver.decodedIRData.command == buttonsCommand[8]) {
Serial.println("8");
} else if (IrReceiver.decodedIRData.command == buttonsCommand[9]) {
Serial.println("9");
}
}
}