rescue robor

hello i am trying to connect two ultrasonic sensors srf05 to arduino and i wand the to compare distances to it will recocnises objects and walls also this is what i have done until now but i dont now how to get them work i can make one work but not both pliss can you help me 

#include <NewPing.h>

int motor_pin1 = 4;

int motor_pin2 = 5;

int motor_pin3 = 6;

int motor_pin4 = 7;

int dist = 0; 

#define TRIGGER_PIN1  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN1     9  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE1 20

#define TRIGGER_PIN2  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN2     9  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE2 30

NewPing sonar(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1);

NewPing radar(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);

void setup ()

{

  pinMode(motor_pin1,OUTPUT);

  pinMode(motor_pin2,OUTPUT);

  pinMode(motor_pin3,OUTPUT);

  pinMode(motor_pin4,OUTPUT);

 

  delay(700);

}

void loop()

{

  dist = sonar.ping();               //reads the sensor

 

  if(dist < MAX_DISTANCE1) {                         //if distance is less than 550

   forward();                                  //then move forward

  }

  if(dist >= MAX_DISTANCE1) {               //if distance is greater than or equal to 550

    findroute();

  }

}

 

void forward() {                            // use combination which works for you

   digitalWrite(motor_pin1,HIGH);

   digitalWrite(motor_pin2,LOW);

   digitalWrite(motor_pin3,HIGH);

   digitalWrite(motor_pin4,LOW);

   return;

 }

 

void findroute() {

  halt();                                             // stop

  backward();                                       //go backwards

  turnleft ();

 

 

}

 

void backward() 

{

  digitalWrite(motor_pin1,LOW);

  digitalWrite(motor_pin2,HIGH);

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,HIGH);

  delay(500);

  halt();

  return;

}

 

void halt () {

  digitalWrite(motor_pin1,LOW);

  digitalWrite(motor_pin2,LOW);

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,LOW);

  delay(500);                          //wait after stopping

  return;

}

 

 

 

void turnleft () {

  digitalWrite(motor_pin1,HIGH);       //use the combination which works for you

  digitalWrite(motor_pin2,LOW);      //right motor rotates forward and left motor backward

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,HIGH);

  delay(1000);                     // wait for the robot to make the turn

  halt();

  return;

}

You’re using the same

You’re using the same trigger/echo pins for both ultrasonic (US) modules, also

you declare one has sonar and the other as radar but you only use the values of ‘sonar’ in the loop() code;

— there are certainly some other issues with your code further ahead but this are the two that relate directly with the inability of one of US modules not working, you need to use it for it to work :stuck_out_tongue:

I also noticed the pinModes are set for the motors, but,

there is no indication of the US sensors having their pinMode set. Is that done in the NewPing lib?

sorry that was my old code

sorry that was my old code this is the code i wanted to post 

#include <Servo.h>                                  //includes the servo library

#include <NewPing.h>

int motor_pin1 = 4;

int motor_pin2 = 5;

int motor_pin3 = 6;

int motor_pin4 = 7;

int servopin = 8;

int sensorpin = 0;

int leftdist = 0;

int rightdist = 0;

int object = 5;             //distance at which the robot should look for another route                           

NewPing sonar1(11, 12, 200); // Sensor 1: trigger pin, echo pin, maximum distance in cm

NewPing sonar2(9, 10, 200);

Servo myservo;

#define pingSpeed 1000 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor

unsigned long pingTimer1, pingTimer2;

int in1, in2;

 

void setup ()

  pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 1 second (pingSpeed)

  pingTimer2 = pingTimer1 + 35; // Sensor 2 fires 35ms later

  pinMode(motor_pin1,OUTPUT);

  pinMode(motor_pin2,OUTPUT);

  pinMode(motor_pin3,OUTPUT);

  pinMode(motor_pin4,OUTPUT);

  myservo.attach(servopin);

  myservo.write(90);

  delay(700);

}

void loop(){

{if (millis() >= pingTimer1) {

    pingTimer1 += pingSpeed; // Make sensor 1 fire again 1 second later (pingSpeed)

    in1 = sonar1.ping_in();

  }

  if (millis() >= pingTimer2) {

    pingTimer2 = pingTimer1 + 35; // Make sensor 2 fire again 35ms after sensor 1 fires

    in2 = sonar2.ping_in();}}

 

if (in2  <  object) && (in1 < object)

{ forward();

}

if (in2  > object) && (in1 > object){

 findroute();

}

  //if (in2  < object) && (in1 > object)

 //grap():

}

 

void forward() {                            // use combination which works for you

   digitalWrite(motor_pin1,HIGH);

   digitalWrite(motor_pin2,LOW);

   digitalWrite(motor_pin3,HIGH);

   digitalWrite(motor_pin4,LOW);

   return;

 }

 

void findroute() {

  halt();                                             // stop

  backward();                                       //go backwards

  lookleft();                                      //go to subroutine lookleft

  lookright();                                   //go to subroutine lookright

 

  if ( leftdist < rightdist )

  {

    turnleft();

  }

 else

 {

   turnright ();

 }

}

 

void backward() {

  digitalWrite(motor_pin1,LOW);

  digitalWrite(motor_pin2,HIGH);

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,HIGH);

  delay(500);

  halt();

  return;

}

 

