Run a DC Motor for a specific time Arduino

Hello...
I want to run a DC motor for a specific time then stops, using this code either by switches or sensors:

void setup ()
{
pinMode (IN1, OUTPUT);
pinMode(IN2, OUTPUT) ;

pinMode (IN3, OUTPUT);
pinMode(IN4, OUTPUT);

pinMode (SW1, INPUT);
pinMode (SW2, INPUT);

pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
Serial.begin(9600);
}

void loop()
{
// DIRECTION CONTROL
boolean sw1Val = digitalRead(SW1);
boolean sw2Val = digitalRead(SW2);

if (!sw1Val && !sw2Val) // 0 0 - right
{
  Serial.println("XXXX");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 255);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

else if (!sw1Val && sw2Val) // 0 1 - left
{
    Serial.println("XXXX");
  digitalWrite (IN1, HIGH); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 255);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

}

else if (sw1Val && !sw2Val) // 1 0 - up
{
    Serial.println("XXXX");
  digitalWrite (IN1, LOW); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  
  analogWrite(ENA, 255);
  analogWrite(ENB, 0);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

else if (sw1Val && sw2Val) // 1 1 - Down

{
    Serial.println("XXXX");
  digitalWrite (IN1, LOW); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 255);
  analogWrite(ENB, 0);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}
}

Does that program compile and make your motor turn? Doesn't look like a complete program.

yes, there is a #define before void set up and for each one but I didn't state them.

yes, there is a #define before void set up and for each one but I didn't state them..

Then the problem is down to you being able to detect time. Have you used millis() before? You save the current millis() value at the beginning of your time calculation and then see if the difference between that original time and the millis() right now is greater or equal to the time you want.

and what is the best way to include it? since it is the first time to do so. and will it stop the motor?

All up to you. What turns the motor on? When you determine that you can do the opposite to turn it off.

i know how to turn the motor off as shown in the code by making it low and 0. but still it wouldn't stop

Hello pp19
Post the schematic to see how we can help.

There is no need for the schematic, all I want is to run the motor for a specific time either in seconds/minutes/hours.. and then after this time, I want to stop the motor.

For example, when switch 1 is on, and switch 2 is off, I want to run the motor for 1 second or 1 minute, and then I want it to stop after this time and doesn't repeat the loop unless there is any change in the switch

Ok
Have a nice day and enjoy coding in C++.
I´m out.

seriously, is that an answer to my question??

It's an answer to you saying that we don't need a schematic or to see the complete code. If you won't provide full details we probably won't bother with your question.

Steve

if you want, here it is:

#define SW1 9
#define SW2 8
#define IN1 13
#define IN2 6
#define IN3 7
#define IN4 12
#define ENA 11
#define ENB 10

void setup ()
{
pinMode (IN1, OUTPUT);
pinMode(IN2, OUTPUT) ;

pinMode (IN3, OUTPUT);
pinMode(IN4, OUTPUT);

pinMode (SW1, INPUT);
pinMode (SW2, INPUT);

pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
Serial.begin(9600);
}

void loop()
{

boolean sw1Val = digitalRead(SW1);
boolean sw2Val = digitalRead(SW2);

if (!sw1Val && !sw2Val) // 0 0 - right
{
  Serial.println("XXXX");
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 255);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

else if (!sw1Val && sw2Val) // 0 1 - left
{
    Serial.println("XXX");
  digitalWrite (IN1, HIGH); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 255);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

}

else if (sw1Val && !sw2Val) // 1 0 - up
{
    Serial.println("XXX");
  digitalWrite (IN1, LOW); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  
  analogWrite(ENA, 255);
  analogWrite(ENB, 0);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

else if (sw1Val && sw2Val) // 1 1 - Down

{
    Serial.println("XXXX");
  digitalWrite (IN1, LOW); //RUN IN REVERSE DIRCECTION
  digitalWrite(IN2,LOW);
  
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 255);
  analogWrite(ENB, 0);
  
  delay(5000);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}
}

Noting that, I'm using the h-bridge module and Arduino mega. but it is the same connection, where the component I am using can go in 4 directions and be shown in the code.

I think the problem is that you are currently acting on the switch positions even if the switch has not changed. You need to store the previous switch positions so you can tell if the switch positions have changed.

For example, change:

void loop()
{
  boolean sw1Val = digitalRead(SW1);
  boolean sw2Val = digitalRead(SW2);

to

void loop()
{
  static boolean oldSW1Val = false;
  static boolean oldSW2Val = false;

  boolean sw1Val = digitalRead(SW1);
  boolean sw2Val = digitalRead(SW2);

  if (sw1Val == oldSW1Val && sw2Val == oldSW2Val)
    return;  // No change: do nothing

  oldSW1Val = sw1Val;
  oldSW2Val = sw2Val;

thank you for the code! it worked perfectly.
now if I want to use it using lidar sensor and to do the same function and work but instead of switches, lidar sensor and distance.

void loop() {

  if (distance >10) 
        {
           digitalWrite(IN1,LOW);
           digitalWrite(IN2,HIGH);
           analogWrite(MOTENA,200);
  
           delay(500);
            
           digitalWrite(IN1,LOW);
           digitalWrite(IN2,LOW);
           analogWrite(MOTENA,0);
           delay(500);
       }

       else if (distance <10) 
       {

           digitalWrite(IN1,HIGH);
           digitalWrite(IN2,LOW);
           analogWrite(MOTENA,200);
  
           delay(500);
            
           digitalWrite(IN1,LOW);
           digitalWrite(IN2,LOW);
           analogWrite(MOTENA,0);
           delay(500);
       }
        
       }

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