2 Ultrasonic sensors

Interfacing 2 Ultrasonic Sensors
Interfacing 2 ultrasonic sensors is same as as interfacing ultrasonic sensor but there's a slight change in the code. We'll have give a identity to the ultrasonic sensor as 1 and another as 2, so that the computer can identify to which ultrasonic sensor we are giving the condition(s).

Click here if you want to visit the page of interfacing one Ultrasonic sensor 

Code
#include <NewPing.h>
int y = 0 ;
int x = 0 ;
#define ECHO_PIN1 3
#define TRIGGER_PIN1 2
#define MAX_DISTANCE1 500// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1);
#define ECHO_PIN2 9
#define TRIGGER_PIN2 8
#define MAX_DISTANCE2 500// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);
void setup()
{
  Serial.begin(9600);
 x = sonar1.ping_cm() ;
 y = sonar2.ping_cm() ;
 }
void loop()
x = sonar1.ping_cm() ;
y = sonar2.ping_cm() ;
Serial.println(x);
Serial.println(y);
}

No comments:

Post a Comment