Page 1 of 1

using timer on DC motor ?

PostPosted: Fri Sep 12, 2014 9:03 pm
by alung92
i Using Cytron 2A Motor Driver Shield, i tend to stop the motor in certain time period, i using delay(); but not working. Any method make it work?

CODE: SELECT_ALL_CODE
int E1 = 5; 
int M1 = 4;
int E2 = 6;                     
int M2 = 7;                       
 
void setup()
{
    pinMode(M1, OUTPUT);   
    pinMode(M2, OUTPUT);
}
 
void loop()
{
 
    digitalWrite(M1,HIGH); 
    delay(1000*5); 
    digitalWrite(M2, LOW);
 
  analogWrite(E1, 255);   
 analogWrite(E2, 255);
 delay(1000*5);
 } 

Re: using timer on DC motor ?

PostPosted: Sat Sep 13, 2014 7:55 am
by ober
I don see any code that stop the motor from moving even after the delay, and the loop is an infinite loop, it will keep repeating. So if the motor rotate at the beginning, it will still go back to rotates.

Re: using timer on DC motor ?

PostPosted: Sat Sep 13, 2014 10:37 am
by alung92
Please guide me how to write the code to stop the motor with certain time period . I want stop the motor after 5 second, no repeating after 5 seconds.

Re: using timer on DC motor ?

PostPosted: Sat Sep 13, 2014 10:14 pm
by ober
1st try to understand the operation of code, simply giving code will not help you because you will have the same problem.

Which of the code control the start and stop of motor?

Re: using timer on DC motor ?

PostPosted: Sat Sep 13, 2014 10:35 pm
by alung92
High, low defined as the direction of motor.(0-255)represent the speed of motor.0 mean stop, 255 mean full speed of motor, but at the end i have no idea how stop the motor within the time setting. I want stop the motor in 5 seconds. If using for loop , it will still keep continue moviing after delay statement.

Re: using timer on DC motor ?

PostPosted: Sun Sep 14, 2014 12:03 pm
by ober
Good! But in your code the E is always 255, even if you put a delay, the motor will still keep running.

In C there are ways to ask the program to pause for infinite time, which is infinite loop. Use while(1); at the end of the program.