Commit 6c6b91af authored by Jason Williams's avatar Jason Williams

Updated distance test to work with dual sensors.

parent 2034b8ba
// Connection pins
const int pin_trigger = D5;
const int pin_echo = D8;
const int pin_trigger1 = D3;
const int pin_echo1 = D4;
const int pin_trigger2 = D5;
const int pin_echo2 = D8;
// Variables
long duration;
......@@ -8,26 +10,43 @@ int distance;
void setup() {
Serial.begin(115200);
pinMode(pin_trigger, OUTPUT);
pinMode(pin_echo, INPUT);
pinMode(pin_trigger1, OUTPUT);
pinMode(pin_trigger2, OUTPUT);
pinMode(pin_echo1, INPUT);
pinMode(pin_echo1, INPUT);
}
void loop() {
// Clear the trigger
digitalWrite(pin_trigger, LOW);
// Clear the triggers
digitalWrite(pin_trigger1, LOW);
digitalWrite(pin_trigger2, LOW);
delayMicroseconds(2);
// Set the trigger pin high
digitalWrite(pin_trigger, HIGH);
// Get a reading for sensor 1
digitalWrite(pin_trigger1, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger, LOW);
digitalWrite(pin_trigger1, LOW);
// Read the echo pin and calculate distance
duration = pulseIn(pin_echo, HIGH);
duration = pulseIn(pin_echo1, HIGH);
distance = duration*0.034/2;
// Output over serial
Serial.print("Distance: ");
Serial.print("Distance 1: ");
Serial.print(distance);
Serial.println("cm");
// Get a reading for sensor 2
digitalWrite(pin_trigger2, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger2, LOW);
// Read the echo pin and calculate distance
duration = pulseIn(pin_echo2, HIGH);
distance = duration*0.034/2;
// Output over serial
Serial.print("Distance 2: ");
Serial.print(distance);
Serial.println("cm");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment