Control DC motor using timer

Hi all,
I wanna ask something. Is it possible to control a DC motor using a timer? it means that for example the motor is running for two seconds. after two seconds, the motor will automatically stop. is that possible? Because when i tried using delay after putting the condition of the motor to move forward then the stop condition after the delay, it did not work.
it looked like this:
Forward();
delay(1000);
Stop();
any recommendation?
best regards :D
I wanna ask something. Is it possible to control a DC motor using a timer? it means that for example the motor is running for two seconds. after two seconds, the motor will automatically stop. is that possible? Because when i tried using delay after putting the condition of the motor to move forward then the stop condition after the delay, it did not work.
it looked like this:
Forward();
delay(1000);
Stop();
any recommendation?
best regards :D
2019-02-25 17:23:26 You can run the motor clockwise for 180 seconds and stop for a while and run count clockwise for 180 seconds.
robert.chen

2019-02-25 02:02:12 i want to do dc motor timing with ardiuino 3 minutes right 3 minute left to move left and I should stop working for a while and I have no knowledge but please help
hamzaaliharta

2012-09-19 09:59:41 ah,it finally it works :D
just like what hector said :D
thanks a lot hector :D
now i can move on to the next phase ;)
thanks :)
holmes.apprentice
just like what hector said :D
thanks a lot hector :D
now i can move on to the next phase ;)
thanks :)

