How to make a motor work for a specific duration when push button pressed once (works like a toggle, without needing to hold it down)

I need the motor to rotate for a set duration. I also need for that to happen when the button is pushed once without having to hold it.

The following is the setup:

The following is the code:


Hey, @heyyy_world,
Remove the "code" pictures, and post your formatted code in a <CODE> block like this...

Also... define "rotate for a set duration"?

Your first setup will only have the Arduino, a push button switch and a LED/resistor. With power you need a program that turns on the LED for your set duration time when the button is pressed.

When that works, then move on to replace the LED with a MOSFET and your motor.

I need 2 buttons, one for the clockwise and the other for the counterclockwise rotation. Why would I need a MOSFET though? I already have an H-bridge. Am I using the wrong one?

int counterClockWise = 8;
int clockWise        = 12;
int delayTime        = 1000; 
int pushButton1      = 2;
int pushButton2      = 3;
int potentio         = A0;
int speedControl     = 11;
int button1Type;
int button2Type;
int speed;
int poReading;

int counterClockWise2 = 9;
int clockWise2        = 13;
int speedControl2     = 10;
int speed2;

void setup()
{
  pinMode(clockWise, OUTPUT);
  pinMode(counterClockWise, OUTPUT);
  pinMode(pushButton1, INPUT_PULLUP);
  pinMode(pushButton2, INPUT_PULLUP);
  pinMode(clockWise2, OUTPUT);
  pinMode(counterClockWise2, OUTPUT);
  
}

void loop()
{
  button1Type = digitalRead(pushButton1);
  button2Type = digitalRead(pushButton2);
  poReading    = analogRead(potentio);
  speed       = map(poReading, 0, 1023, 0, 255);
  speed2      = map(poReading, 0, 1023, 0, 255);
  analogWrite(speedControl, speed);
  analogWrite(speedControl2, speed2); 
  
  if((button1Type == HIGH)){
    digitalWrite(clockWise, LOW);
    digitalWrite(clockWise2, LOW);
  }
  else {
    digitalWrite(clockWise, HIGH);
    digitalWrite(clockWise2, HIGH);
  }
  if(button2Type == HIGH){
    digitalWrite(counterClockWise, LOW);
    digitalWrite(counterClockWise2, LOW);
  }
  else {
    digitalWrite(counterClockWise, HIGH);
    digitalWrite(counterClockWise2, HIGH);
  }
}

What I mean is that I want to press the pushbutton and then I want the motor to work for an amount of time ex: 10 seconds, then the program runs again only if I press the button again.

turn motor on and start timer when button pressed.
turn motor off when timer expires

const byte PinLed = LED_BUILTIN;

const byte PinBut = A2;
      byte butState;

const unsigned long MsecPeriod = 1500;
      unsigned long msec0;
      bool          tmr;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    // monintor timer
    if (tmr && msec - msec0 >= MsecPeriod) {
        tmr   = true;
        digitalWrite (PinLed, Off);
    }


    // monintor buttone
    byte but = digitalRead (PinBut);
    if (butState != but)  {             // state change
        butState  = but;
        delay (30);                     // debounce

        if (LOW == but) {               // press
            msec0 = msec;               // start timer
            tmr   = true;
            digitalWrite (PinLed, On);
        }
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    pinMode      (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

How do I know you have an H-bridge?

I coppied this but it won't work

It's in the picture of the setup

So is your 9 volt battery power supply.

I don't know what it is with people and these

I hope it's there as a symbol for the real battery.

:upside_down_face:

Yes

That style of battery is too weak for motors.
Use use a bench power supply to test, or a pack of six of AA (1.5 * 6 = 9vdc).

Is that why even though I uploaded the code I have it only works if the arduino board is connected to the laptop? I thought it would be sufficient because it is a hobby motor that doesn't need much voltage.

In your diagram the only source of power for the Arduino is from USB, of course you need it plugged in.

Voltage isn't the whole picture. If your load require double the current than what your battery can deliver, what happens with those 9 V then?

it simply demonstrates how to use a button and timer to turn an LED on and then off. You need to understand the approach and integrate it into your code.

look this over

// dual motor controller

const byte PinEn12   = 11;
const byte Pin1A     = 12;
const byte Pin2A     =  8;

const byte PinEn34   = 10;
const byte Pin3A     =  9;
const byte Pin4A     = 13;

const byte PinBut1   = 2;
const byte PinBut2   = 3;

const byte PinCcw1   = Pin2A;
const byte PinCw1    = Pin1A;
const byte PinSpd1   = PinEn12;

const byte PinCcw2   = Pin3A;
const byte PinCw2    = Pin4A;
const byte PinSpd2   = PinEn34;

const byte PinSpd    = A0;

const unsigned long MsecPeriod = 1000;
      unsigned long msec0;
      bool          tmr;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    // monitor timer
    if (tmr && msec - msec0 >= MsecPeriod)  {
        tmr = false;

        digitalWrite (PinCw1,  HIGH);
        digitalWrite (PinCw2,  HIGH);
        digitalWrite (PinCcw1, HIGH);
        digitalWrite (PinCcw2, HIGH);

        Serial.println ("stop");
    }

    // monitor buttons
    if (LOW == digitalRead (PinBut1))  {
        Serial.println ("cw");
        digitalWrite (PinCw1, LOW);
        digitalWrite (PinCw2, LOW);
        tmr   = true;
        msec0 = msec;
    }

    if (LOW == digitalRead (PinBut2))  {
        Serial.println ("ccw");
        digitalWrite (PinCcw1, LOW);
        digitalWrite (PinCcw2, LOW);
        tmr   = true;
        msec0 = msec;
    }

    // adjust speed
    analogWrite (PinSpd1, map (analogRead (PinSpd), 0, 1023, 0, 255));
    analogWrite (PinSpd2, map (analogRead (PinSpd), 0, 1023, 0, 255));
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    Serial.println ("ready");

    pinMode (PinBut1, INPUT_PULLUP);
    pinMode (PinBut2, INPUT_PULLUP);

    pinMode (PinCw1 , OUTPUT);
    pinMode (PinCcw1, OUTPUT);

    pinMode (PinCw2,  OUTPUT);
    pinMode (PinCcw2, OUTPUT);

    digitalWrite (PinCw1,  HIGH);
    digitalWrite (PinCw2,  HIGH);
    digitalWrite (PinCcw1, HIGH);
    digitalWrite (PinCcw2, HIGH);

    analogWrite (PinSpd1, 0);
    analogWrite (PinSpd2, 0);
}

Thank you so much. This was very insightful. So just to check my understanding. I need a different battery, right? then it will start working without a USB? which one do I use?

I'll just repeat what @xfpd wrote

Connect either to the barrel jack or Vin pin.