ARDUINO SKETCH FOR MULTIPLE DISTANCE SENSORS



Code:


  
#include<NewPing.h>
#define TRIGGER_PIN A0
#define ECHO_PIN A1
#define TRIGGER_PIN2 A2
#define ECHO_PIN2 A3
#define TRIGGER_PIN3 A4
#define ECHO_PIN3 A5
#define TRIGGER_PIN4 7

#define ECHO_PIN4 6

#define MAX_DISTANCE 200

NewPing sonar1(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE);

NewPing sonar4(TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

}

void loop()
{

delay(50);

unsigned int uS1 = sonar1.ping();

pinMode(ECHO_PIN,OUTPUT);

digitalWrite(ECHO_PIN,LOW);

pinMode(ECHO_PIN,INPUT);

Serial.print("Ping1: ");

Serial.print(uS1 / US_ROUNDTRIP_CM);

Serial.println("cm");

delay(1000);

unsigned int uS2 = sonar2.ping();

pinMode(ECHO_PIN2,OUTPUT);

digitalWrite(ECHO_PIN2,LOW);

pinMode(ECHO_PIN2,INPUT);

Serial.print("Ping2: ");

Serial.print(uS2 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);

unsigned int uS3 = sonar3.ping();

pinMode(ECHO_PIN3,OUTPUT);

digitalWrite(ECHO_PIN3,LOW);

pinMode(ECHO_PIN3,INPUT);

Serial.print("Ping3: ");

Serial.print(uS3 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);
unsigned int uS4 = sonar4.ping();

pinMode(ECHO_PIN4,OUTPUT);

digitalWrite(ECHO_PIN4,LOW);

pinMode(ECHO_PIN4,INPUT);

Serial.print("Ping4: ");

Serial.print(uS4 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);
}> 
#define TRIGGER_PIN A0
#define ECHO_PIN A1
#define TRIGGER_PIN2 A2
#define ECHO_PIN2 A3
#define TRIGGER_PIN3 A4
#define ECHO_PIN3 A5
#define TRIGGER_PIN4 7

#define ECHO_PIN4 6

#define MAX_DISTANCE 200

NewPing sonar1(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE);

NewPing sonar4(TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

}

void loop()
{

delay(50);

unsigned int uS1 = sonar1.ping();

pinMode(ECHO_PIN,OUTPUT);

digitalWrite(ECHO_PIN,LOW);

pinMode(ECHO_PIN,INPUT);

Serial.print("Ping1: ");

Serial.print(uS1 / US_ROUNDTRIP_CM);

Serial.println("cm");

delay(1000);

unsigned int uS2 = sonar2.ping();

pinMode(ECHO_PIN2,OUTPUT);

digitalWrite(ECHO_PIN2,LOW);

pinMode(ECHO_PIN2,INPUT);

Serial.print("Ping2: ");

Serial.print(uS2 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);

unsigned int uS3 = sonar3.ping();

pinMode(ECHO_PIN3,OUTPUT);

digitalWrite(ECHO_PIN3,LOW);

pinMode(ECHO_PIN3,INPUT);

Serial.print("Ping3: ");

Serial.print(uS3 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);
unsigned int uS4 = sonar4.ping();

pinMode(ECHO_PIN4,OUTPUT);

digitalWrite(ECHO_PIN4,LOW);

pinMode(ECHO_PIN4,INPUT);

Serial.print("Ping4: ");

Serial.print(uS4 / US_ROUNDTRIP_CM);

Serial.println("cm");
delay(1000);
}
  

Comments

Popular posts from this blog

"Kernel Panic” error After upgrading Parrot OS

HACK HC-05(BLUETOOTH MODULE) FRIMWARE INTO HID FRIMWARE

HACK AND CLONE CAR KEY FOB, MY EPIC FAILURE...!!