If I press Button 3, it works perfectly as coded, but if I switch on the Button 2, the delay sequence switches alternately with the button 3.
How can I code it that button 3's sequence is independent than that of button 2?
/*
source: www.electroschematics.com
You'll need to change the led pins and the codes
accordingly to your configuration and IR remote
*/
#include <IRremote.h>
const int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838
const int led1 = 3; //NAV
const int led2 = 2; //STROBE WING
const int led3 = 4; //STROBE TAIL
const int led4 = 5; //BEACON
boolean itsONled[13]; // Defaults to 'false'
/*old code int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 12495 // code received from button 1
#define code2 6375 // code received from button 2
#define code3 31365 // code received from button 3
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT); //NAV
pinMode(led2, OUTPUT); //STROBE WING
pinMode(led3, OUTPUT); //STROBE TAIL
pinMode(led4, OUTPUT); //BEACON
}
void loop()
{
static unsigned long previousMillis = 0;
// if led2 is 'on', show the blink pattern once per second.
if (itsONled[2] && millis() - previousMillis >= 1000)
{
previousMillis = millis();
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
delay(50);
digitalWrite(led2, LOW);
delay(50);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
delay(50);
digitalWrite(led2, LOW);
}
if (itsONled[4] && millis() - previousMillis >= 1000)
{
previousMillis = millis();
digitalWrite(led4, HIGH);
delay(50);
digitalWrite(led4, LOW);
delay(50);
}
if (irrecv.decode(&results))
{
unsigned int value = results.value;
switch (value)
{
case code1:
if (itsONled[1]) // if first led is on then
{
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = false; // and set its state as off
}
else // else if first led is off
{
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = true; // and set its state as on
}
break;
case code2:
if (itsONled[2])
{
digitalWrite(led2, LOW);
itsONled[2] = false;
}
else
{
itsONled[2] = true;
}
break;
case code3:
if (itsONled[4])
{
digitalWrite(led4, LOW);
itsONled[4] = false;
}
else
{
itsONled[4] = true;
}
break;
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}