Hi, I am new to making robots with Arduino and I am struggling to make my ultrasonic range finder work. I am trying to make a walking fish robot that will turn when it gets to close to a wall. However, it gives out a reading on the serial monitor, it always says an object is 0 cm away, even when I point it at the ceiling or put my hand a foot away from it:
Motor test!
Close Obstacle detected!
Obstacle Details:
Distance From Robot is 0 CM! DANGER WILL ROBINSON! THAT THING IS IN YOUR WAY!
Turning !
Close Obstacle detected!
Obstacle Details:
Distance From Robot is 0 CM! DANGER WILL ROBINSON! THAT THING IS IN YOUR WAY!
Turning !
Close Obstacle detected!
Obstacle Details:
Distance From Robot is 0 CM! DANGER WILL ROBINSON! THAT THING IS IN YOUR WAY!
Turning !
Close Obstacle detected!
Obstacle Details:
Distance From Robot is 0 CM! DANGER WILL ROBINSON! THAT THING IS IN YOUR WAY!
Turning !
I have attached a photo of my wiring for the range finder, as well as my code.
I would appreciate any help that I can get!
#include <Servo.h>
Servo myservoR;
Servo myservoL;
Servo myservoB;
// create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
const int trigPin = 8;
const int echoPin = 13 ;
void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
myservoL.attach(9);
myservoR.attach(10);
myservoB.attach(11);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" DANGER WILL ROBINSON! THAT THING IS IN YOUR WAY! ");
Serial.println (" Turning !");
for (pos = 90; pos >= 0; pos -= 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoL.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservoB.write(100);
delay(2000);
myservoB.write(90);
// Turn as long as there's an obstacle ahead.
}
else {
{
for (pos = 0; pos <= 90; pos += 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoR.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservoB.write(100);
delay(1100);
myservoB.write(90);
for (pos = 90; pos >= 0; pos -= 2) { // goes from 180 degrees to 0 degrees
myservoR.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservoL.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservoB.write(100);
delay(1100);
myservoB.write(90);
for (pos = 0; pos <= 90; pos += 2) { // goes from 180 degrees to 0 degrees
myservoL.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}}
Robotcottus.ino (2.99 KB)