Getting motors to run for set time but be able to stop if toggle switch clicked

Hi there

I'm incredibly new to coding and it is not my forte at all but I'm trying! I want to set up a jig that runs four 3.3V motors at the same time for 2.5 mins when turned on by a toggle switch. I have managed to get them all running correctly with the switch and even them for 2.5 mins if I use a delay but I want to be able to press the toggle switch again during the 2.5 mins and turn the motor off (then when re pressed it starts the 2.5 mins running again).

int motorState1=0;
int motorPin1=13;
int motorState2=0;
int motorPin2=10;
int motorState3=0;
int motorPin3=8;
int motorState4=0;
int motorPin4=6;

int buttonPin=2;
int buttonNew;
int buttonOld=1;
int dt=100;

void setup() {
  // put your setup code here, to run once:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(buttonPin, INPUT);

void loop() {
buttonNew=digitalRead (buttonPin);
if(buttonOld==0 && buttonNew== 1) {
  if (motorState1== 0) {
    digitalWrite (motorPin1, HIGH);
    digitalWrite (motorPin2, HIGH);
    digitalWrite (motorPin3, HIGH);
    digitalWrite (motorPin4, HIGH);

  else {
    digitalWrite (motorPin2, LOW);
    digitalWrite (motorPin4, LOW);
delay (dt);

This is my code ^^^ I followed this guy's code and then just swapped the LEDs for motors.

I've searched around and it seems like millis is the answer but I just do not know how to incorporate it properly!

Any help would be much much much appreciated. Hope this is clear enough and makes sense!

Thanks :slight_smile:

See See Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum and Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Hi UK HeliBob

Thanks so much for answering and so quickly too!!

I had read those but gave them a re read as the first time I didn't understand. I am still unsure how I would include millis into my needs but am having a go.

I tried your part 3 and 4 codes and all I was receiving on the monitor were the symbols shown in the screenshots. Any clue why?


It looks like the baud rate in your code does not match that of the Serial monitor. Change one or the other so that they match

Also possibly useful to you: Timer counter state change toggle FSM - all at once

Hello, yes that seems to make it work thank you!

Sorry I am very new to this so seems a little silly to make such a basic mistake!

Why do you choose a different baud rate than the standard one, I've only seen other codes with 9600, so am curious as to why?

As you use byte quite a lot in the codes, why that over int? I looked up the answer but couldn't really understand it haha so if you could explain it very simply that would be amazing!


Great, I'll check it out and let you know how I get on! Cheers

9600 baud is verrry slow by today's standards. It's not a problem to use the highest baud rate your system will support.

Run this short sketch

// byte vs. int size

byte eightBits = 0xf0;
unsigned sixteenBits = 0xff00;

void setup() {

  Serial.print(" byte of storage required for ");
  Serial.println(eightBits, HEX);

  Serial.print(" byte of storage required for ");
  Serial.print(sixteenBits, HEX);

void loop() {


115200 is my standard baud rate. Most/all of the Arduino examples were written a while ago and use 9600 as their standard. You can never have too may standards :slight_smile:

If you ant you can use much faster baud rates, such as 2000000, yes, 2 million, if you want to and your connected hardware supports it.

As to int vs byte, as doug has illustrated it saves memory. Not so important with Mickey Mouse example sketches but a good habit to get into, as RAM is a precious resource on most microprocessors, is to use the smallest data type that will do the job in hand

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