First Code giving erratic results . Think I'm missing a puzzle piece

hi everyone.

I’m fresh to all of this and this is my first project. I’m a noob at everything from programming to circuits so its been a steep learning curve but i’m getting there… I think. :slight_smile:

I’ve just put together my obstacle avoiding robot. bit of a DIY job but it works (mechanically).

I’ve spent the day writing some code for it. i basically put the hole code together from scratch from what I’d learned today and from examples online. I uploaded the code and it verified ok but my robot seems to be doing some random and not so smooth movements.

first , one motor seems to be getting more power than the other. and even though i think i wrote the code for both wheels to go forward it seem that they are spinning in different directions. (which could mean its not resetting from the obstacle detected actions). my guess is ive missed a line of crucial code somehwere.
can i get some tips on how to improve on what code I’ve wrote and if I’m doing it right?
I’m running on a tamiya twin motor gear box, arduino uno r3 and an r3 arduino motor shield.
thanks in advance for the help!

this is everything on my code

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

#define trigPin 4 // change to 4!!!//
#define echoPin 8 // change to pin 8 on robot to avoid pin clash!!! //
#define pwmA 3 // motor on //
#define pwmB 11 // motor on //
#define brakeA 9
#define brakeB 8
#define dirA 12 //motor control speed pin //
#define dirB 13 //motor control speed pin//

void setup() {
Serial.begin(9600);
pinMode(dirA, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)

}

void loop() {

Serial.begin(9600);
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);

delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // convert the distance to centimeters.
if (distance > 25) {

delayMicroseconds(1000);
digitalWrite(dirA, LOW); // motor A setup / changing all highs to lows//
digitalWrite(brakeA, LOW);
analogWrite(pwmA, 150);
digitalWrite(dirB, LOW); //motor B setup//
digitalWrite(brakeB, LOW);
analogWrite(pwmB, 150);
}

else if (distance < 25);
{
/*if there’s an obstacle 25 centimers, ahead, do the following: */
delayMicroseconds(400);
digitalWrite(dirA, HIGH); // motor A setup//
digitalWrite(brakeA, LOW);
analogWrite(pwmA, 150);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmB, 150);
}

}

aslo i have a separate power supply for both the arduino and motors yet the motor battery connected to the shield powers the arduino too when its power supply is off. is this normal? Another stupid thing is how would you go about adding some code so that it doesn't move around when your connected to the computer via a usb lead. its running away all the time ....

Edit your post to put your code in code tags.

It is a strange thing but when you drive your car forward one wheel goes round clock wise and the other anti clock wise.

You can fix it in code but I find the best way is to switch over the wires to the motor on one side of the bot.

Mark

else if (distance < 25);

What do you suppose that that ; there is doing?

@robotrabbithole: Welcome to the Forum. Please read the two posts by Nick Gammon at the top of this Forum on posting guidelines. Also, use Ctrl-T on your code in the IDE before using code tags to post it here. It will reformat your code and make some errors more obvious, like the one pointed out by Paul.

You can’t use Pin 8 for both ‘echo’ (input) and ‘dirB’ (output).

You should avoid the use of #define in place of ‘const’ variables and certainly not use both with the same name in the same sketch!

const int dirA = 12;
const int pwmA = 3;
const int brakeA = 9;

const int dirB = 13;
const int pwmB = 11;
const int brakeB = 8;

const int trigPin = 4;
const int echoPin = 5;

void setup() {
  Serial.begin(9600);

  analogWrite(pwmA, 0); // Stop the motor
  analogWrite(pwmB, 0); // Stop the motor

  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);

  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW); // Turn off the brakes
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW); // Turn off the brakes

  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
}

void loop() {
  long duration, distance;
  // Check the distance sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH, 29100);  // Timeout at 500 cm

  if (duration != 0) { // An object is detected
    distance = (duration / 2) / 29.1; // convert the distance to centimeters.
    if (distance > 25) {
      // Move forward
      digitalWrite(dirA, LOW);
      digitalWrite(dirB, LOW);
    } else {  // distance <= 25
      // Turn
      digitalWrite(dirA, HIGH);
      digitalWrite(dirB, LOW);
    }
    analogWrite(pwmA, 150);
    analogWrite(pwmB, 150);
  }
}

thanks for the help everyone. sorry about the wrong format on this post. great to have a community to help :slight_smile:

The motors are handed. If the red lead is connect to positive
on one motor I suspect the other should have the black lead connected.
You should not switch the ground.
Dwight

@John:

You should avoid the use of #define in place of 'const' variables

Why to you feel this way?

#define is always global while const is within
the current scope. When adding code later, this can avoid
name conflicts.
#define should really be used for compile time options.
Dwight

dwightthinker:
#define is always global while const is within
the current scope. When adding code later, this can avoid
name conflicts.
#define should really be used for compile time options.
Dwight

...and you cannot build a macro using const...

I believe "const" is in same category / class as function prototype - it does not add much to code logic but it is like your mother checking on you once awhile!

I believe "const" is in same category / class as function prototype - it does not add much to code logic

More evidence that you don't know what you are talking about.

"It's better to keep quiet and be thought an idiot than to speak up and remove all doubt."