Leds Blinking Millis

How do I make two or more leds blink one at a time? For instance, red blinks first then green blinks, blue blinks, and finally it loops back to red, and continues the cycle again. This is for case 2

int redled = A3;
int greenled = 5;
int blueled = 8;
int purpleled1 = 9;
int purpleled2 = 10;
int yellowled1 = 12;
int yellowled2 = A1;
int button = A4;
int buttonstate;
int toggle = 0;
unsigned long previousMillis = 0; 
unsigned long previousMillis2 = 0; 
const long interval = 1000; 
const long interval2 = 3000;
int LDR = 0;
int Light;
long OnTime1 = 1000;
long OffTime1 = 10000;
long OnTime2 = 1000;
long OffTime2 = 1000;
int redledstate = LOW;
int greenledstate = LOW;

void setup() {                

  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(blueled, OUTPUT);
  pinMode(purpleled1, OUTPUT);
  pinMode(purpleled2, OUTPUT);
  pinMode(yellowled1, OUTPUT);
  pinMode(yellowled2, OUTPUT);
  pinMode(LDR, INPUT);
  pinMode(button, INPUT);
  
  
  Serial.begin(9600);
}


void loop() {
	unsigned long currentMillis = millis();
	Light = analogRead(LDR);
	Serial.println(Light);
	
	buttonstate = digitalRead(button);
	
		if (buttonstate == HIGH)
	{
	delay(200);
	toggle = toggle + 1;
	}
	else if (toggle > 6)
	{
		delay(150);
		toggle = 0;
		
	}
	else{}
	
	
	switch (toggle) {

	
    case 0:    // Off
    Serial.println("Off");
	{
		digitalWrite(redled, LOW);   
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	break;
	    case 2:  
	{	 
		unsigned long currentMillis = millis();

 

 if((redledstate == HIGH) && (currentMillis - previousMillis >= interval))
	{
    
    previousMillis = currentMillis;
    Serial.println(previousMillis);
  	redledstate = LOW;
		digitalWrite(redled, LOW);   
	}
else if ((redledstate == LOW) && (currentMillis - previousMillis >= interval) )
  {
  		previousMillis = currentMillis;
  		redledstate = HIGH;
  		digitalWrite(redled, HIGH); 
  } 
  	

	
else if ((greenledstate == HIGH) && (currentMillis - previousMillis2 >= interval2))
	{
    // save the last time you blinked the LED 
    previousMillis2 = currentMillis;
	greenledstate = LOW;

	digitalWrite(greenled, LOW);

	}
else if((greenledstate == LOW) && (currentMillis - previousMillis2 >= interval2))
	{
	
	    previousMillis2 = currentMillis;
	    greenledstate = HIGH;

	    digitalWrite(greenled, HIGH);
	    
	
	
	}
 }
}

	break;
	
		    case 3:    // Green
    Serial.println("Green");
	{
		digitalWrite(redled, LOW);  
  		digitalWrite(greenled, HIGH);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	break;

		    case 4:    // Blue
    Serial.println("Blue");
	{
		digitalWrite(redled, LOW);  
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, HIGH);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	break;	
	

	case 5:
	Serial.println("Light");
	{	
	if (buttonstate == HIGH)
	{
		digitalWrite(redled, HIGH);   
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	
	else if (Light > 700)
	{
		digitalWrite(redled, LOW);   
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}

	else if (Light > 650 && Light < 700)
	{
		digitalWrite(redled, HIGH);  
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	
		else if (Light > 600 && Light < 650)
	{
		digitalWrite(redled, LOW);  
  		digitalWrite(greenled, HIGH);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	
		else if (Light > 550 && Light < 600)
	{
		digitalWrite(redled, LOW); 
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, HIGH);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	
		else if (Light > 500 && Light < 550)
	{
		digitalWrite(redled, LOW);  
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, HIGH);
  		digitalWrite(purpleled2, HIGH);
  		digitalWrite(yellowled1, LOW);
  		digitalWrite(yellowled2, LOW);
	}
	
		else if (Light > 450 && Light < 500)
	{
		digitalWrite(redled, LOW);   // turn the LED on (HIGH is the voltage level)
  		digitalWrite(greenled, LOW);
  		digitalWrite(blueled, LOW);
  		digitalWrite(purpleled1, LOW);
  		digitalWrite(purpleled2, LOW);
  		digitalWrite(yellowled1, HIGH);
  		digitalWrite(yellowled2, HIGH);
	}
	
	else
	{
		
	}
		
	}
	
}

}

Show your complete sketch.

.

Hi
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Good to see from your bit of code, you using Millis for delays counting.

Tom... :slight_smile:

Good to see from your bit of code, you using Millis for delays counting.

The second thing I saw is delay().

The first thing I saw was the piss-poor indenting. I will NOT read code that is so poorly indented. Life's too short for putting up with that. Tools + Auto Format is NOT too difficult to use.

Hi,

11-06-2016, 11:11:24 Last Edit: 11-06-2016, 16:18:09 by judgemantbag

edited after my posting.

What is you code doing and not doing?

Tom..... :slight_smile:

Your code does not compile. The first indication that it is wrong is if you use T in the IDE (as indicated by PaulS); you will see that you have two closing '}' at the end. Another indication is that 'case 3:' starts at the beginning of the line and therefor does not seem to be part of the 'switch'; you probably have too many closing curlies ('}') in 'case 2:'.

Noobs often think that code formatting is done for cosmetic reasons. Far from it! When you format your code, you get instand counts of all the matched punctuation. Since matching the punctuation is so important to the code even compiling, you should do it every time you make some changes. That way you will know right away what's wrong.

The demo Several Things at a Time flashes 3 LEDs and does not use the delay() function.

The same technique can be used for sequential behaviour that needs timing.

...R