Ultrasonic sensor help.

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. :stuck_out_tongue:

Regards,
Julian

?

you will need to sit down with a piece of paper and make a plan. The code you posted is a simple argument using one sensor. If you use the same argument for 3 sensors its going to start a fight.

Either look for a better 3 sensor steering robot code or start with a plan based on what you want it to do.

1/ forward sensor detects object
1a/ test side sensors
1b/ if right side detects object and left doesn't turn left
1c/ if left detects object and right doesn't turn right
1d/ if both detect object reverse a bit then retest (no sensor on rear so need to reverse same way you came)
2/ move based on above code

theres a lot of arguments to think of based on the 3 sensors.

you could hook up the 3 sensors and 3 leds then push the robot around. Make and write down decisions based on the 3 leds not on what you see. After 10 minutes of playing you should have a good understanding of what the code looks like.

so basically running this :

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
}
}

3 times but with different movements depending on the sensor would not work? (also I would add variables for the sensor)

gpop1:
you will need to sit down with a piece of paper and make a plan. The code you posted is a simple argument using one sensor. If you use the same argument for 3 sensors its going to start a fight.

Either look for a better 3 sensor steering robot code or start with a plan based on what you want it to do.

1/ forward sensor detects object
1a/ test side sensors
1b/ if right side detects object and left doesn't turn left
1c/ if left detects object and right doesn't turn right
1d/ if both detect object reverse a bit then retest (no sensor on rear so need to reverse same way you came)
2/ move based on above code

theres a lot of arguments to think of based on the 3 sensors.

you could hook up the 3 sensors and 3 leds then push the robot around. Make and write down decisions based on the 3 leds not on what you see. After 10 minutes of playing you should have a good understanding of what the code looks like.

?

Julianfer5:
so basically running this :

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
}
}

3 times but with different movements depending on the sensor would not work? (also I would add variables for the sensor)

what would happen if 2 sensors detect the same object. First the code would tell it to do something then tell it to do something else. If it gets in a corner it will probably just sit there moving left to right as the code has no way to handle such a situation as the sensors are not acting together but are acting as 3 separate codes linked to the same wheels.
The code up till the "if" can be repeated as long as the names are changed for each sensor

gpop1:
what would happen if 2 sensors detect the same object. First the code would tell it to do something then tell it to do something else. If it gets in a corner it will probably just sit there moving left to right as the code has no way to handle such a situation as the sensors are not acting together but are acting as 3 separate codes linked to the same wheels.
The code up till the "if" can be repeated as long as the names are changed for each sensor

what if I code it so when it detects objects on both sides it goes back?

would this work

#include <Servo.h>
#define echoPin 7
#define trigPin 6
#define echoPin5 5
#define trigPin4 4
#define echoPin3 3
#define trigPin2 2
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
}

