All hardware, but the Arduino UNO I am using, I got from https://www.casadarobotica.com/[](https://www.casadarobotica.com/kit-s-didaticos/outros/kit-infravermelho-controle-remoto-receptor-led-infravermelho)
I am using a control to turn the LED on and off using any button — if the LED is on and a button is pressed, the LED is turned off; if the LED is off and a button is pressed, the LED is turned on.
Sometimes after some buttons are pressed, usually when two or more buttons are presses simultaneously, the variable stateReceiver is always true and the results.value is always the same. When it happens, I disconnect the infrared receiver and the cables connecting to it, since the receiver's quality is questionable, but stateReceiver still is always true, so the LED is blinking every 0.1 second, even without the receiver.
#include <IRremote.h>
// Requirements
int ledPin = 3; // Led' pin
int receiverPin = 2; // Receiver's pin
// System
bool stateLED = false; // If the LED's on or off
bool stateReceiver = false; // if the receiver is on or off
IRrecv irrecv(receiverPin);
decode_results results;
void setup() {
irrecv.enableIRIn(); // Turns the sensor on
Serial.begin(9600); // Begins a serial connection
pinMode(ledPin, OUTPUT); // Sets the LED's pin for output
}
void loop() {
stateReceiver = irrecv.decode(&results); // If the receiver has received anything
delay(100); // Waits 0.1 second to each signal durates 0.1 second. TEMPORARY
Serial.println(results.value, HEX);
if (stateReceiver) { // If the receiver is on
if (stateLED) { // If the LED is on, it is turned off
digitalWrite(ledPin, LOW);
stateLED = false;
}
else { // If the receiver is off, it is turned on
digitalWrite(ledPin, HIGH);
stateLED = true;
}
irrecv.resume(); // Waits for the next message
}
}