Need the simple code for turing Clockwise and Counterclockwise a DC motor

Hi Every one

I have a simple project that the following pins of my arduino UNO are connected to a L293D IC and would like to receive a simple code that the dc motor can turn to right and left by dialing 1 or 2 or using an App and also the motor working time can be set for 5000 mili seconds and then off

int motorPin1 = 3; // pin 2 on L293D IC

int motorPin2 = 4; // pin 7 on L293D IC

int enablePin = 5; // pin 1 on L293D IC

Regards

Have you so far written a sketch to at least run the motor one way then the other without any commands? Seems the way the forum works if you have to bring something first: you are unlikely to get a turnkey solution.

There must be lots of examples of how to drive a motor with an L293: all you need is to make the enable pin high and then the other two pins high/low for one direction and low/high for the other.

Once that's working you can add the other functionality with help.

Thanks for your reply and I have the program that can turn the dc motor to right and left by dialing 1 and 2 and 0 to stop the motor but I did not send it to avoid any confusion, so I just sent the connection pins that any one who apply to help me can send the code that believe is good for turn the dc motor CW and CCW with the 5000 mili seconds working time.

Anyway, you can see the detail of project and code on http://www.instructables.com/id/Arduino-Control-DC-Motor-via-Bluetooth/step2/Schematics-and-common-mistakes/

But should remind you that I am not familiar with codes and would like just to get a ready to upload code according the functions what I required above.

Regards

motorlock:
But should remind you that I am not familiar with codes and would like just to get a ready to upload code according the functions what I required above.

Well that's impossible without the code you already have, since nobody would know where to put the new lines to do the 5000ms and turn it off.

Here's a wild guess. At some point there will be lines like:

digitalWrite(motorPin1, HIGH);  //for one direction
digitalWrite(motorPin2, LOW);

and

digitalWrite(motorPin1, LOW);  //for the other direction
digitalWrite(motorPin2, HIGH);

A quick and dirty way to do what you want is to use delay(), followed by the lines to stop the motor.

So immediately after each of the 2 sets of lines I just showed, stick these in:

delay(5000);
digitalWrite(motorPin1, LOW);  //for stop
digitalWrite(motorPin2, LOW);

So you'll have this:

digitalWrite(motorPin1, HIGH);  //for one direction
digitalWrite(motorPin2, LOW);
delay(5000);
digitalWrite(motorPin1, LOW);  //for stop
digitalWrite(motorPin2, LOW);

And this:

digitalWrite(motorPin1, LOW);  //for the other direction
digitalWrite(motorPin2, HIGH);
delay(5000);
digitalWrite(motorPin1, LOW);  //for stop
digitalWrite(motorPin2, LOW);

The below code should work well to control both H-Bridges Let us know how it goes :slight_smile:
Set Speed to 255 for full forward
set Speed to -255 for full reverse
or anywhere in between for other speeds
set Speed to Zero for full Stop

int InputMax = 255; // Set motor Speed input range -255 to 255 
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePinA = 5; // pin 1 on L293D IC 
int enablePinB = 6; // pin 9 on L293D IC
int motorPin3 = 7; // pin 10 on L293D IC
int motorPin4 = 8; // pin 15 on L293D IC
// To Call the function 

int SpeedA = 0; // Full Stop
int SpeedB = 0; // full Stop
void setup() {
  // put your setup code here, to run once:

pinMode(motorPin1 ,OUTPUT);
pinMode(motorPin2 ,OUTPUT);
pinMode(enablePinA ,OUTPUT);
pinMode(enablePinB ,OUTPUT);
pinMode(motorPin3 ,OUTPUT);
pinMode(motorPin4 ,OUTPUT);
TankPWM(SpeedA,SpeedB, enablePinA ,motorPin1 ,motorPin2 , enablePinA , motorPin3 , motorPin4);


}

void loop() {
  static int DirA = -1;
  static int DirB = 2;
  SpeedA += DirA;
  SpeedB += DirB;
  TankPWM(SpeedA,SpeedB, enablePinA ,motorPin1 ,motorPin2 , enablePinA , motorPin3 , motorPin4); // Call the motor Dirve Function
  delay(10);
  if(abs(SpeedA) >= 255) DirA = -1*DirA; // Reverse counter
  if(abs(SpeedB) >= 255) DirB = -1*DirB; // Reverse counter
}