void halt () {

  digitalWrite(motor_pin1,LOW);

  digitalWrite(motor_pin2,LOW);

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,LOW);

  delay(500);                          //wait after stopping

  return;

}

 

void lookleft() {

  myservo.write(150);

  delay(700);                                //wait for the servo to get there

  leftdist= in2();

  myservo.write(90);

  delay(700);                                 //wait for the servo to get there

  return;

}

 

void lookright () {

  myservo.write(30);

  delay(700);                           //wait for the servo to get there

  rightdist = in2();

  myservo.write(90);                                  

  delay(700);                        //wait for the servo to get there

  return;

}

 

void turnleft () {

  digitalWrite(motor_pin1,HIGH);       //use the combination which works for you

  digitalWrite(motor_pin2,LOW);      //right motor rotates forward and left motor backward

  digitalWrite(motor_pin3,LOW);

  digitalWrite(motor_pin4,HIGH);

  delay(1000);                     // wait for the robot to make the turn

  halt();

  return;

}

 

void turnright () {

  digitalWrite(motor_pin1,LOW);       //use the combination which works for you

  digitalWrite(motor_pin2,HIGH);    //left motor rotates forward and right motor backward

  digitalWrite(motor_pin3,HIGH);

  digitalWrite(motor_pin4,LOW);

  delay(1000);                              // wait for the robot to make the turn

  halt();

  return;

}

void 

yes thats the new ping lib i

yes thats the new ping lib i am trying to declare the value from sensor1 as in1 and the value from sensor 2 as in 2 then by combaring the two distances with the distance i declare for an object to move forward or find another routeand later i am going to put when is an object to go and grap it 

You may have an issue in your lookleft/lookright functions.

You set the variables leftdist and rightdist to in2(); Unless in2() is a function in the NewPing lib, I don’t even know why your program would compile properly. Also, you have failed to answer whether or not the US Trigger pins are set as outputs in the NewPing lib. I would highly doubt that is the case.

new code

#include <Servo.h>                                  //includes the servo library
#include <NewPing.h>
int motor_pin1 = 4;
int motor_pin2 = 5;
int motor_pin3 = 6;
int motor_pin4 = 7;
int servopin = 8;
int dist1 = 0;
int dist2 = 0;
int leftdist = 0;
int rightdist = 0;
int object = 500;
#define TRIGGER_PIN2 10  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2    9
#define TRIGGER_PIN1  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1     11// Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

Servo myservo;

void setup ()
{
  pinMode(motor_pin1,OUTPUT);
  pinMode(motor_pin2,OUTPUT);
  pinMode(motor_pin3,OUTPUT);
  pinMode(motor_pin4,OUTPUT);
  myservo.attach(servopin);
  myservo.write(90);
  delay(700);
}
void loop()
{
  dist1 = sonar1.ping();               //reads the sensor
  dist2 = sonar2.ping();
 
  if(dist1 < object && dist2 < object)
  {                       
   forward();                                  //then move forward
  }
  if(dist1 >= object && dist2 > object ){               //if distance is greater than or equal to 550
    findroute();
  }
}

 
void forward() {                            // use combination which works for you
   digitalWrite(motor_pin1,HIGH);
   digitalWrite(motor_pin2,LOW);
   digitalWrite(motor_pin3,HIGH);
   digitalWrite(motor_pin4,LOW);
   return;
 }
 
void findroute() {
  halt();                                             // stop
  backward();                                       //go backwards
  lookleft();                                      //go to subroutine lookleft
  lookright();                                   //go to subroutine lookright
                                     
  if ( leftdist < rightdist )
  {
    turnleft();
  }
 else
 {
   turnright ();
 }
}

void backward() {
  digitalWrite(motor_pin1,LOW);
  digitalWrite(motor_pin2,HIGH);
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,HIGH);
  delay(500);
  halt();
  return;
}

void halt () {
  digitalWrite(motor_pin1,LOW);
  digitalWrite(motor_pin2,LOW);
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,LOW);
  delay(500);                          //wait after stopping
  return;
}
 
void lookleft() {
  myservo.write(150);
  delay(700);                                //wait for the servo to get there
  leftdist = sonar1.ping();
  myservo.write(90);
  delay(700);                                 //wait for the servo to get there
  return;
}

void lookright () {
  myservo.write(30);
  delay(700);                           //wait for the servo to get there
  rightdist = sonar1.ping();
  myservo.write(90);                                 
  delay(700);                        //wait for the servo to get there
  return;
}

void turnleft () {
  digitalWrite(motor_pin1,HIGH);       //use the combination which works for you
  digitalWrite(motor_pin2,LOW);      //right motor rotates forward and left motor backward
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,HIGH);
  delay(1000);                     // wait for the robot to make the turn
  halt();
  return;
}

void turnright () {
  digitalWrite(motor_pin1,LOW);       //use the combination which works for you
  digitalWrite(motor_pin2,HIGH);    //left motor rotates forward and right motor backward
  digitalWrite(motor_pin3,HIGH);
  digitalWrite(motor_pin4,LOW);
  delay(1000);                              // wait for the robot to make the turn
  halt();
  return;
}

i check what you talt me and yes in the library the US triger is an output but i made this code and now its combiling i havend test it yet on my robot but i think its going to work

your lookleft, and lookright function

you only check sonar1