Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
1
Indexable
Never
int trig = 9;
int echo = 10;
int red = 8;
int orange = 7;
int green = 6;
int buzz = 5;
void setup() {
pinMode(trig, OUTPUT);
pinMode(red, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(green, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t1 = pulseIn(echo, HIGH);
  int distance = 0.0343*t1/2;
  Serial.println(distance);
  if (distance > 30 && distance < 45){
    digitalWrite(green, HIGH);
    digitalWrite(orange, LOW);
    digitalWrite(red, LOW);
    digitalWrite(buzz, HIGH);
    delay(200);
    digitalWrite(buzz, LOW);
    } else{
       digitalWrite(green, LOW);
      }
      if (distance <= 30 && distance >=15){
    digitalWrite(green, LOW);
    digitalWrite(orange, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(buzz, HIGH);
    delay(100);
    digitalWrite(buzz, LOW);
    } else{
       digitalWrite(orange, LOW);
      }
      if (distance < 15){
    digitalWrite(green, LOW);
    digitalWrite(orange, LOW);
    digitalWrite(red, HIGH);
    digitalWrite(buzz, HIGH);
    } else{
       digitalWrite(red, LOW);
       digitalWrite(buzz, LOW);
      }
  delay(100);
}