Multi-Tasking with Arduino.

I have my arduino code typed up and ready. the code is to power four motors and have them spin. but i need to have a push button switch connected to the arduino. how do i program the arduino so that it will check to see if the switch is pushed in, and when it is have the arduino turn off the motors and then turn them back on after a few seconds?

In the loop() function, do a digitalRead(...) to get the status on a i/p (switch pin). When the switch changes from High to Low or Low to High do a digitalWrite(...) to turn off the motor pin and enable an incrementing counter (incremented every few seconds). When the counter reaches a value that you decide, do a digital Write(...) to turn on the motor pin and disable & clear the counter.

Thanks for the reply. But I don't quite understand. Could you show me an explain of how I'm supposed to type up the code?

Do you understand how this code works?

//Motor off for X seconds on button push

#define motorOn  HIGH        // A HIGH will turn on the motor
#define motorOff LOW         // A LOW will turn off the motor

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int motor = 13;        // the pin that the motor is attached to
const int offTime = 30;      // 30/10= 3 seconds, time the motor is off after a switch push

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int secondsCounter =0;       // times how long the motor is off
boolean lock = LOW;          // allows counter to incement enables comparing

void setup()
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin,HIGH);   //conect pull-up resitor to switch

  pinMode(motor, OUTPUT);
  digitalWrite(motor,motorOn);    //Motor is on at power up


}     // END of setup()

void loop()
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // If allowed, compare the buttonState to its previous state
  if (buttonState != lastButtonState && lock == LOW)

    if (buttonState == LOW)       //is the button pushed now?
      lock = HIGH;                  //disables button monitoring    
      //allows timing
      secondsCounter = 0;           //counter starts at 0
      digitalWrite(motor,motorOff); //turn off the motor  
  // save the current state as the last state, 
  // for next time through the loop
  lastButtonState = buttonState;
  delay(100);                 //wait 1/10th of a second   

  if(lock == HIGH)            //are we in timing mode?
    secondsCounter++;         //add 1/10th second 

    if(secondsCounter >= offTime)    //have we reached the on time yet?
      lock = LOW;                    //enables button monitoring
      digitalWrite(motor,motorOn);   //turn the motor back on 
}     // END of loop()

i partly understand it. thanks :) i study up on it and figured it out using digitalRead. i tested it and it seemed to work fine. im going to try this code as well and see what i can come up with. :)

Another way to do this is with an interrupt when the button is pressed. See:

Another way to do this is with an interrupt when the button is pressed.

I wouldn’t recommend using interrupts for a problem like this. Using interrupts makes the solution much more complicated, and IMO should be done only when needed - it’s definitely not needed here.