I am a newbie in arduino programing and have been learning for a couple months now, I have already made a bluetooth car.... I am building a DIY arduino roomba but I would like it to have 3 ultrasonic sensors.
Here is the code for only 1 ultrasonic sensor
#include <Servo.h>
#define echoPin 7
#define trigPin 6
Servo leftservo;
Servo rightservo;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
leftservo.attach(9);
rightservo.attach(10);
}
void loop()
{
int distance,duration;
digitalWrite(trigPin, HIGH);
delay(200);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
Serial.println(“”);
Serial.println(distance);
if(distance >7) // for going forward
{
leftservo.write(360); // left wheel forward
rightservo.write(-360); // right wheel forward
}
else // obstacle detected turn left
{
leftservo.write(-360); //left wheel backward
rightservo.write(-360); // right wheel forward
}
}
If I want to add other ping sensors I just repeat the ultrasonic part and change the pin and wheel direction?
the 3 sensors will be mounted in 90*degree difference
any help would be greatly appreciated as I am a complete noobie.
Regards,
Julian