Let's Make Robots!

Reversing DC Motor

I have the 4WD Dagu 5 Rover with a Dagu 4 channel motor controller and I have got that working perfectly with an Arduino UNO and it runs very well.

I need to control 2 more DC motors however and so I went and got another motor controller and wired it the same using different outputs from my Arduino. I used very similar code for this DC motor as I did for the 4 wheels of the Rover.

HOWEVER, I can only seem to get the DC motor to rotate in 1 direction and not the other using "HIGH" and "LOW" outputs. The "High" output is the only one that the motor seems to respond to and if I use a "LOW" output the the motor does nothing.

Here is my code, if you see a problem then could you please let me know. It is in regards to the function "Lift_Forks" which does not seem to be responding.

//Car System-Motor controller 1

int pinPWM_fl = 3; //ch1

int pinPWM_bl = 5; //ch2

int pinPWM_fr = 6; //ch3

int pinPWM_br = 9; //ch4

 

int pinDIR_fl = 2; //ch1

int pinDIR_bl = 4; //ch2

int pinDIR_fr = 7; //ch3

int pinDIR_br = 8; //ch4

 

//Bridge System-Motor controller 2

int motPWM_f = 10; //ch1

int motPWM_b = 11; //ch2

 

int motDIR_f = 12; //ch1

int motDIR_b = 13; //ch2

 

void setup() {

  Serial.begin(9600);

  pinMode(pinPWM_fl,OUTPUT);

  pinMode(pinPWM_bl,OUTPUT);

  pinMode(pinPWM_fr,OUTPUT);

  pinMode(pinPWM_br,OUTPUT);

 

  pinMode(pinDIR_fl,OUTPUT);

  pinMode(pinDIR_bl,OUTPUT);

  pinMode(pinDIR_fr,OUTPUT);

  pinMode(pinDIR_br,OUTPUT);

 

  pinMode(motPWM_f,OUTPUT);

  pinMode(motPWM_b,OUTPUT);

 

  pinMode(motDIR_f,OUTPUT);

  pinMode(motDIR_b,OUTPUT); 

}

 

void loop() {

  delay(4000);

  Move_Forward(2500);

  Pause(1000);

  Turn_Right(720);

  Pause(1000);

  Move_Forward(1500);

  Pause(1000);

  Turn_Left(760);

  Pause(1000);

  Move_Forward(2200);

  Pause(1000);

  Put_Down(40);

  Pause(1000);

  Move_Backward(200);

  Pause(1000);

  Lift_Forks(1000);

  Pause(1000);

  Move_Forward(2200);

  Drop_Forks(x);

  Pause(3000);

  Move_Backward(200);

  Pick_Up(x)

  Pause(5000);

  Move_Forward(200);

  Pause(1000);

  Turn_Left(760);

  Pause(1000);

  Move_Forward(5000);

  Shut_Down();

  while(1) { }

}

 

void Move_Forward(int x) {

  digitalWrite(pinDIR_fl,LOW);

  digitalWrite(pinDIR_bl,HIGH);

  digitalWrite(pinDIR_fr,LOW);

  digitalWrite(pinDIR_br,HIGH);

  analogWrite(pinPWM_fl,255);

  analogWrite(pinPWM_bl,255);

  analogWrite(pinPWM_fr,180);

  analogWrite(pinPWM_br,180);

  delay(x);

}

 

void Pause(int x) {

  analogWrite(pinPWM_fl,0);

  analogWrite(pinPWM_bl,0);

  analogWrite(pinPWM_fr,0);

  analogWrite(pinPWM_br,0);

  delay(x);

}

 

void Turn_Right(int x) {

  digitalWrite(pinDIR_fl,LOW);

  digitalWrite(pinDIR_bl,HIGH);

  digitalWrite(pinDIR_fr,HIGH);

  digitalWrite(pinDIR_br,LOW);

  analogWrite(pinPWM_fl,255);

  analogWrite(pinPWM_bl,255);

  analogWrite(pinPWM_fr,255);

  analogWrite(pinPWM_br,255);

  delay(x);

}

 

void Turn_Left(int x) {

  digitalWrite(pinDIR_fl,HIGH);

  digitalWrite(pinDIR_bl,LOW);

  digitalWrite(pinDIR_fr,LOW);

  digitalWrite(pinDIR_br,HIGH);

  analogWrite(pinPWM_fl,255);

  analogWrite(pinPWM_bl,255);

  analogWrite(pinPWM_fr,255);

  analogWrite(pinPWM_br,255);

  delay(x);

}

 

void Move_Backward(int x) {

  digitalWrite(pinDIR_fl,HIGH);

  digitalWrite(pinDIR_bl,LOW);

  digitalWrite(pinDIR_fr,HIGH);

  digitalWrite(pinDIR_br,LOW);

  analogWrite(pinPWM_fl,255);

  analogWrite(pinPWM_bl,255);

  analogWrite(pinPWM_fr,180);

  analogWrite(pinPWM_br,180);

  delay(x);

}

 

void Shut_Down() {

  analogWrite(pinPWM_fl,0);

  analogWrite(pinPWM_bl,0);

  analogWrite(pinPWM_fr,0);

  analogWrite(pinPWM_br,0);

}

 

void Put_Down(int x) {

  digitalWrite(motPWM_f,HIGH);

  analogWrite(motDIR_f,255);

  delay(x);

}

 

void Lift_Forks(int x) {

  digitalWrite(motPWM_f,LOW);

  analogWrite(motDIR_f,255);

  delay(x);

}

 

void Drop_Forks(int x) {

  digitalWrite(motPWM_b,LOW);

  analogWrite(motDIR_b,255);

  delay(x);

}

 

void Pick_Up(int x) {

  digitalWrite(motPWM_b,HIGH);

  analogWrite(motDIR_b,255);

  delay(x);

}

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bdk6's picture

That might be pushing it just a wee bit, but I certainly understand how you feel.  I've been on the other side many, many times.  Glad you got it working.  Look forward to seeing your robot posted.

Thankyou.

I am glad I got it working too. I will post a video of it running soon, we have to competition for it in a couple of weeks: Australian/New Zealand Warman competition.

D

bdk6's picture

I really haven't dug into your code, but I notice in the last four functions, Put_Down(), Lift_Forks(), Drop_Forks(), and Pick_Up(), you digitalWrite to pins labeled PWM and analogWrite to pins labeled DIR.  Normally, and consistent with the rest of your code, a PWM pin should have an analogWrite and a direction pin should have a digitalWrite.  PWM (Pulse Width Modulation) is how analogWrite works and how motor speed is controlled.  Maybe you have those backwards?

You sir, are a god amongst men.

Thankyou very much, works perfectly now.