I am trying to make a object avoider for a friend who built a bot. I was successful with using bumpers but when I added the ping sensor nothing seems to work. Please check out my code and tell me what I am doing wrong. It seem logical to me.
I don't think there's anything wrong with enabling the pull-ups and then reading back, though you'd normally do the enable just once, in "setup ()". (assuming that's how the bumper switch is wired)
Oh I see what you mean. I put in the write statement for the buttonPin to enable the pull up resistor. My bumper switches are connected to GND pin. If I just use my switches the program works fine. When I added the code for the Ping sensor is when all goes wrong.
You're not using the Ping data yet, correct? Just reading it but not processing it?
The pulsein command has a total wait time of 3 minutes if it does not receive data properly. That could be either sensor line never transitions high, or transitions high and stays there.
So check the common issues that Ping is wired to power, sig is connected to Arduino D7. If all is good, try and scope it to see if it responds to your commands at all. Maybe dead sensor or pulled wire?
Check to make sure you have it wired as Ground, +5, Sig
I will try to comment it out and see if the bot moves again. I will also check the signals with a scope. I am looking for the ping to turn around the robot at about 12 inches. Is the duration variable set about right for this? Can I use a integer instead of a long variable to simplify the use of duration?
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
I commented out both lines and the robot started moving again. It acted like my if statement is always true and the light on the ping was flashing. I believe this is what should of happened. Next I commented out just the Duration = line and the bot stopped working. Is there something wrong with my pinmode line or????
Ok I put the Ping to a scope and all looks good. I changed the program so I could see what the ping is doing. I have found that the ping seems to work. I see the logic change the direction. What does not work is the motors do not work. Sometimes the motors will move super slow and you can sometimes here the motors trying to move but still no go. So what I am thinking now is I have some kind of power problem. I am using the motor shield with a separate power supply of 4 AA batteries to give me 6 volts. Is this not enough? Has any one else had this problem? I have included my current code just in cas some one sees something else I missed.
Thanks
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
const int pingPin = 7;
void setup() {
// initialize serial communication:
Serial.begin(9600);
motor1.setSpeed(250);
motor2.setSpeed(250);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// 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();
if(inches < 18)
{
Serial.print("Back ");
motor1.run(BACKWARD);
motor2.run(BACKWARD);
//delay(1000);
motor1.run(FORWARD);
motor2.run(BACKWARD);
//delay(1000);
}
else
{
Serial.print("Forward ");
motor1.run(FORWARD);
motor2.run(FORWARD);
}
delay(100);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
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;
}