I am making a basic 2WD robot using Arduino Uno with motor shield. I am getting no compile errors - but something in my logic flow is not working as expected.

Expected Operation:

  1. Ping sensor does distance check.
  2. If distance is less than 5cm.
  3. Turn both Channel A & B motors half-speed for 1 second.
  4. Stop motors.
  5. Take new distance reading ping, start again.

What however is happening, is that channel A works as expected, but channel B motor keeps running?? Where is my code / logic mistake?

Later date I will add to else statement to turn one wheel to change direction.

 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 1000; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
  // put your setup code here, to run once:

Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)

  pinMode(12, OUTPUT); /* Motor Channel A RIGHT SIDE*/
  pinMode(9, OUTPUT); /* Motor Channel A */

  pinMode(13, OUTPUT); /* Motor Channel B LEFT SIDE */
  pinMode(8, OUTPUT);


void loop() {

  /* Distance check begins */

  digitalWrite(trigPin, LOW); 

  digitalWrite(trigPin, HIGH);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 /* End distance check */

 if (distance >= 5){
 /* Distance great enough to provide room to move */

  digitalWrite(LEDPin, HIGH); /* LED show movement */

  digitalWrite(12, LOW); /*move channel A motor half-speed */
  digitalWrite(9, LOW);
  analogWrite(3, 123);

  digitalWrite(13, HIGH); /*move channel B motor half-speed */
  digitalWrite(8, LOW);
  analogWrite(11, 123); 


  digitalWrite(9, HIGH); 
  digitalWrite(8, HIGH);

 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 digitalWrite(LEDPin, LOW); 

 //Delay 50ms before next reading.

  • 1
    You might add the pins for the motors to your #define statements. This way you could've easily spotted them having the same pin number. Some other points that might improve your code are: 1.Try increasing the baudrate Serial.begin(115200) in both the code and your terminal (should make it a little quicker).` 2. echoPin 7 // Echo Pin is just a too obvious comment. Try to use comments for a purpose. like //This pin receives the echo pulse 3. maximumRange should be an unsigned int it makes no sense to have negative ranges (and you can have a higher max range with unsigned). – Paul Apr 1 '16 at 9:28

I believe that your error is in that you declare LEDpin as pin 13 and then your motor B as pin 13 as well, that to me would result in errors when you write to the led in the else statement at the end of your code. I would recommend that you clean this up, don't use pins for different things. Try this first.

Your logic is ok in terms of functionality.

Also why is there inconsistency at this part on your code:

digitalWrite(12, LOW); /*move channel A motor half-speed */
  digitalWrite(9, LOW);
  analogWrite(3, 123);

  digitalWrite(13, HIGH); /*move channel B motor half-speed */
 digitalWrite(8, LOW);
  analogWrite(11, 123);

Why do you say pin 12 must beLOW and 13 HIGH when moving forward? Is this the way your un-mentioned circuit setup works, if possible could you add this info?

  • Thanks - think I missed the LED 13 bit. Reason for the inconsistency is that I motors are wired differently (ie by mistake), so to move fwd one needs a HIGH and other needs LOW to maintain direction. – Tex Apr 1 '16 at 7:11
  • @SvenHansen thanks for clarifying, try setting all things related to the motors to an "off" setting so when setting the brake pins also set the pwm pins to 0 – RSM Apr 1 '16 at 7:32
  • Indeed, I can still remember one of those controllers that uses 1-pin for CW or CCW rotation. Obviously one has to rotate CW and the other CCW to go forward (since the motors are 180 degree turned). @SvenHansen you should accept RSM's answer, to let the system (and others) know this question is answered succesfully. – Paul Apr 1 '16 at 9:24
  • @SvenHansen glad its working ;) – RSM Apr 1 '16 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.