/*
Anschluss:
VCC 1 und 2 and 5V (über Breadbord)
Sensor 1 trig und echo an 10 und 13 (oder andere)
Sensor 2 trig und echo an 5 und 6 (oder andere)
GND and GND
*/
#include <NewPing.h> // NewPing-Library einbinden, muss auch installiert sein
#define TRIGGER_PIN 10 // Hier überall beliebige Pins
#define ECHO_PIN 13 //
#define TRIGGER_PIN_2 5 //
#define ECHO_PIN_2 6 //
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
float distance, duration, distance2, duration2;
int iterations = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
duration = sonar.ping_median(iterations); // Sensor misst iterations-mal und nimmt den Durchschnitt
delay(100);
duration2 = sonar2.ping_median(iterations);
distance = (duration/2)* 0.0343;
distance2 = (duration2/2)* 0.0343;
Serial.print("Sensor 1: ");
if(distance >= 400 || distance <= 2) // Wenn mehr als 400cm oder 2cm wird außer Reichweite aus-
{ // gegeben, da das die Reichweite des Sensors ist.
Serial.print("Außer Reichweite");
}
else
{
Serial.print(distance);
Serial.print(" cm");
}
Serial.print(" Sensor 2: ");
if(distance2 >= 400 || distance2 <= 2 )
{
Serial.println("Außer Reichweite");
}
else
{
Serial.print(distance2);
Serial.println(" cm");
}
delay(100);
}