Hi everybody
I have this very simple code
I am trying to control the LEDs 1 2 3 4 with the button
But I'm having some problems finding a solution
All I want is when the button is pressed I want the LEDs to flash gradually according to the selected time and I am still holding the button, when I take my finger off the button I want the process to stop immediately
for example
The code shows the following process:
When you press the button, LED 1 lights up, and after a second, LED 2 lights up, and after another second, LED 3 lights up, then LED 4 lights up, and in the end, all LEDs are extinguished.
This process occurs immediately after pressing the button
I want this process:
When the button is pressed without removing the finger from it, the LED 1 lights up, then after a second the LED 2 lights up. When the finger is lifted from the button, everything stops, or when you continue to press the button, the process occurs without stopping until all the LEDs are turned on and then all of them turn off.
int Button=2;
int LED1=3;
int LED2=4;
int LED3=5;
int LED4=6;
void setup() {
pinMode(Button, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW){
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED2, HIGH);
delay(1000);
digitalWrite(LED3, HIGH);
delay(1000);
digitalWrite(LED4, HIGH);
delay(3000);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
You cannot use long delays for this. While the delay() function is executing, the code is not reading the button and will not notice of it has been released.
I suggest you should not use delay() for more than 100ms.
Always the question - do you propose to learn anything?
That involves careful consideration of the problem.
You can actually do it without using the "millis()" approach; you just need to check the button on a regular basis, say every second.
So take your first code - it starts by checking the button. OK, if the button is pressed, turn the first LED on and delay one second.
Now check the button again. If the button is pressed, you go on to the next LED. Then check the button again. Continue this until you get to the three second part. Break this up into three button checks and one second delays. This means that it will always respond to the button within one second which while not "instantly" will be visually effective.
You will end up with several nested "if" statements, each only being entered if the test was passed from the previous one. The statements to turn off the LEDs will be outside the "if" statements.
You need to use "auto format" - Ctrl-T - in the IDE so the loops appear correctly nested. I see you did post the code correctly otherwise.
Yes, exactly.. This is exactly what I want. I do not want to use the "millis()" because my students are very novices and do not formulate complex code.
Unfortunately, their teacher is also a beginner in the world of Arduino, but ignorance and seeking knowledge is not a defect
In fact, I could not formulate my question correctly, because I did not understand much in Arduino terms, but after searching and looking at some of the codes, I discovered that all I wanted was to stop the void loop as soon as the finger was lifted from the button, and if the button was kept pressed, the void loop will complete
I am a general science teacher. As for the Arduino lessons, I volunteer only for my students in their spare time. I am still learning about programming.
That's it