Hi, I am trying to get a robot to go forward until an object is detected within 5 inches. Then I want it to go backwards 10 inches from the object, then I want it to turn right. And finally, repeat the loop by going forward again and checking for an object within 5 inches. My robot just isn't moving in the correct manner. Is there an issue with my code?
Here is my code:
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
int ultraSoundSignal = 19; // assigning the PING sensor to Digitial Pin 19
unsigned long ultrasoundValue = 0;
unsigned long echo = 0;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(ultraSoundSignal,OUTPUT);
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
}
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}
void forward() // the forward function
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void backward() // the backward function
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void haltMotors() //the functon to stop the motors before reversing direction
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void turnRight() // the function to turn right
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void loop() {
ping();
while (ultrasoundValue > 5) //while robot is 5 inches away from object
{
forward(); //go forward
ping(); // update position
}
haltMotors(); //stop the motors before reversing direction
delay(3000);
while (ultrasoundValue < 10) //move robot back 10 inches before turning
{
backward();
ping(); //update position
}
turnRight();
delay(5000);
}
While you are telling us what the robot actually does versus what you want it to do, please explain why echo, ultrasoundValue, and ultraSoundSignal are global variables. If ping() returns a value, the value that it returns should be used, not ignored in favor of a global variable.
Consistency in variable names is a good thing. ultrasoundValue and ultraSoundSignal are not consistent. One has sound with a lower case s, the other has sound with an upper case s. In addition, the ultraSoundSignal contains a pin number, and has nothing to do with any signal.
void loop()
{
forward ();
while (ping () > 5);
haltMotors(); //stop the motors before reversing direction
delay(3000);
backward ();
while (ping () < 10) ; //move robot back 10 inches before turning
turnRight();
}
It looks the same as yours, but with superfluous stuff removed.
I found the code for this sensor on this Arduino site
It's pretty horrible (I would have parameterised the output pin number), and it's never really a good idea to return a global value, things could get confusing.