int j [3] = {2, 4, 6}; //trig pins
int k [3] = {3, 5, 7}; // echo pins
# define led 10
# define rele 11
# define buzzer 12
int buzzerState = 0;
int distance0;
int distance1 ;
int distance2 ;
void setup()
{
Serial.begin(115200);
// put your setup code here, to run once:
for (int trigPin : j)
{
pinMode (trigPin, OUTPUT );
}
for (int echoPin : k)
{
pinMode(echoPin, INPUT);
}
pinMode(led, OUTPUT);
pinMode(rele, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop () {
distanceUpdate();
// check distance, set LED and relay and maybe launch a tone burst
if ((distance0 < 4 || distance1 < 4 || distance2 < 4 ) && buzzerState==0)
{
// if (buzzerState == 0 ) {
digitalWrite(led, HIGH);
digitalWrite(rele, LOW);
buzzerState = 1; // no more tone until we've seen the distnace go > 10
for (int i = 0; i < 5; i++) {
distanceUpdate();
if (distance0 > 9 && distance1 > 9 && distance2 > 9 ) {
break;
}
tone(buzzer, 2500); // hearing this better at 1000 Hz
delay(200);
noTone(buzzer);
delay(200);
}
}
if (distance0 > 9 && distance1 > 9 && distance2 > 9 )
{
// noTone(buzzer); would immediately stop buzzer
buzzerState = 0; // allow again the tone to trigger on
Serial.print("buzzerState:");
Serial.println(buzzerState);
digitalWrite(led, LOW);
digitalWrite(rele, HIGH);
}
}
int distanceUpdate() {
distance0 = getEcho(2, 3);
distance1 = getEcho(4, 5);
distance2 = getEcho(6, 7);
Serial.println(distance0);
Serial.println(distance1);
Serial.println(distance2);
}
long getEcho(int trigPin, int echoPin)
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
return distance;
}
/*
int j [3] = {2, 4, 6}; //trig pins
int k [3] = {3, 5, 7}; // echo pins
# define led 10
# define rele 11
# define buzzer 12
int buzzerState = 0;
int distance0;
int distance1 ;
int distance2 ;
void setup()
{
Serial.begin(115200);
// put your setup code here, to run once:
for (int trigPin : j)
{
pinMode (trigPin, OUTPUT );
}
for (int echoPin : k)
{
pinMode(echoPin, INPUT);
}
pinMode(led, OUTPUT);
pinMode(rele, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop()
{ delay(500);
long distance0, distance1, distance2;
distance0 = getEcho(2, 3);
distance1 = getEcho(4, 5);
distance2 = getEcho(6, 7);
Serial.println(distance0);
Serial.println(distance1);
Serial.println(distance2);
// currentMillis = millis(); you are using delay
// check distance, set LED and relay and maybe launch a tone burst
if ((distance0 < 4 || distance1 < 4 || distance2 < 4 ) && buzzerState == 0 )
{
digitalWrite(led, HIGH);
digitalWrite(rele, LOW);
buzzerState = 1; // no more tone until we've seen the distnace go > 10
for (int i = 0; i < 5; i++) {
tone(buzzer, 2500); // hearing this better at 1000 Hz
delay(500);
noTone(buzzer);
delay(500);
}
}
if (distance0 > 9 && distance1 > 9 && distance2 > 9 )
{
// noTone(buzzer); would immediately stop buzzer
buzzerState = 0;
Serial.print("buzzerState:");
Serial.println(buzzerState); // allow again the tone to trigger on
digitalWrite(led, LOW);
digitalWrite(rele, HIGH);
}
}
long getEcho(int trigPin, int echoPin)
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
return distance;
}*/