arduino uno with HC SR04 and servos project

Hello everybody,
I'm building a robot with 3 servos (1 for the right wheel, 1 for the left wheel, and 1 for the sensor). I have built everything , but I've got a problem with the program. I have tried to make programs, but it doesn't work, sometimes, the robot turn around, and sometimes, it simply doesn't work. If someone could give a program, I would be really happy

Thanks

Hi,
I like many of us have done this and I could just send you the code! But you're not going to learn anything this way.

So we may help you, we need a schematic even if hand drawn, and to see what code your using, because my chrystal ball needs recharging and I can't KNOW what you mean.

Regards

Mel.

You need to supply the programming, how the servos are powered, mechanical setup etc.., for anybody to supply any idea of what the issue might be.

thanks for your replys, I made the arduino code this afternoon, and, It works pretty well, but I think I have a problem, because my right servo doesn't works the robot is still turning around. I tested it with a servo tester, and It works. I don't know where does the problem comes from.

Thx

Subaru:
I don't know where does the problem comes from.

Well, as zoomkat said:

zoomkat:
You need to supply the programming, how the servos are powered, mechanical setup etc

Until you do that, we have even less idea of where the problem comes from than you do.

Hi,
here is the program :

#include <Servo.h> //Inclue Servo Library
#include <NewPing.h> //Include NewPing Library

Servo leftServo; //Create Left Servo object
Servo rightServo; //Create Right Servo object

#define TRIGGER_PIN 6 //Trigger pin of Ultrasonic sensor connected to pin 6
#define ECHO_PIN 7 //Echo pin of Ultrasonic sensor connected to pin 7
#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic sensor object

unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back
int distance; //Variable to store the distance calculated from the sensor
int triggerDistance = 30; //The distance we want the robot to look for a new path
int fDistance; //Variable to store the distance in front of the robot
int lDistance; //Variable to store the distance on the left side of the robot
int rDistance; //Variable to store the distance on the right side of the robot

void setup()
{
leftServo.attach(9); //Left servo connected to pin 9
leftServo.write(90); //Write the neutral position to that servo
rightServo.attach(8); //Right servo connected to pin 8
rightServo.write(90); //Write the neutral position to that servo
}

void loop()
{
scan(); //Get the distance retrieved
fDistance = distance; //Set that distance to the front distance
if(fDistance < triggerDistance){ //If there is something closer than 30cm in front of us
moveBackward(); //Move Backward for a second
delay(1000);
moveRight(); //Turn Right for half a second
delay(500);
moveStop(); //Stop
scan(); //Take a reading
rDistance = distance; //Store that to the distance on the right side
moveLeft();
delay(1000); //Turn left for a second
moveStop(); //Stop
scan(); //Take a reading
lDistance = distance; //Store that to the distance on the left side
if(lDistance < rDistance){ //If the distance on the left is smaller than that of the right
moveRight(); //Move right for a second
delay(1000);
moveForward(); //Then move forward
}
else{
moveForward(); //If the left side is larger than the right side move forward
}
}
else{
moveForward(); //If there is nothing infront of the robot move forward
}
}

void scan(){
time = sonar.ping(); //Send out a ping and store the time it took for it to come back in the time variable
distance = time / US_ROUNDTRIP_CM; //Convert that time into a distance
if(distance == 0){ //If no ping was recieved
distance = 100; //Set the distance to max
}
delay(10);
}

void moveBackward(){
rightServo.write(180);
leftServo.write(0);
}

void moveForward(){
rightServo.write(0);
leftServo.write(180);
}

void moveRight(){
rightServo.write(0);
leftServo.write(0);
}

void moveLeft(){
rightServo.write(180);
leftServo.write(180);
}

void moveStop(){
rightServo.write(90);
leftServo.write(95);
}

So, Left servo is attached to pin 9 and GND and +5V, Right is attached to Pin 8, GND and +5V, echo is attached to pin 7 and trigger pin 6. then, VCC is attached to +5V and GND to GND^^That’s all.

Thx

Hi,
I had this problem and it could be to do with what TIMER uses which pins, 2 timers are 8 bit, one is 16bit, put the U/S sensor servo on the 16bit one. Sorry I don't have that info in front of me, you'll have to look it up!!

Here's the U/S code I use, works very well:

long scanner(long cm)
{
	const int pingPin=7, EchoPin=8;
	long duration;

	// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
	// Give a short LOW pulse before to ensure a clean HIGH pulse:
	pinMode(pingPin, OUTPUT);
	pinMode(EchoPin, INPUT);  

	digitalWrite(pingPin, LOW);
	delayMicroseconds(2);
	digitalWrite(pingPin, HIGH);
	delayMicroseconds(2);
	digitalWrite(pingPin, LOW); 
	duration = pulseIn(EchoPin, HIGH);
	delay(100);

	// convert the time into a distance
	// inches = microsecondsToInches(duration);
	cm = microsecondsToCentimeters(duration);
	return (cm);
}
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
	// The speed of sound is 340 m/s or 29 microseconds per centimeter.
	// The ping travels out and back, so to find the distance of the
	// object we take half of the distance travelled.
	return microseconds / 29 / 2;
}
//------------------------------------------------------------------

It does'nt look much like yours....

Hope it helps
Regards

Mel.

Subaru:
So, Left servo is attached to pin 9 and GND and +5V, Right is attached to Pin 8, GND and +5V,

When you say 5V, you mean the 5V on the Arduino? You should provide external power like in my pic below.

And post code in code tags so it doesn’t get smilies in the middle.

servo power.png

Hi Subaru,

I had a similar problem.

Its reason was a bad power supply. You might add some Capacitors to the Servos. These can be put on the Breadboard, if available.

Another possibility would be adding an external power supply, of course.

Best wishes

Why are you using servos instead of DC's for this?

goacego:
Why are you using servos instead of DC's for this?

Hi goacego,
I can't speak for Subaru, but I used continious rotation servos (not normal RC servos) as wheel drivers, as you need no special motor driver chips, and you only need one signal/pin to drive it, you vary the speed and direction, with a digitalWrite(servo,XX) with hopefully 90 being the dead point where the servo is still (most have pot to trim this), 90+ will go one way, lets say CW, 90- is CWW, speed increases the further you get from 90! (0-180).

Could be:

dubbidub:
Hi Subaru,

Its reason was a bad power supply. You might add some Capacitors to the Servos. These can be put on the Breadboard, if available.

Another possibility would be adding an external power supply, of course.

It's worth a try, we all have different set ups and ways of wiring, etc..

Regards

Mel.