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);
}
}
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. :~