IR Control and Independent Strobe Patterns

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
  }
}

Maybe rewrite this section:

  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);
  }

so it more closely resembles that of the button 3 below which you say works:

 if (itsONled[4] && millis() - previousMillis >= 1000)
  {
    previousMillis = millis();

    digitalWrite(led4, HIGH);
    delay(50);
    digitalWrite(led4, LOW);
    delay(50);
     }

Button 2 code is fine as it is, so is Button 3 code.

The problem is that if I switch both of them on at the same time, Button 2 code runs first then alternately Button 3 code runs, then back to Button 2.

What I wanted to happen is that if I switch both Button 2 and Button 3, their strobe patterns run independently rather than pattern of Button 2 runs then proceeds to pattern of Button 3 then back to Button 2 pattern. BTW Button 2 has 1 LED so does Button 3.

What I want to happen:
clicks Button 2 Led goes on off on off etc.
clicks Button 3 Led goes on off on off etc.

What actually happens:
clicks Button 2 Led goes on off on off etc.
clicks Button 3 runs Button 2's code for 1 sec then Led goes on off on off etc.

OK. That is clear now.
It is not so easy because you are using delay() which is just about OK if only one pattern is to run at any one time. If your are running sequences simultaneously, you have to start using millis() instead of delay() and have a timer variable to control each led. Look at the "Blink Without Delay" example sketch which comes with Arduino IDE.

Any tips on how to time a strobe pattern with millis? It seems like it's way different and not as simple as using delay function.

I've been reading that Blink Without Delay from Arduino but I can't get my head around on how to do it with multiple timings. I can only fathom the part where it gets a 1 second 1 blink pattern.

The first thing to do is write a clear description of the pattern you require from each of the 4 leds.
This could be done as a time line which milliseconds on the X axis and ON/OFF on the Y axis.
You will need 4 timer variables which will determine the time remaining until the leds are next switched. You test these against the current value of millis() and switch as required.

This thread bears a remarkable similarity to another current thread.

Still figuring out the codes, and yes I may have posted one before, I'm getting the hang of the proper way to categorize my post so apologies if I may have double posted. :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.