int echoPin = 9;
int trigPin = 10;
int echoPin2 = 11;
int trigPin2 = 12;
int low_limit = 20;
int mid_limit = 50;
int high_limit = 100;
int buzzerPin = 5;



void setup() {
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

}

void loop() {
  int distance = get_distance();
  int distance2 = get_distance2();  
   
   if(distance>mid_limit && distance<high_limit)
{
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    tone (buzzerPin, 1000, 100);
    delay(800);
}
 if(distance>low_limit && distance<mid_limit)
{
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    tone (buzzerPin, 2000, 100);
    delay(400);
    
}
   if(distance<=low_limit && distance>=0)
{
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    tone (buzzerPin, 3000, 100);
    delay(200);
}
   if(distance2<=mid_limit && distance2>=0)
{
    tone (buzzerPin, 3000, 200);
    delay(200);
}
  if((distance>=high_limit && distance<0) && (distance>=high_limit && distance<0))
{
  digitalWrite(buzzerPin, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  }
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}
int get_distance2()
{
  digitalWrite(trigPin2, HIGH);
  delay(10);
  digitalWrite(trigPin2, LOW);
  int distance2 = pulseIn(echoPin2, HIGH);
  distance2 = (distance2/2)/29.1;
  return distance2;
}
int get_distance()
{
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);
  int distance = pulseIn(echoPin, HIGH);
  distance = (distance/2)/29.1;
  return distance;
}

© Wklejaj.pl