Override cut off with Push button

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

Never use while( ) unless you know why you shouldn’t.


Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring. Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.

int switch15 = 7;
int switch30 = 6;
int DC = 11;
int doorstate = 9;
int LED15 = 8;
int LEDR = 13;
int buzzer = 4;
unsigned long t_start = 0;

void setup() {

  pinMode(DC, OUTPUT);
  pinMode(doorstate, INPUT_PULLUP);
  pinMode(switch15, INPUT);
  pinMode(switch30, INPUT);
  pinMode(LED15, OUTPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (!digitalRead(doorstate)) {


    if (digitalRead(switch15) == HIGH) {

      tone(buzzer, 1000);

      (digitalWrite(LED15, HIGH));
      t_start = millis();
      while (millis() - t_start <= 250)
        ;
      digitalWrite(LED15, LOW);

      noTone(buzzer);

      digitalWrite(DC, HIGH);
      t_start = millis();
      while (millis() - t_start <= 5000)
        ;
      digitalWrite(DC, LOW);

      tone(buzzer, 1000);

      digitalWrite(LEDR, HIGH);
      t_start = millis();
      while (millis() - t_start <= 500)
        ;
      digitalWrite(LEDR, LOW);

      noTone(buzzer);
    }



    if (digitalRead(switch30) == HIGH) {

      tone(buzzer, 1000);

      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);
      t_start = millis();
      while (millis() - t_start <= 10000)
        ;
      digitalWrite(DC, LOW);

      tone(buzzer, 1000);

      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);
  }
}

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

what method of investing time do you prefer?

I prefer the first method. Am I able to use delays if I use the first method? is there any way of using An interrupt instead?

delay() does REALLY not work

As long as the microcontroller is executing a delay() no other function is executed

the function delay() should have the name

freeze_microcontroller_until_freezingtime_is_over()

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

then it is easier to check the button itself

You do not need delays or interrupts to do this. I would suggest using a state machine.

Hello YH05

all these

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.

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.