Hi Folks,
Please bear with me for this long msg.
I wanted to drive a motor in both clockwise and reverse directions (Similar to the youtube video "Control Large DC Motors with Arduino! SyRen Motor Driver Tutorial".
But to complicate the things, it should stop driving as soon as the proximity sensor triggers. It then waits quietly for 2 seconds, then it starts back in the opposite direction and repeats at the other end.
I also, wants the above full cycle to be repeated for n number of times (say 5 cycles).
I have tried the original code as given in "www.nyccnc.com" website, with little amendment as you see below:
#include <SyRenSimplified.h>
const int StopButton = 7;
const int ForwardButton = 3;
const int ReverseButton = 2;
const int ForwardSpeedPin = A1;
const int ReverseSpeedPin = A2;
int ForwardState = 0;
int ReverseState = 0;
int StopState = 0;
int ForwardSpeed = 0;
int ReverseSpeed = 0;
int CurrentState = 0; // 0 is stopped, 1 is forward, 2 is reverse
SyRenSimplified ST;
// Simplified Serial Mode. Baud rate of 9600. Arduino TX->1 -> Sabertooth S1 Arduino GND -> Sabertooth 0V [ST.motor(1, X); X of 0 is full reverse, 128 is stop, 255 full forward] <--- WRONG! -127 full reverse, 0 stop, 127 full forward
void setup()
{
SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.
ST.motor(1, 0);
ForwardState = digitalRead(ForwardButton);
ReverseState = digitalRead(ReverseButton);
ForwardSpeed = analogRead(ForwardSpeedPin);
ReverseSpeed = analogRead(ReverseSpeedPin);
ForwardSpeed = map(ForwardSpeed,0,1023,127,1);
ReverseSpeed = map(ReverseSpeed,0,1023,-127,-1);
}
void loop()
{
ForwardState = digitalRead(ForwardButton);
ReverseState = digitalRead(ReverseButton);
StopState = digitalRead(StopButton);
//SET THREE DIFFERENT STATES (FORWARD, REVERSE, STOP)
if (ForwardState == LOW)
{ //up state
CurrentState = 1;
}
if (ReverseState == LOW)
{ // down state
CurrentState = 2;
}
if (StopState == LOW)
{
CurrentState = 0;
ST.motor(1, 0);
}
//FINISHED WITH "SET THREE DIFFERENT STATES (FORWARD, REVERSE, STOP)"
int i;
for( int i =0; i < 5; i++) // To repeat the full cycle for 5 number of times
{
if (CurrentState == 0)
{
ST.motor(1, 0);
//delay(500);
}
if (CurrentState == 1)
{ //Forward
ST.motor(1, StopState);
//I also tried ST.motor(1,0), but it stops the whole movement for ever and doesn't return after 2 seconds.
Delay(2000);
ST.motor(1,ForwardSpeed);
ForwardSpeed = analogRead(ForwardSpeedPin);
ForwardSpeed = map(ForwardSpeed,0,1023,127,1);
delay(200);
}
if (CurrentState == 2)
{ //Reverse
ST.motor(1, StopState);
//I also tried ST.motor(1,0), but it stops the whole movement for ever and doesn't return after 2 seconds.
Delay(2000);
ST.motor(1, ReverseSpeed);
ReverseSpeed = analogRead(ReverseSpeedPin);
ReverseSpeed = map(ReverseSpeed,0,1023,-127,-1);
delay(200);
}
}
ST.motor(1, 0); // I’m trying to stop the whole thing after 5 cycles
}
How ever, it doesn't work as I intended and I really appreciate, if you could help me to correct the above code please.
Thanks for your time and effort.
Gopi