Hi guys,
This is my first post. Im just getting into the arduino world and decided to try to build a robot. I am using an arduino uno, a ping ultrasonic sensor, and two springrc continuous rotation servos for my wheels.
I have used a program to control the servos successfully. I have used a program to use the ping successfully. The problem develops when I integrate the two. When the servos and ping are both in the program, the ping stops reading and shows 0's in the serial monitor. If i disconnect the servos' signal wires from the arduino then the ping works again fine, but as soon as one is plugged in, it no longer functions properly. I am unsure if the problem is something electrical or a problem with my program, so I am turning to you guys for help! Here is my code, its not done but it should be enough to identify a problem in the code if one exists
const int pingPin = 11;
const int servoR = 9;
const int servoL = 10;
long duration, inches, cm;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
pinMode (servoR,OUTPUT);
pinMode (servoL,OUTPUT);
}
void go() {
if (inches > 2){
analogWrite(servoR, 170);
analogWrite(servoL, 170);
}
else
{
// BLAH BLAH havent worked out the turning routine yet but it will go here
}
}
void ping() {
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
}
void loop() {
ping();
go();
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
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;
}