Blinking RGB LED without delay

Hello everyone, I have a doubt about how can I blink a RGB LED but using millis()
for example, I want to do the following:

int pinRed = 3;
int pinGrn = 5;
int pinBlu = 6;

void setup() {
  pinMode(pinRed, OUTPUT);
  pinMode(pinGrn, OUTPUT);
  pinMode(pinBlu, OUTPUT);
}

void loop() {
  redColor();
  delay(1000);
  greenColor();
  delay(1000);
  blueColor();
  delay(1000);
}

void redColor(){
  digitalWrite(pinRed, LOW);
  digitalWrite(pinGrn, HIGH);
  digitalWrite(pinBlu, HIGH);
}

void greenColor(){
  digitalWrite(pinGrn, LOW);
  digitalWrite(pinRed, HIGH);
  digitalWrite(pinBlu, HIGH);
}

void blueColor(){
  digitalWrite(pinBlu, LOW);
  digitalWrite(pinGrn, HIGH);
  digitalWrite(pinRed, HIGH);
}

...but using millis();

I have this code

int pinRed = 3;
int pinGrn = 5;
int pinBlu = 6;

unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;

//Function Prototype
void redColor();
void greenColor();
void blueColor();
void flash(unsigned long);

void setup() {
  pinMode(pinRed, OUTPUT);
  pinMode(pinGrn, OUTPUT);
  pinMode(pinBlu, OUTPUT);
}

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

void redColor(){
  digitalWrite(pinRed, LOW);
  digitalWrite(pinGrn, HIGH);
  digitalWrite(pinBlu, HIGH);
}

void greenColor(){
  digitalWrite(pinGrn, LOW);
  digitalWrite(pinRed, HIGH);
  digitalWrite(pinBlu, HIGH);
}

void blueColor(){
  digitalWrite(pinBlu, LOW);
  digitalWrite(pinGrn, HIGH);
  digitalWrite(pinRed, HIGH);
}

void flash(unsigned long thisMillis){
  
  if (((thisMillis - previousMillis) >= 1000)){
    previousMillis = thisMillis;
    redColor();
  }
  if ((thisMillis - previousMillis1) >= 2000){
    previousMillis1 = thisMillis;
    greenColor();
  }
  if ((thisMillis - previousMillis2) >= 3000){
    previousMillis2 = thisMillis;
    blueColor();
  }
}

but it does not work as expected.
At the begining the sequence is:
red, green blue, green, red, blue, red, green, blue, green, red, blue...
I would really appreciate if someone can guide me

The millis code has too many timers..
You got 3, 1 for each of the leds..
Thus they can be controlled separately..

Really if you want the millis to work like the blocking delay code..

you need 1 millis timer, the first with 1000 a second..
then a simple byte var to keep track of which led you blink..

could do a switch case, then, ++ your var, reset var after blue..


byte CurrentColor = 0;
void flash(unsigned long thisMillis) {

  if (((thisMillis - previousMillis) >= 1000)) {
    previousMillis = thisMillis;

    switch (CurrentColor) {
      case 0: redColor(); CurrentColor++; break;
      case 1: greenColor(); CurrentColor++; break;
      case 2: blueColor(); CurrentColor = 0; break;
    }
  }
}

have fun.. ~q

2 Likes

It works!!!
You are the BEST!

Thank you very much! I'm very grateful

1 Like

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