2012-09-19 07:07:04 guys,please help me check, what is wrong with this code :(
it doesnot even work once to stop after the delay set :(
[code]
//#include <Servo.h>
int motorRight1 = 36;
int motorRight2 = 37;
int motorLeft1 = 40;
int motorLeft2 = 41;
int enable1 = 38;
int enable2 = 39;
//int motorPlot1 = 36;
//int motorPlot2 = 37;
//int enablePlot = 38;
//Servo seedOpenMotor;
//Servo seedPushMotor;
//Servo moisMotor;
void setup()
{
pinMode(motorRight1, OUTPUT);
pinMode(motorRight2, OUTPUT);
pinMode(enable1, OUTPUT);
pinMode(motorLeft1, OUTPUT);
pinMode(motorLeft2, OUTPUT);
pinMode(enable2, OUTPUT);
// naviMotor.attach(44); // set the digital pin of the front servo
// seedOpenMotor.attach(45); // set the digital pin of the side servo
// seedPushMotor.attach(46); // set the digital pin of the rear servo
// moisMotor.attach(47);
// Serial.begin(9600);
// digitalWrite(38,HIGH);
// digitalWrite(39,HIGH);
}
void loop()
{
digitalWrite(motorRight1,HIGH);
digitalWrite(motorRight2,LOW);
digitalWrite(motorLeft1,LOW);
digitalWrite(motorLeft2,HIGH);
analogWrite(enable1, 255);
analogWrite(enable2, 255);
delay(10);
digitalWrite(motorRight1,LOW);
digitalWrite(motorRight2,LOW);
digitalWrite(motorLeft1,LOW);
digitalWrite(motorLeft2,LOW);
analogWrite(enable1, 0);
analogWrite(enable2, 0);
delay(10);
}
[/code]
Please :(
sorry for the late reply anyewy.
holmes.apprentice
it doesnot even work once to stop after the delay set :(
[code]
//#include <Servo.h>
int motorRight1 = 36;
int motorRight2 = 37;
int motorLeft1 = 40;
int motorLeft2 = 41;
int enable1 = 38;
int enable2 = 39;
//int motorPlot1 = 36;
//int motorPlot2 = 37;
//int enablePlot = 38;
//Servo seedOpenMotor;
//Servo seedPushMotor;
//Servo moisMotor;
void setup()
{
pinMode(motorRight1, OUTPUT);
pinMode(motorRight2, OUTPUT);
pinMode(enable1, OUTPUT);
pinMode(motorLeft1, OUTPUT);
pinMode(motorLeft2, OUTPUT);
pinMode(enable2, OUTPUT);
// naviMotor.attach(44); // set the digital pin of the front servo
// seedOpenMotor.attach(45); // set the digital pin of the side servo
// seedPushMotor.attach(46); // set the digital pin of the rear servo
// moisMotor.attach(47);
// Serial.begin(9600);
// digitalWrite(38,HIGH);
// digitalWrite(39,HIGH);
}
void loop()
{
digitalWrite(motorRight1,HIGH);
digitalWrite(motorRight2,LOW);
digitalWrite(motorLeft1,LOW);
digitalWrite(motorLeft2,HIGH);
analogWrite(enable1, 255);
analogWrite(enable2, 255);
delay(10);
digitalWrite(motorRight1,LOW);
digitalWrite(motorRight2,LOW);
digitalWrite(motorLeft1,LOW);
digitalWrite(motorLeft2,LOW);
analogWrite(enable1, 0);
analogWrite(enable2, 0);
delay(10);
}
[/code]
Please :(
sorry for the late reply anyewy.

2012-09-01 12:38:16 Holmes.Apprentice,
I am using these codes for my Motor Shield and it works how you want it to work. Moves forward for 3 Second and Stops.
Here are the codes.
[code]int ml = 7;
int el = 6;
int er = 5;
int mr = 4;
char val;
void setup()
{
pinMode(ml, OUTPUT);
pinMode(mr, OUTPUT);
Serial.begin(115200);
}
void loop()
{
if(Serial.available()){;}
val = Serial.read();
if (val == 'w')
{
Serial.println("Moving Forward");
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
analogWrite(el, 255);
analogWrite(er, 255);
delay(3000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'x')
{
Serial.println("Moving Backwards");
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 255);
analogWrite(er, 255);
delay(3000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'a')
{
Serial.println("Turning Left");
digitalWrite(ml,HIGH);
digitalWrite(mr,LOW);
analogWrite(el, 255);
analogWrite(er, 255);
delay(1000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'd')
{
Serial.println("Turning Right");
digitalWrite(ml,LOW);
digitalWrite(mr,HIGH);
analogWrite(el, 255);
analogWrite(er, 255);
delay(1000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 's')
{
Serial.println("Stopping");
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
}
[/code]
Hope this helps.
Ali.
Ali.Irshad
I am using these codes for my Motor Shield and it works how you want it to work. Moves forward for 3 Second and Stops.
Here are the codes.
[code]int ml = 7;
int el = 6;
int er = 5;
int mr = 4;
char val;
void setup()
{
pinMode(ml, OUTPUT);
pinMode(mr, OUTPUT);
Serial.begin(115200);
}
void loop()
{
if(Serial.available()){;}
val = Serial.read();
if (val == 'w')
{
Serial.println("Moving Forward");
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
analogWrite(el, 255);
analogWrite(er, 255);
delay(3000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'x')
{
Serial.println("Moving Backwards");
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 255);
analogWrite(er, 255);
delay(3000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'a')
{
Serial.println("Turning Left");
digitalWrite(ml,HIGH);
digitalWrite(mr,LOW);
analogWrite(el, 255);
analogWrite(er, 255);
delay(1000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 'd')
{
Serial.println("Turning Right");
digitalWrite(ml,LOW);
digitalWrite(mr,HIGH);
analogWrite(el, 255);
analogWrite(er, 255);
delay(1000);
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
if (val == 's')
{
Serial.println("Stopping");
digitalWrite(ml,LOW);
digitalWrite(mr,LOW);
analogWrite(el, 0);
analogWrite(er, 0);
}
}
[/code]
Hope this helps.
Ali.

2012-08-31 19:32:31 um...I don't know what the rest of your code looks like, but if there is no other delay between the stop instruction and the forward instruction, then you wont see the stop...
I mean this:
forward> delay> stop>>forward...
You see, no delay between the stop and forward section. So either add an additional delay to your loop or put in some conditional formatting to make sure this sequence is only executed once...
Hector
I mean this:
forward> delay> stop>>forward...
You see, no delay between the stop and forward section. So either add an additional delay to your loop or put in some conditional formatting to make sure this sequence is only executed once...
