Rgb blinkg with millis?

Hello! I'm new to the forum (been looking for tutorials since I don't remember),
I'm working with rgb leds (this type Screenshot by Lightshot) and I'm trying to make them blink using millis. The general idea is when it blinks, blink with another color, it could be a random color or with a secuence (red, green,blue) But I don't know how to adress this problem.
It worked with delay, but when I switch mode with a button, it needs to wait to the loop to end.
The other problem I have is that I need to control more than 4 rgb leds at once, with differents secuences, but with the pwn I can only use 2 leds, what can I do?

This is the code working with delay...

int buttonPin1=2;
int redPin1=11;
int greenPin1=10;
int bluPin1=9;
int buttonState=0;

void setup()
{ 
  pinMode(buttonPin1, INPUT);
  pinMode(redPin1, OUTPUT);
  pinMode(greenPin1, OUTPUT);
  pinMode(bluPin1, OUTPUT);  
}
void loop()
{
  buttonState=digitalRead(buttonPin1);
   if (buttonState == LOW)
  {
    setColor1(255, 0, 0);  // red
    delay(1000);
    setColor1(0, 255, 0);  // green
    delay(1000);
    setColor1(0, 0, 255);  // blue
    delay(1000);
    setColor1(255, 255, 0);  // yellow
    delay(1000);  
    setColor1(80, 0, 80);  // purple
    delay(1000);
    setColor1(0, 255, 255);  // aqua
    delay(1000);
    setColor1(75, 0, 130);  // indigo
    delay(5000);
    setColor1(255, 255, 255); //blancoo
    delay(2500);
  }
  if (buttonState == HIGH)
  {
    setColor1(255, 0, 0);  // red
    delay(100);
    setColor1(0, 255, 0);  // green
    delay(100);
    setColor1(0, 0, 255);  // blue
    delay(100);
  }
}
void setColor1(int red1,int green1,int blu1)
{
  analogWrite(redPin1,red1);
  analogWrite(greenPin1,green1);
  analogWrite(bluPin1,blu1);
  
}

If you only want red, green and blue, plus yellow, cyan, magenta and white, then you do not need to use pwm pins, you can use any digital pins. You only need pwm pins if you want to mix more than those 7 colours.

If you want lots of RGB leds, and mix many colours, use ws2812b LEDs.

Post your code where you attempted to use millis(). We will help you fix it.

The problem is that I don't understand how to use it with a RGB led, the example only uses two states, LOW and HIGH with the millis, so how to adress it with a lot of colors?

int redPin =11;
int greenPin=10;
int bluPin=9;
int buttonPin=2;
int buttonState=0;
unsigned long previousMillis=0;
unsigned long interval= 1000;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluPin,OUTPUT);
}

void loop()
{
  buttonState=digitalRead(buttonPin);
  unsigned long currentMillis = millis();
  if (buttonState ==LOW)
  {
    ?????????????????????????????????????????????????
  }
  
}

There is where I don't know what to do. I understand the "if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;" and how it works, but how to make it work for various states?
I'm sorry for being a very hard learner :cry: :cry:

And about the rgb leds I didn't know about the ws2812b, so I will look, but for now this is all I have,, so I guess I'll be working with just two leds.

I am finding it quite difficult to understand your questions. I appreciate that English is not your first language.

I think you are asking how to have button presses select one of many modes, and each mode has a different flashing/colour sequence. This is one of the most common questions asked on this section of the forum, and there are many example solutions if you perform a search. Try searching for "finite state machine", "button debouncing" and "blink without delay".

Volicius:
The problem is that I don't understand how to use it with a RGB led, the example only uses two states, LOW and HIGH with the millis, so how to adress it with a lot of colors?

You're looking at it wrong. Don't worry about what the Blink Without Delay example does, just focus on the fact that it does something every so often. You can have that something be anything you want. Call a function there that decides what color your LED should be and sets it to that color.

Yesss, after a little, I did the blink :stuck_out_tongue:

int redPin =11;
int greenPin=10;
int bluPin=9;
int buttonPin=2;
int buttonState=0;
unsigned long previousMillis=0;
unsigned long interval1= 1000;
unsigned long interval2= 3000;
unsigned long interval3= 5000;
unsigned long interval4= 7000;
unsigned long interval5= 9000;
unsigned long interval6= 11000;
unsigned long vuelta=12000;
long int fade_goTime;
int fade_break = 100;

void setColor (int red,int green,int blu)
{ 
 
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluPin, blu);  
}

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(bluPin,OUTPUT);
  pinMode(buttonPin,INPUT);
  fade_goTime = millis();
}

void loop()
{
  buttonState=digitalRead(buttonPin);
  if (buttonState ==0)
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis == interval1) 
   { 
    setColor( 255, 0, 0); //red
   }
   if (currentMillis - previousMillis == interval2) 
   { 
    setColor( 0, 255, 0); //green
   }
    if (currentMillis - previousMillis == interval3) 
   { 
    setColor( 0, 0, 255); //blue
    }
    if (currentMillis - previousMillis == interval4) 
   { 
    setColor( 255, 255, 0); //yellow
    }
    if (currentMillis - previousMillis == interval5) 
   { 
    setColor( 0, 255, 255); //aqua
    }
    if (currentMillis - previousMillis == interval6) 
   { 
    setColor( 255, 0, 255); //purple
    
   }
    if (currentMillis-previousMillis==vuelta)
    {
      previousMillis=currentMillis;  
    }
  }
   else
  {
   ////////////////FADE
      if(  millis() >= fade_goTime) 
{
  static unsigned int rgbColour[3] = {255,0,0}; //Start on red
  static int incColour = 1;
  static int decColour = 0;
  static int i = -1;
     
      // cross-fade the two colours.
      i++;
      if(i > 255) {
        i=0;
        decColour ++;
        if(decColour >2) decColour = 0;     
        if (decColour == 2)  incColour = 0;
        else incColour = decColour +1;
      }

       
        rgbColour[decColour] -= 1;
        rgbColour[incColour] += 1;
       
        setColor(rgbColour[0], rgbColour[1], rgbColour[2]);     
       fade_goTime = millis() + fade_break;       
}
   
  }
}

The problem I'm facing now is with the fade, I just copied a fade code I found here and pasted it, did some tweaks and put it at work, the problem is when I push the button to change, it starts to fade but blinks once in a while :S What could be the problem now?

Thank you for the help :stuck_out_tongue:

Also, they may be another way to do the blinking code shorter and easier, but I can't think how.

currentMillis - previousMillis == interval1

never use == in these situations, use >= or <= depending on what’s required.

 I just copied a fade code I found here and pasted it, did some tweaks and put it at work,

Your problem is that this sentence doesn't include, "figured out how it works".

That fade code is not well written. Its more complicated than it needs to be. I suspect the flashes are caused by the fade levels hitting -1, which is interpreted by analogWrite() as 255.

After this line

        setColor(rgbColour[0], rgbColour[1], rgbColour[2]);

print the 3 colour values to the serial monitor. Then you can see if any of them are ever -1.

Or try this:

   ////////////////FADE
   if(  millis() >= fade_goTime) 
   {
    static int i = 0;
     
    if (i < 256) setColor(255 - i, i, 0); // red -> yellow -> green
    else if (i < 512) setColor(0, 511 - i, i - 256); // green -> cyan -> blue
    else setColor(i - 512, 0, 767 - i); // blue -> magenta -> red

    if (i++ > 767) i = 0;

    fade_goTime = millis() + fade_break;       
    }
  }
}