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