RGB LED Strip- Using millis to switch between two colours every x ms.

Hi there all

I’m having fun with millis…!

I’ve got two functions, one that shows a blue colour on an LED strip, and one that shows a green colour.
I’m trying to use millis to swap between the two, but I am a bit unsure where to start.

I’m my many code interations, I can get both colours to show, but one of them is for a brief moment only, the other is solid, so the timing isn’t even.

Anyone got any pointers…?

This is my most recent piece of code.

I think I’m missing something!

void flash() {

unsigned long currentMillis = millis();
              
 if(currentMillis - previousMillis1 > 500) {
 
 
  previousMillis1 = millis(); 
  flashCounter++;

 
  
 } 

 if (flashCounter < 1) {

  colourBlue();
 }

 if (flashCounter > 0 && flashCounter < 2) {

  colourGreen();
  flashCounter = 0;
 }

you can try something like this:

typedef struct LedColor{
  uint8_t red;
  uint8_t green;
  uint8_t blue;
};

LedColor blue = {0, 0, 255};
LedColor green = {0, 255, 0};

void setup()
{
  //
}

void loop()
{
  flashLed(red, blue);
}

void flashLed(struct LedColor colorA, struct LedColor colorB)
{
   static unsigned long lastMillis = 0;
   static bool toggle = false;
   if(millis() - lastMillis > 1000UL)
   {
     if(toggle)
     {
       //show colorA
     }
     else
     {
       //show colorB
     }
     toggle = !toggle;
     lastMillis = millis();
   }
}
void loop(){
unsigned long currentMillis = millis();
 if(currentMillis - previousMillis1 > 500) {
  previousMillis1 = millis(); 
  color = 1 - color; // 1 - 0 - 1 - 0 ...
if (color == 0){
colourBlue();
}
else{
colourGreen();
 } 
}

Without seeing colourBlue(), colourGreen(), or how flash() is called (or even if it is called), it is hard to help.

I can see that, in the first 500 milliseconds, inside flash(), if the variable flashCounter ever makes it to one thus calling colourGreen()), flashCounter gets set back to zero (thus calling colourBlue()). I do not think that is your intention.