void TankPWM(int16_t LeftDrive,int16_t RightDrive, uint8_t LeftPin, uint8_t Forward_LeftPin , uint8_t  Reverse_LeftPin, uint8_t RightPin, uint8_t  Forward_RightPin, uint8_t Reverse_RightPin ){
  static int16_t LastLeftDrive;
  static int16_t LastRightDrive;
  if ((LeftDrive == 0) ||((LastLeftDrive > 0) != (LeftDrive > 0))){
    digitalWrite(Forward_LeftPin, LOW);
    digitalWrite(Reverse_LeftPin, LOW); 
    // Add Breaking 
  }
  if ((RightDrive == 0) ||((LastRightDrive > 0) != (RightDrive > 0))){
    digitalWrite(Forward_LeftPin, LOW);
    digitalWrite(Reverse_LeftPin, LOW); 
    // Add Breaking 
  }
  //Add Slow Ramp up to Speed to Prevent sudden changes in direction of motor

  LastLeftDrive = LeftDrive;
  LastRightDrive = RightDrive;

  analogWrite(LeftPin,constrain(map(abs(LeftDrive),0,InputMax ,0,255),0,255));
  analogWrite(RightPin,constrain(map(abs(RightDrive),0,InputMax ,0,255),0,255));

  if(LeftDrive < 0){
    digitalWrite(Forward_LeftPin, HIGH);
    digitalWrite(Reverse_LeftPin, LOW);
  }
  if(RightDrive < 0){
    digitalWrite(Forward_RightPin, HIGH);
    digitalWrite(Reverse_RightPin, LOW);
  }
  if(LeftDrive > 0){
    digitalWrite(Forward_LeftPin, LOW);
    digitalWrite(Reverse_LeftPin, HIGH);
  }
  if(RightDrive > 0){
    digitalWrite(Forward_RightPin, LOW);
    digitalWrite(Reverse_RightPin, HIGH);
  }


}

Z

zhomeslice:
The below code should work well to control both H-Bridges

OP's already got code that runs the motor with the 0/1/2 input, and was just wanting to let it run for 5 seconds.

manor_royal:
OP's already got code that runs the motor with the 0/1/2 input, and was just wanting to let it run for 5 seconds.

Your code is missing enable pin state switching the high side on and off without enabling it won't start the motors
z

zhomeslice:
Your code is missing....

OP has code working already, and I suggested what he should look for in that code, and what he could add and where, to do what he wanted.

I have subsequently looked at the instructable he linked to, and my suggestion just slots in exactly as I had surmised. His existing code already does the high of the enable pin, and is working code. OP asked for a modification. I gave that.

Yes I already got code that runs the motor with the 0/1/2 input and just wanting to let it run for 5 seconds and stop until next command. Thanks

motorlock:
Yes I already got code that runs the motor with the 0/1/2 input and just wanting to let it run for 5 seconds and stop until next command. Thanks

So did you put my code in? Did it work?

Got this pm from op and post in spirit of this being a forum:

So may you modify the code and send it to me for upload?

I also uploaded the code you sent me and although that could run my dc motor two time click wise and two times counter clock wise according the delay you put there but this is not what I am looking for because I am looking or turning clock wise by dialing 1 and turning counter clock wise by dialing 2 but in both turn, the dc motor stop after 5000 miliseconds and this is what I do not know how to modify the code I already got form http://www.instructables.com/id/Arduino-Control-DC-Motor-via-Bluetooth/step2/Schematics-and-common-mistakes/

I assume "I also uploaded the code you sent me" means you slotted it in to the existing code? It does a 5s delay after a 1 or a 2, then turns it off. That's what you asked for I'm sure, or at least it's my understanding of what you asked for. It's still my understanding...

But in any case, as someone pointed out in your other thread, you need to be looking into fixing this yourself, not expecting bespoke solutions.

No more PMs please.

No. that was not what I was looking

I am looking for following code

1- by dialing 1 the dc motor turns to right and stop after 5 miliseconds

2- by dialing 2 the dc motor turns to left and stop after 5 miliseconds

3- We can see what the last command was in our screen to know if the motor is in left or right position

That is all with thanks

double posting is not good

1- by dialing 1 the dc motor turns to right and stop after 5 miliseconds

How far do you expect a motor to move in 1/200th of a second?

And yes, cross-posting is not good.

Duplicate deleted.