Hello, I am trying to do a project where I use an infrared transmitter to turn on a relay. I used this code that lights up LEDs when a certain infrared code is received and what I did was instead of an led I put the output on the signal pin of a relay. I am using an Arduino UNO for the project. The first thing that u are supposed to do is upload the code and press different buttons on the remote that correspond to a code that appears on the serial monitor. and write down and use those numbers in the code. The only problem is when I press the buttons for the transmitter on the serial monitor two different sets of numbers appear and even if I only press the same button sometimes the number will be different. So technically it somewhat works but I want it to be more consistent with where it shows the same number every time, and I have tried 3 different infrared transmitters.
here is the code I used...
Any suggestion as to why I'm getting different and inconsistent number every time? Thanks.
#include <IRremote.h>
#define first_key 65535m
#define second_key 58359
#define third_key 539
#define fourth_key 25979
int receiver_pin = 8;
int first_led_pin = 7;
int second_led_pin = 6;
int third_led_pin = 5;
int fourth_led_pin = 4;
int led[] = {0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);
pinMode(fourth_led_pin, OUTPUT);
}
void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1] == 1) {
digitalWrite(first_led_pin, LOW);
led[1] = 0;
} else {
digitalWrite(first_led_pin, HIGH);
led[1] = 1;
}
break;
case second_key:
if(led[2] == 1) {
digitalWrite(second_led_pin, LOW);
led[2] = 0;
} else {
digitalWrite(second_led_pin, HIGH);
led[2] = 1;
}
break;
case third_key:
if(led[3] == 1) {
digitalWrite(third_led_pin, LOW);
led[3] = 0;
} else {
digitalWrite(third_led_pin, HIGH);
led[3] = 1;
}
break;
case fourth_key:
if(led[4] == 1) {
digitalWrite(fourth_led_pin, LOW);
led[4] = 0;
} else {
digitalWrite(fourth_led_pin, HIGH);
led[4] = 1;
}
break;
}
Serial.println(value);
receiver.resume();
}
}