Programming request

Dear All,

I am the new Adriano user and I don’t know how to write the C langauage, I am trying to learn it from Internet.

I want to create a IR remote to control some LEDs, and target :

Press a button number 5 - a led will power on (this part is ok now)

Press a button number 4 - a set of LED will running from left to right (this part is ok now)

Press a button number 9 - i want the 2 LEDs start to blinking, but cannot now, the result is press one times, it bright and stopped, and press again, it will be power off, so I need to non-stop press and press to manual start the blinking.

Press a button number 1 - trun all LEDs off.

Now, I hope some C professional help to teach me how can fix the button 9? I want to press one time, all LEDs will start blinking until press button 1 to trun all off. I really want you help, thank you very much.

Attached my code in below.

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

const int receiver = 2;            // pin for IR receiver to Arduino digital pin 11 
const int Rled01 =  8; 
const byte NUM = 5; 
const byte Blueled[] = {3, 4, 5, 6, 7}; 
const int delaytime = 80; 
bool Runningledloop = false; 
int i; 

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

decode_results results; 

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

class Flasher 
{ 
    int ledPin; 
    long OnTime; 
    long OffTime; 
    int ledState; 
    unsigned long previousMillis; 
  public: 
    Flasher(int pin, long on, long off) 
    { 
      ledPin = pin; 
      pinMode(ledPin, OUTPUT); 

      OnTime = on; 
      OffTime = off; 

      ledState = LOW; 
      previousMillis = 0; 
    } 

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

      if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) 
      { 
        ledState = LOW; 
        previousMillis = currentMillis; 
        digitalWrite(ledPin, ledState); 
      } 
      else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) 
      { 
        ledState = HIGH;  // turn it on 
        previousMillis = currentMillis; 
        digitalWrite(ledPin, ledState); 
      } 
    } 
}; 

Flasher led1(9, 123, 400); 
Flasher led2(10, 350, 350); 

//////////////////////////////////////////////////////////// 

void setup() 

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

//////////////////////////////////////////////////////////// 

void loop() 
{ 
  
  if (Runningledloop) 
  { 
    if (i > NUM - 1 ) i = 0; 
    digitalWrite(Blueled[i], HIGH); 
    delay(delaytime); 
    digitalWrite(Blueled[i], LOW); 
    i++; 
  } 

//////////////////////////////////////////////////////////// 
  
  if (irrecv.decode(&results)) 
  { 

//////////////////////////////////////////////////////////// 
  
    if (results.value == 0xC1AA827D) // || 0xFF6897) // remote button 5 
    { 
      if (millis() - last1 > 300) 
      { 
        lightState1 = !lightState1; 
        digitalWrite(Rled01, lightState1); 
        last1 = millis(); 
      } 
    } 

//////////////////////////////////////////////////////////// 

    if (results.value == 0xC1AAF10E) // || 0xFF18E7)  remote button 9 
    {   
    led1.Update(); 
    led2.Update(); 
    } 

//////////////////////////////////////////////////////////// 

    if (results.value == 0xC1AAC23D) // ||0xFF7A85)  remote button 4 
      Runningledloop = true; 

//////////////////////////////////////////////////////////// 

    if (results.value == 0xC1AAFC03) //remote button 1  || 0xFF02FD 
    { 
      digitalWrite(Rled01, LOW); 
      digitalWrite(Blueled[i], LOW); 
      Runningledloop = false; 
    } 

//////////////////////////////////////////////////////////// 

    irrecv.resume(); 
  } 
}

you need to introduce a new global variable, so the code becomes something like:

bool flashingLEDS = false;

loop()
{
....
    if (flashingLEDS)
    {
        led1.Update();
        led2.Update();
    }
...
    if (decodeIR)
    {
...
       if (results.value == 0xC1AAF10E) // || 0xFF18E7)  remote button 9
       {   
          flashingLEDS = true;
       }
...
    }
}

don't forget to set flashingLEDS = false when you press other buttons.

Simplify your program to see what is happening. Do you know that “9” is being received? Do you know that it is entering the flash part of your program?

You can send comments to the serial terminal. EG, "9 detected ", “in flash routine” etc. this will confirm the program is getting to a particular point.

Weedpharma

weedpharma:
Simplify your program to see what is happening. Do you know that "9" is being received? Do you know that it is entering the flash part of your program?

You can send comments to the serial terminal. EG, "9 detected ", "in flash routine" etc. this will confirm the program is getting to a particular point.

Weedpharma

Dear weedpharma

I never try in serial terminal, let me open it to try but not sure what can I do, let me think first, thanks

arduinodlb:
you need to introduce a new global variable, so the code becomes something like:

bool flashingLEDS = false;

loop()
{

    if (flashingLEDS)
    {
        led1.Update();
        led2.Update();
    }

    if (decodeIR)
    {

      if (results.value == 0xC1AAF10E) // || 0xFF18E7)  remote button 9
      { 
          flashingLEDS = true;
      }

    }
}




don't forget to set flashingLEDS = false when you press other buttons.

Dear, thanks for your recommendation, let me Change my code to see what is result first