voltage controlled blinking leds?

Hi guys, I need to do a device with tree voltage controled leds ,first led (red) constantly blinking other two not ,when push the button red led stop, other two start to blink.Idea is when push the button, red immediately stop blinking no matter in what condition is and start other two.Now with this sketch when push the button red make on/off cycle (it is obvious) and other two start. :~

int Red = 12;                   
int Yellow = 10;                  
int Green = 11;                                    
int Btn = 2;                    
int val = 0;                        

void setup() {                             
  pinMode(Red, OUTPUT);         
  pinMode(Yellow, OUTPUT);        
  pinMode(Green, OUTPUT);       
  pinMode(Btn, INPUT);       
}

void loop(){  
  
  digitalWrite (Red, LOW);     
  digitalWrite (Green, LOW);
  digitalWrite(Yellow,LOW);
  delay(3000);
  digitalWrite (Red, HIGH);
  delay(3000);
 
  val = digitalRead(Btn);
  
 
  if (val == HIGH) {                                 
    digitalWrite(Green, HIGH);  
    digitalWrite(Red,LOW);  
    delay(3000); 
    digitalWrite(Yellow,HIGH);    
    digitalWrite(Green, LOW);    
    delay(3000);                    
             
   
  }                     
}

Any ideas what i must change?

You have to remove all the calls to delay.

int Red = 12;                   
int Yellow = 10;                  
int Green = 11;                                    
int Btn = 2;                    
int val = 0;                        

void setup() {                             
  pinMode(Red, OUTPUT);         
  pinMode(Yellow, OUTPUT);        
  pinMode(Green, OUTPUT);       
  pinMode(Btn, INPUT);       
}

void loop(){ 
 
  val = digitalRead(Btn);
  
 
  if (val == HIGH) {

    digitalWrite(Red,LOW);
    digitalWrite(Yellow,HIGH);                       
    digitalWrite(Green,HIGH);  
    
    delay(3000);
    
    digitalWrite(Green,LOW);
    digitalWrite(Yellow,HIGH); 
  }
  else {
    digitalWrite(Red, LOW);     
    digitalWrite(Green, LOW);
    digitalWrite(Yellow,LOW);

    delay(3000);

    digitalWrite (Red, HIGH);
  }

delay(3000);

}

Is this what you wanted? i changed it a little bit, so i hope it works now :slight_smile:

Why would you erase all the calls to delay? if you want it to blink every 3 seconds...

Why get rid of the delays?
To improve responsiveness to the button.

Just throwing in some insight to the comment posted, I believe what he means is that when the Arduino is calling a delay(x), the code is just waiting for that time to elapse. It can't execute any other code while it waits, so even if you push the button, if it's currently waiting that x time, it won't get that button press. A solution is provided somewhere that explains how to replace delay(x) with millis(x) timers, allowing the rest of the code to run while the timer is also running. By removing the delays from your code, you make sure that when the button is pressed, the Arduino is not in a "standstill" condition, waiting for the delay(x) to finish.

Thanks Steen but this sketch not work.Leds flashing chaotically.
In real board on a place of button is a voltage comparator, in led places are drivers which manage solenoids.Voltage comparator monitoring a current on a "red led" and when is too high stop it and start other two.In my sketch obviously problem is a red led delays in a first part. :~

Thanks guys problem solved!
:slight_smile: work perfect!

const int Red = 12;      
int RedState = LOW;           
long previousMillis = 0;      
long interval = 1000; 
int Yellow = 10;                  
int Green = 11;                                    
int Btn = 2;                    
int val = 0;                        

void setup() {                             
  pinMode(Red, OUTPUT);         
  pinMode(Yellow, OUTPUT);        
  pinMode(Green, OUTPUT);       
  pinMode(Btn, INPUT);       
}

void loop(){  
   unsigned long currentMillis = millis();
  
   if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;   
     if (RedState == LOW)
       RedState = HIGH;
     else
       RedState = LOW;
     digitalWrite(Red, RedState);
   }
    
  val = digitalRead(Btn);
  
 
  if (val == HIGH) {      
    digitalWrite(Green, HIGH);  
    digitalWrite(Red,LOW);  
    delay(3000); 
    digitalWrite(Yellow,HIGH);    
    digitalWrite(Green, LOW);    
    delay(3000);                    
    digitalWrite(Yellow,LOW);
      }

  }