char led_red = 23;
char led_yellow = 19;
char led_green = 18;
char led_red_state = LOW;
char led_yellow_state = LOW;
char led_green_state = LOW;
long led_red_previous_millis = 0;
long led_yellow_previous_millis = 0;
long led_green_previous_millis = 0;
//this variable to store the last time
// the red led has changed store
int red_led_interval = 1000;
int yellow_led_interval = 2000;
int green_led_interval = 500;
// ON for 1000 ms
// OFF for 1000 ms
void setup() {
// put your setup code here, to run once:
pinMode(led_red, OUTPUT);
pinMode(led_yellow, OUTPUT);
pinMode(led_green, OUTPUT);
}
void loop() {
long current_millis = millis();
if (current_millis - led_red_previous_millis >= red_led_interval)
{
led_red_previous_millis = current_millis;
if(led_red_state == LOW)
led_red_state =HIGH;
else
led_red_state = LOW;
digitalWrite(led_red, led_red_state);
}
if (current_millis - led_yellow_previous_millis >= yellow_led_interval)
{
led_yellow_previous_millis =current_millis;
if(led_yellow_state == LOW)
led_yellow_state =HIGH;
else
led_yellow_state = LOW;
digitalWrite(led_yellow, led_yellow_state);
}
if (current_millis - led_green_previous_millis >= green_led_interval)
{
led_green_previous_millis =current_millis;
if(led_green_state == LOW)
led_green_state =HIGH;
else
led_green_state = LOW;
digitalWrite(led_green, led_green_state);
}
}