Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.2 kB
4
Indexable
Never
#define trigPin 13
#define echoPin 12
void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}
void loop()
{
  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;
  if (distance >= 200 || distance <= 0) {
    Serial.println("Out of range");
  }
  else
  {

    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    if (distance > 12) {
      digitalWrite(1, HIGH);
    }

    if (distance > 15) {
      digitalWrite(2, HIGH);
    }

    if (distance > 17) {
      digitalWrite(3, HIGH);
    }

    if (distance > 20) {
      digitalWrite(4, HIGH);
    }

      if (distance > 23) {
      digitalWrite(5, HIGH);
    }

  }
  delay(500);
}