Controlling Large DC Motor with Arduino

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

How ever, it doesn't work as I intended

That means either that the code does something you don't want, or it appears to do nothing. Which is it?

We'll need a link to the library you are using, too.

Having a diagram of the wiring would be nice, too. Help us to help you.

Hi Fellas,

Thank you for your posts.

Presently, the DC Motor drives continuously without stops at the micro switches

  1. It drives in the forward direction and as soon as the micro switch is triggered, it immediately reverses the direction
  2. Also it drives continuously until I stop the power

However, I want

  1. the motor to stop driving as soon as the micro-switch triggers. It should then wait quietly for 2 seconds, then it starts back in the opposite direction (This is where I am struggling to code it) -

  2. Also the motor should come to a complete stop after n number of cycles without I manually turnoff the power (say after 5 forward and reverse rotations) - I used the for loop in the void loop(), but it doesn't work........

Library
I am using SyrenSimplified Library and please see the below link.
https://www.dimensionengineering.com/software/SyRenSimplifiedArduinoLibrary/html/index.html

I see that they have a command as follows:
void SyRenSimplified::stop ( ) to stop the motor. But I'm quit perplexed about how to incorporate it in my code....

Controlling Larger DC Motor with 2s wait time .txt (2.4 KB)

Please find my latest code. Thanks.

Controlling DC Motor with 2s wait time .txt (2.9 KB)

ATKGopinath, did you ever resolve stopping after a select number of cycles occur?

jpwade:
ATKGopinath, did you ever resolve stopping after a select number of cycles occur?

With a post count of 4 I doubt that he is still around 2 years later.

I have not looked at the code, but adding a counter which is incremented on each motor reverse and a boolean to control whether the motor moves when the count exceeds a given value sounds simple enough.

Have you got a program that you have a problem with ?

thank you have tried ----> working with script in the initial post, and have a count of passes occuring and a stop at end of count, thou the integrity of count is not worth spit ----> inconsistent with running total passes, stops prior to final count (deounce, overshoot, program timing? ---- AND proximity sensors are what they are ---- proximity)

tried ---->

int PassCounter = 0; // counter for number of passes,


void loop(){
ForwardState = digitalRead(ForwardButton);
ReverseState = digitalRead(ReverseButton);

i put in this ---->

delay (50); // dependent on acceleration of motor - at motor stop, allows carriage to move off of prox sensor, Also, increase of delay lengthens overshoot distance across top of prox sensors

without this delay initiation of passes will not start when a proximity sensor is triggered to start reciprocating forward reverse movements


//SET THREE DIFFERENT STATES (FORWARD, REVERSE, STOP)

if (ForwardState == HIGH) { //up state
CurrentState = 1;
}

i put in this ---->

if(PassCounter>10) {
CurrentState = 0;
ST.motor(1, 0);
}

if (ReverseState == HIGH) { // down state
CurrentState = 2;
PassCounter = PassCounter + 1; // increment pass counter
}


problems i am seeing , have any thoughts on these ????

if motor speed is set too slow, 2 or 3 passes occur then motor goes to stop , as if total pass count has been reached .... (displayed at end of youtube video) , count was set to (i think) and at slow speed motor stops at 3 proximity triggers)

1st part of video is - make x amount of passes ----> then ----> accelerate motor ----> then ----> after a higher pass count stop motor

Syren 50a with Uno Reciprocating Count and Stop - Syren 50a with Uno Reciprocating Count and Stop - YouTube


p.s mess on the bread board is for another project (100vdc h-bridge, thus the heat sink),

for the reciprocating in out , analog pots , stop button and proximity inputs are at the upper left side of the breadboard grid.