void loop()
{
int distance,duration;
digitalWrite(trigPin5, HIGH);
delay(200);
digitalWrite(trigPin5, LOW);
duration=pulseIn(echoPin4, 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 right
{
leftservo.write(360); //left wheel backward
rightservo.write(360); // right wheel forward
}

void loop()
{
int distance,duration;
digitalWrite(trigPin2, HIGH);
delay(200);
digitalWrite(trigPin2, LOW);
duration=pulseIn(echoPin3, 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
}
}

Welcome to the Forum. Your code examples are not posted properly. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code looklike thiswhen posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your messages and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.

aarg:
Welcome to the Forum. Your code examples are not posted properly. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code looklike thiswhen posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your messages and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.

Thanks for the help mate!

Can you help with the code I posted above your post?

Kind Regards,
Julian

you can only have one void loop it can call other loops called functions

so no the code will not compile or work

stitched the codes together but no idea how to write the logic with out testing

#include <Servo.h>
#define FrontEchoPin 7
#define FrontTrigPin 6
#define rightEchoPin 5
#define rightTrigPin 4
#define leftEchoPin 3
#define leftTrigPin 2
Servo leftservo;
Servo rightservo;
void setup()


{
  Serial.begin(9600);
  pinMode(FrontTrigPin, OUTPUT);
  pinMode(FrontEchoPin, INPUT);
  pinMode(rightTrigPin, OUTPUT);
  pinMode(rightEchoPin, INPUT);
  pinMode(leftTrigPin, OUTPUT);
  pinMode(leftEchoPin, INPUT);


  leftservo.attach(9);
  rightservo.attach(10);
}

void loop()
{
  int frontDistance, frontDuration;
  int rightDistance, rightDuration;
  int leftDistance, leftDuration;
  digitalWrite(FrontTrigPin, HIGH);
  delay(200);
  digitalWrite(FrontTrigPin, LOW);
  frontDuration = pulseIn(FrontEchoPin, HIGH);
  frontDistance = (frontDuration / 2) / 29.1;


  digitalWrite(rightTrigPin, HIGH);
  delay(200);
  digitalWrite(rightTrigPin, LOW);
  rightDuration = pulseIn(rightEchoPin, HIGH);
  rightDistance = (rightDuration / 2) / 29.1;


  digitalWrite(leftTrigPin, HIGH);
  delay(200);
  digitalWrite(leftTrigPin, LOW);
  leftDuration = pulseIn(leftEchoPin, HIGH);
  leftDistance = (leftDuration / 2) / 29.1;

  Serial.print("left: ");
  Serial.print(leftDistance);
  Serial.print(" front: ");
  Serial.print(frontDistance);
  Serial.print(" right: ");
  Serial.println(rightDistance);

// no idea how to write this logic
// a lot of "if" "and" "or" will be required

  if (frontDistance > 7) // for going forward
  {
    if (rightDistance > 7) {
      leftservo.write(360); // left wheel forward
      rightservo.write(-360); // right wheel forward
    }
    if (leftDistance > 7) {
      leftservo.write(-360); //left wheel backward
      rightservo.write(-360); // right wheel forward
    }
  } else {
    //reverse
  }
}

gpop1:
stitched the codes together but no idea how to write the logic with out testing

#include <Servo.h>

#define FrontEchoPin 7
#define FrontTrigPin 6
#define rightEchoPin 5
#define rightTrigPin 4
#define leftEchoPin 3
#define leftTrigPin 2
Servo leftservo;
Servo rightservo;
void setup()

{
  Serial.begin(9600);
  pinMode(FrontTrigPin, OUTPUT);
  pinMode(FrontEchoPin, INPUT);
  pinMode(rightTrigPin, OUTPUT);
  pinMode(rightEchoPin, INPUT);
  pinMode(leftTrigPin, OUTPUT);
  pinMode(leftEchoPin, INPUT);

leftservo.attach(9);
  rightservo.attach(10);
}

void loop()
{
  int frontDistance, frontDuration;
  int rightDistance, rightDuration;
  int leftDistance, leftDuration;
  digitalWrite(FrontTrigPin, HIGH);
  delay(200);
  digitalWrite(FrontTrigPin, LOW);
  frontDuration = pulseIn(FrontEchoPin, HIGH);
  frontDistance = (frontDuration / 2) / 29.1;

digitalWrite(rightTrigPin, HIGH);
  delay(200);
  digitalWrite(rightTrigPin, LOW);
  rightDuration = pulseIn(rightEchoPin, HIGH);
  rightDistance = (rightDuration / 2) / 29.1;

digitalWrite(leftTrigPin, HIGH);
  delay(200);
  digitalWrite(leftTrigPin, LOW);
  leftDuration = pulseIn(leftEchoPin, HIGH);
  leftDistance = (leftDuration / 2) / 29.1;

Serial.print("left: ");
  Serial.print(leftDistance);
  Serial.print(" front: ");
  Serial.print(frontDistance);
  Serial.print(" right: ");
  Serial.println(rightDistance);

// no idea how to write this logic
// a lot of "if" "and" "or" will be required

if (frontDistance > 7) // for going forward
  {
    if (rightDistance > 7) {
      leftservo.write(360); // left wheel forward
      rightservo.write(-360); // right wheel forward
    }
    if (leftDistance > 7) {
      leftservo.write(-360); //left wheel backward
      rightservo.write(-360); // right wheel forward
    }
  } else {
    //reverse
  }
}

Thanks mate, In about a week or so I will tell you if the code worked. I still have not finished printing the base of the robot. (It will be a DIY Roomba).