The code not work

Dear All,

I am the new user of Arduino and first time to learn the C language, I have a problem of below coding, can help me to review any missing?

When I press button 1, the LED will on, when I press button again, the LED will off, this part is ok.

But when I press button 2, the horse racing LED will turn on one times only then off. My target is when i press button 2, the racing LED will loop to running On until I press the button (or other button) to stop it.

Please help, thank you very much.

#include <IRremote.h> // use the library for IR

const int receiver = A0; // pin 1 of IR receiver to Arduino digital pin 11
const int led1 =  8;
const byte NUM = 5;                     //define how many Airstrip blue leds
const byte Blueled[] = {3, 4, 5, 6, 7}; //5 sets Airstrip blue leds
const int delaytime = 50;
int i;

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

boolean lightState1 = false;
unsigned long last1 = millis();

void setup()

{ 
  pinMode(led1, OUTPUT);
  irrecv.enableIRIn();
    for (i = 0; i < NUM; i++)
  {
    pinMode(Blueled[i], OUTPUT);
    digitalWrite(Blueled[i], LOW);
  }
}

void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value == 0x5468AA42) // remote button 1
    {
      if (millis() - last1 > 300)
      {
      lightState1 =!lightState1;
      digitalWrite(led1,lightState1);
      last1 = millis();
      }
    }

      if (results.value == 0xADC8B4D) // remote button 2
          for (i = 0; i < NUM; i++)
          {
            digitalWrite(Blueled[i], HIGH);
            delay(delaytime);
            digitalWrite(Blueled[i], LOW);
          }
  
     if (results.value == 0x9773272E) //remote button Power
       {
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(Blueled[i], LOW);
       }

irrecv.resume(); 
  }
}

The last digitalWrite uses Blueled [ i ], but 'i' was used elsewhere in a loop.

While the running blue leds are looping, do you want to run other code ? or is it okay to wait for any button to be pressed ?

If you want to use other code, use millis() and a 'state' or sequence index that remembers which led is active. Another variable is needed to indicate that the loop is running.

If it is okay to wait, then use a function that does delay and during that delay checks for buttons.

the use of a “for” loop especially with a delay is going to block the rest of the program

using the same key will require the program to register state changes

im not sure if this will work as I can not compile the program as I don’t have the same ir library. Im not sure the original program compiled as led2 and led3 were not declared

If it does work see if you can follow the code. If not use serial print to work out whats wrong.

#include <IRremote.h> // use the library for IR

const int receiver = A0; // pin 1 of IR receiver to Arduino digital pin 11
const int led1 =  8;
const byte NUM = 5;                     //define how many Airstrip blue leds
const byte Blueled[] = {3, 4, 5, 6, 7}; //5 sets Airstrip blue leds
unsigned long delaytime = 50;
int i;
unsigned long prevMillis = 0;
byte number = 0;
byte runBlueLeds = 0;
byte onetime = 0;
byte prevrunBlueLeds = 0;
IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

boolean lightState1 = false;
unsigned long last1 = millis();

void setup()

{
  pinMode(led1, OUTPUT);
  irrecv.enableIRIn();
  for (i = 0; i < NUM; i++)
  {
    pinMode(Blueled[i], OUTPUT);
    digitalWrite(Blueled[i], LOW);
  }
}

void loop()
{
  unsigned long currentMillis = millis();

  if (irrecv.decode(&results))
  {
    if (results.value == 0x5468AA42) // remote button 1
    {
      if (millis() - last1 > 300)
      {
        lightState1 = !lightState1;
        digitalWrite(led1, lightState1);
        last1 = millis();
      }
    }

    if (results.value == 0xADC8B4D) // remote button 2
    {
      if (prevrunBlueLeds != runBlueLeds) {
        runBlueLeds = !runBlueLeds;
        prevrunBlueLeds = runBlueLeds;
      }
    }

    if (results.value == 0x9773272E) //remote button Power
    {
      digitalWrite(led1, LOW);
      // digitalWrite(led2, LOW);//no idea what this is for
      //  digitalWrite(led3, LOW);//no idea what this is for
      runBlueLeds = 0;
      for (i = 0; i < NUM; i++)//turn all leds off
      {
        digitalWrite(Blueled[i], LOW);
      }
    }

    irrecv.resume();
  }

  if (runBlueLeds == 1) {//if we want leds to chase
    if (currentMillis - prevMillis >= delaytime) {//if time is done
      if (onetime == 1) {//to be done one time
        digitalWrite(Blueled[number], HIGH);//turn one led on based on number
        number++;//add one to number
        if (number > 4) {//5 = zero
          number = 0;
        }
        onetime = 0;//toggle flag onetime
        delaytime = 50;
      } else {
        for (i = 0; i < NUM; i++)//turn all leds off
        {
          digitalWrite(Blueled[i], LOW);
          onetime = 1;//toggle flag onetime
          delaytime = 0;
        }
      }
      prevMillis =  currentMillis;// take new time stamp
    }
  }
}

I get this compiler error maybe I have a different library than you have so I haven’t looked into that error.

C:\Program Files (x86)\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp:5:16: error: ‘TKD2’ was not declared in this scope
int RECV_PIN = TKD2; // the pin the IR receiver is connected to

Dear Koepel and Goop1

Thank you thank you so much.

I am at company cannot try it now, but i will try it tonight at home.

And Koepel, yes, when the blue led running, other button still want to work for other led.

And Gpop1, my IR library is download at https://github.com/z3t0/Arduino-IRremote

you’re picking up the version of the library for that Arduino Robot thing, which had TKD #defined to something.