const byte numSensors = 3;
const byte ECHO_PIN[numSensors] = {2, 5, 8};
const byte TRIG_PIN[numSensors] = {3, 4, 7};
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
for (byte s=0; s<numSensors; s++) {
pinMode(TRIG_PIN[s], OUTPUT);
pinMode(ECHO_PIN[s], INPUT);
}
}
float readDistanceCM(byte s) {
digitalWrite(TRIG_PIN[s], LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN[s], HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN[s], LOW);
int duration = pulseIn(ECHO_PIN[s], HIGH);
return duration * 0.034 / 2;
}
void loop() {
ledVariable=map(vol,2,400,0,255);
bool isNearby = false;
Serial.print("Measured distance: ");
for (byte s=0; s<numSensors; s++) {
float distance = readDistanceCM(s);
isNearby |= distance < 100;
Serial.print(distance);
Serial.print(" ");
}
Serial.println();
digitalWrite(LED_BUILTIN, isNearby);
delay(100);
}