Issue with Arduino Uno/Motor Shield r3 - Code or faulty power connection or both?

roverRobot.jpg (64991Bytes)

First post and second robot build (first was the Build your first robot from the Popular Mechanics magazine).  I am having an issue and seeing if I can get some assistance.

When not plugged in, the robot sometimes moves, jerks or just sits there. I noted that times the battery connection to the Motor Shield fell out, but I have re-inserted for testing - I will be giving it a bigger wire to fit in the shield. There are other times, when it has power, it jerks as it if is doing something then nothing, or jerk - stop, jerk-stop. I dont know if it may be code or the power or both.

 

My robot is built using the following (using the Clusterbot as a influence: letsmakerobots.com/node/30832):

 - Erector set for chassis.

 - Arduino Uno

 - Arduino Motor Shield r3

 - HC-SR04 Distance Sensor - using the NewPing library

 - Tamiya Twin Motor gearbox

 - Radio Shack 9V connector (270-0324) & Plug (274-1569) to power the Uno

 - Radio Shack 4-AA battery compartment (270-0391)

 

Code is below - you can note I am also checking for ping that is not 0. In the serial monitor, it would show as 0 when there was nothing in front of the sensor - I am attributing it to a software bug or dust/hair.

#include 

#define SENSOR_TRIG_PIN 7
#define SENSOR_ECHO_PIN 6
#define SENSOR_MIN_DIST 10
#define SENSOR_MAX_DIST 200

#define MOTOR_CH_A 12
#define BRAKE_CH_A 9
#define SPEED_CH_A 3

#define MOTOR_CH_B 13
#define BRAKE_CH_B 8
#define SPEED_CH_B 11

#define HI_SPEED 175
#define LO_SPEED 75

NewPing sonar(SENSOR_TRIG_PIN, SENSOR_ECHO_PIN, SENSOR_MAX_DIST);

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

pinMode(MOTOR_CH_A, OUTPUT);
pinMode(BRAKE_CH_A, OUTPUT);
pinMode(SPEED_CH_A, OUTPUT);

pinMode(MOTOR_CH_B, OUTPUT);
pinMode(BRAKE_CH_B, OUTPUT);
pinMode(SPEED_CH_B, OUTPUT);

}

void loop() {
delay(50);
int ping = sonar.ping_cm();
Serial.println(ping);

go_forward();
if ( ping <= SENSOR_MIN_DIST ) {
	if ( ping != 0 ) {
		brake();
			delay(20);
		go_backwards();
			delay(150);
		brake();
			delay(20);
		turn_right();
			delay(20);
	}
}

}

void go_forward() {
Serial.println(“Going forward…”);

digitalWrite(MOTOR_CH_A, HIGH);
digitalWrite(BRAKE_CH_A, LOW);
analogWrite(SPEED_CH_A, LO_SPEED);

digitalWrite(MOTOR_CH_B, HIGH);
digitalWrite(BRAKE_CH_B, LOW);
analogWrite(SPEED_CH_B, LO_SPEED);

}

void go_backwards() {
Serial.println(“Going back…”);

digitalWrite(MOTOR_CH_A, LOW);
digitalWrite(BRAKE_CH_A, LOW);
analogWrite(SPEED_CH_A, LO_SPEED);

digitalWrite(MOTOR_CH_B, LOW);
digitalWrite(BRAKE_CH_B, LOW);
analogWrite(SPEED_CH_B, LO_SPEED);

}

void turn_left() {
Serial.println(“Going left…”);

digitalWrite(MOTOR_CH_A, LOW);
digitalWrite(BRAKE_CH_A, HIGH);
analogWrite(SPEED_CH_A, LO_SPEED);

digitalWrite(MOTOR_CH_B, HIGH);
digitalWrite(BRAKE_CH_B, LOW);
analogWrite(SPEED_CH_B, HI_SPEED);	

}

void turn_right() {
Serial.println(“Going right…”);

digitalWrite(MOTOR_CH_A, HIGH);
digitalWrite(BRAKE_CH_A, LOW);
analogWrite(SPEED_CH_A, HI_SPEED);

digitalWrite(MOTOR_CH_B, LOW);
digitalWrite(BRAKE_CH_B, HIGH);
analogWrite(SPEED_CH_B, LO_SPEED);	

}

void brake() {
Serial.println(“Braking…”);

digitalWrite(BRAKE_CH_A, HIGH);
digitalWrite(BRAKE_CH_B, HIGH);

}

Any help is greatly appreciated. A picture of the robot is included in the attachment.

Looks good to me

This actaually looks like good, clean solid code. Nothing jumps out at me --certainly not something that would keep your motors from going --all your fwd/rev/turn routines look fine.

Yeah, I would say to rework your solder joints and check all the basics --V1 / v2 both getting power etc. This one feels like one dirty connection or one very simply thing (right in front of you) that your eyes are just not seeing.

 

One unrelated note  --Get your goForward() command AFTER your if(sonar is too close) command. --Right now, no matter if you are needing to avoid an obstacle or not, your motors are commanded to go forward each time through the loop. Let’s say you are too close to something. We fire the sonar, then set the motors to forward, then (within a few micro seconds) we get into the if(too close) routine and imediately change the motor direction. It would be better to put this goForward() AFTER your if(tooClose) routine. If you are not too close, we skip past and get straight to goForward(). If we do avoid an obstacle, we don’t have to slam our bot into forward for a split-second before we do. 

Oh, and get that delay(50) outta your first line there --no need to have it.

 

Great little bot, dude. Good clean code. Nice work.

I agree with CtC’s comments.

Another thing that might be helpful are some clear pictures.

Are your grounds connected? Like Chris said, the code looks pretty good, and, to me your issues originally sounded power related.

@Chris the Carpenter -

@Chris the Carpenter - Thanks for the code suggestion, I will try it out.

@birdmun, @Chris the Carpenter Thanks, pics are below.  I am figuring the same with the power.

 

More Pics - I apologize, imageshack resized them down.   Power is not connected to the boards.

http://imageshack.us/a/img195/2159/roverbotbackbattery.jpg

http://imageshack.us/a/img542/6753/roverbotbottomview.jpg

http://imageshack.us/a/img685/7287/roverbotleftside.jpg

http://imageshack.us/a/img405/9675/roverbotlowview.jpg

http://imageshack.us/a/img838/1581/roverbotohrightside.jpg

http://imageshack.us/a/img152/9430/roverbotohfront.jpg

My question still stands.

Are you connecting the two separate power supply grounds together? From what I can see on the pics they are not connected anywhere.

No, for the picture, I

No, for the picture, I unplugged the power so it wouldn’t move.  The lose wires are for the VIN/Ground (red/black respectively).