obstacle avoiding bot with IR SHARP, 2 DC motors and micro servo not working

Hello,

I started to build bot with Arduino Uno R3, that uses 2 DC motors to drive around (with L293D IC), uses SHARP IR distance sensor (atached to micro servo) for finding obstacles and micro servo for pointing IR sensor to different directions for finding best way to continue driving.

Here is the code:

#include <DistanceGP2Y0A21YK.h>	//distance library
#include <Servo.h>		                                        //servo library

const int motor11Pin = 3;	// H-bridge pin 1A
const int motor12Pin = 5;	// H-bridge pin 2A
const int motor21Pin = 9;	// H-bridge pin 3A
const int motor22Pin = 10;	// H-bridge pin 4A
const int enablePin1 = 8;	// H-bridge enable pin motor1
const int enablePin2 = 12;	// H-bridge enable pin motor2

DistanceGP2Y0A21YK Dist;	//object Dist for measuring distance
int distance;	//distance ahead
int distanceL;	//distance left
int distanceD;	//distance right
Servo mojservo;	//servo object

void setup()
{
Serial.begin(9600); //opens communiaction
pinMode(motor11Pin, OUTPUT);	 //output for motor1
pinMode(motor12Pin, OUTPUT); //output for motor1
pinMode(motor21Pin, OUTPUT); //output for motor2
pinMode(motor22Pin, OUTPUT); //output for motor2
pinMode(enablePin1, OUTPUT); //output for enable pin1
pinMode(enablePin2, OUTPUT);	//output for enable pin2
digitalWrite(enablePin1, HIGH); //activate motor1
digitalWrite(enablePin2, HIGH); //activate motor2
Dist.begin(A0); //begin measuring distance
mojservo.attach(6);	 //attaches servo on pin6 on Arduino
mojservo.write(90); //turn servo to middle
}

void stop() //stop driving
{
digitalWrite(motor11Pin, LOW);
digitalWrite(motor12Pin, LOW);
digitalWrite(motor21Pin, LOW);
digitalWrite(motor22Pin, LOW);
}

void naprej() //drive forward
{
digitalWrite(motor11Pin, HIGH);
digitalWrite(motor12Pin, LOW);
digitalWrite(motor21Pin, HIGH);
digitalWrite(motor22Pin, LOW);
}

void nazaj() //drive backward
{
digitalWrite(motor11Pin, LOW);
digitalWrite(motor12Pin, HIGH);
digitalWrite(motor21Pin, LOW);
digitalWrite(motor22Pin, HIGH);
}

void desno() //turn right
{
digitalWrite(motor11Pin, HIGH);
digitalWrite(motor12Pin, LOW);
digitalWrite(motor21Pin, LOW);
digitalWrite(motor22Pin, HIGH);
delay(200);
}

void levo() //turn left
{
digitalWrite(motor11Pin, LOW);
digitalWrite(motor12Pin, HIGH);
digitalWrite(motor21Pin, HIGH);
digitalWrite(motor22Pin, LOW);
delay(200);
}

int razdaljaL() //measure left distance
{
mojservo.write(180);
delay(300);
Dist.begin(A0);
distanceL = Dist.getDistanceCentimeter();
delay(300);
return distanceL;
}

int razdaljaD() //measure right distance
{
mojservo.write(0);
delay(300);
Dist.begin(A0);
distanceD = Dist.getDistanceCentimeter();
delay(300);
return distanceD;
}

void loop()
{
mojservo.write(90);
delay(2000);
distance = Dist.getDistanceCentimeter();

if (distance<14) {
    stop();
    nazaj();
    stop();
    if (razdaljaL() > razdaljaD()) levo(); else desno();
}
  else {
    naprej();
  }
}

Picture of wiring is in the attachements!

And now my problems...

  • it seems that IR sensor isn't reading accurate distance even though is using library,
  • bot acts funky in a way that changeing code for PWM signals doesn't effect it's time of spinning wheels nor speed,
  • bot stops working when ever he wants to :smiley: , or even worse, doesn't start at all when I plug in both batteries, nor via USB and battery.

It's really confusing for me and I'm wondering if I did something wrong with wirring or the code? Help would be appriciated! :smiley:

THANKS! :smiley:

First write a simple sketch that gets the value from the IR distance sensor and prints it. That will let you test to se if the IR sensor is working properly. If not, you may need to get a new one or may have wired it incorrectly.

It's a little difficult to tell if every thing is hooked up correctly, so just a couple of suggestions.

  1. you should use a better battery for the motors, as those 9V bricks don't last very long, even for the Arduino board. I use series-wired NiMH AA-cells on my robots.

  2. you should never power an r/c servo from the Arduino 5V pin, as the voltage regulator won't adequately provide high enough currents, and powering any motors off the regulated 5V will add noise on the microprocessor Vcc.

  3. you'd be much better off buying a motor shield, then you can directly connect the servos and motors without a spaghetti wiring mess. Eg,

http://www.ebay.com/itm/L293D-Motor-Shield-Board-Arduino-MEGA-UNO-Duemilanove-Best-Quality-US-SELLER-/181174426234

  1. also, when it comes to writing software, you want to always use modular programming techniques, writing everything as functional modules that basically each perform just one function [eg, motor control], for which you have a good start.

  2. you want to test the various subsystems in "isolation" before putting the modules together . So make sure the motor control modules run the motors properly, and the sensor read modules output the correct data, etc, first. Then, put them together once you know each works correctly.

Hello,

thanks to both of you...actualy, I did modular programing and tested each subsystem independently. That's why I don't understand where is the catch - independetnly EVERYTHING works fine, no problems, no glich, nada. Everything is perfect when running solo. Also when I hoockup sensor and servo, they both perform as they should, or when I combine motor with servo or motors with sensor is just fine - they work flawlessly. But ALL together - disaster :smiley: ...as I mentioned above :smiley:

I'am powering motors from separate battery (9V) and Arduino from another 9V battery.

I'll try to use and do what you suggested (NiMh battery pack)...

Anyway...5 min ago, I tried to power Arduino through USB and motors via ATX power supply from one of my PC's...worked like a charm...I guess you were right when You sat, that baterries are no good...THANKS again!!!

With robots, power is always the first issue to look at. With electronics in general, hookup is always the first issue to look at.

Also, since motors and servos tend to generate a LOT of electrical noise, it's usually best to use separate batteries for the electronics and motor sections. Also, you should always have a good deal of filtering at the point where the motor battery connects to the h-bridge Vcc pin, eg a 100nF bypass cap, and 100uf [or higher] electrolytic.