Hi people
I am new to Arduino and I have made a school project to switch a DC motor on with 2 push buttons for 5 secs and 10 secs with LEDs and a buzzer as indicators to show when the DC motor goes on and off.
The program is running well at the moment but I am trying to add an override button to cut off the DC motor while running.
void loop() {
if (!digitalRead(doorstate)) {
if (digitalRead(switch15) == HIGH) {
tone(buzzer, 1000); //buzzer and LED indicator
(digitalWrite(LED15, HIGH));
t_start = millis();
while (millis() - t_start <= 250) ;
digitalWrite(LED15, LOW);
noTone(buzzer);
digitalWrite(DC, HIGH); //turn on DC motor for 5secs
t_start = millis();
while (millis() - t_start <= 5000)
;
digitalWrite(DC, LOW);
tone(buzzer, 1000);
digitalWrite(LEDR, HIGH); //buzzer and LED indicator when DC motor stops
t_start = millis();
while (millis() - t_start <= 500)
;
digitalWrite(LEDR, LOW);
noTone(buzzer);
}
if (digitalRead(switch30) == HIGH) {
tone(buzzer, 1000); //buzzer and LED blink indicator when second button pushed
digitalWrite(LED15, HIGH);
t_start = millis();
while (millis() - t_start <= 250)
;
noTone(buzzer);
digitalWrite(LED15, LOW);
t_start = millis();
while (millis() - t_start <= 250)
;
tone(buzzer, 1000);
digitalWrite(LED15, HIGH);
t_start = millis();
while (millis() - t_start <= 250)
;
digitalWrite(LED15, LOW);
noTone(buzzer);
digitalWrite(DC, HIGH); //turn buzzer on for 10 secs when second button pushed
t_start = millis();
while (millis() - t_start <= 10000)
;
digitalWrite(DC, LOW);
tone(buzzer, 1000); // blink LED and buzzer indicator after DC motor stops
digitalWrite(LEDR, HIGH);
t_start = millis();
while (millis() - t_start <= 500)
;
noTone(buzzer);
digitalWrite(LEDR, LOW);
t_start = millis();
while (millis() - t_start <= 500)
;
tone(buzzer, 1000);
digitalWrite(LEDR, HIGH);
t_start = millis();
while (millis() - t_start <= 500;
digitalWrite(LEDR, LOW);
noTone(buzzer);
}
delay(500);
}
if ((digitalRead(doorstate))) {
digitalWrite(DC, LOW);
}
}
Those statements are the same as delay() since the code blocks while it is running and nothing else is getting done (like checking your override button).
You need to restructure your code so loop() executes quickly, over and over. Each time through loop, you check your buttons and do the proper thing. Look at the State Change example in the IDE (File->examples->02.digital->State Change Detection) to learn how to detect when buttons become pressed or released, not if they are pressed or released. You can then use that information to change what state you are in
using millis() does not automatically mean non-blocking code
t_start = millis();
while (millis() - t_start <= 10000)
// captured inside this loop
// all other code-execution is blocked
;
digitalWrite(DC, LOW);
the ugly way to make your code responsive to your stop button is to add in each and every while-loop reading the button if the button is pressed to jump out of the while-loop
just do enter the next while-loop and to jump out again
just do enter the next while-loop and to jump out again
just do enter the next while-loop and to jump out again
....
This takes time to add the code still is ugly
the elegant way is to use really non-blocking timing
and to use multi-mode-operation
both things need time to learn
The interrupt itself does not help
in this case
you could make things more complicated by setting a flag inside the interrupt and then check inside each and every while-loop for the flag beeing set
Line 32: while (millis() - t_start <= 250)
Line 40: while (millis() - t_start <= 5000)
Line 48: while (millis() - t_start <= 500)
Line 63: while (millis() - t_start <= 250)
Line 70: while (millis() - t_start <= 250)
Line 77: while (millis() - t_start <= 250)
Line 85: while (millis() - t_start <= 10000)
Line 93: while (millis() - t_start <= 500)
Line 100: while (millis() - t_start <= 500)
Line 107: while (millis() - t_start <= 500)
are hidden or how ever called delay() function calls blocking the execution of the sketch.
The sketch needs a millis() function based timer will be executed unblocked to read a button.
Take a view in the IDE examples to find the BinkWithOutDelay example. This example contains the mother of all timers in variations used in the Arduino biotop.
Make a copy and build your own timer as needed simply.