ultrasonic sensor level

/*-----------------------------------------------------
'   
'     TRIG --> Pin 7
'     ECHO --> Pin 6
'     Baud Rate 9600 bps
'     asun86.com
'-----------------------------------------------------*/


//pin. Ultrasonik
#define trigPin 7
#define echoPin 6

#define ledAman 8;
#define led1 9;
#define led2 10;
#define led3 11;



void setup() {
  Serial.Begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(ledAman, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, 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 >= 16);
  {
    digitalWrite(ledAman, HIGH);
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3,LOW);
  }
  else {
    digitalWrite(ledAman,LOW);
  }

  if (distance <= 15);
  {
      digital.Write(led1, HIGH);
      digital.Write(led2, LOW);
      digital.Write(led3,LOW);
  }

  if (distance < 10); {
      digital.Write(led2, HIGH);
      digital.Write(led1, LOW);
      digital.Write(led3,LOW);   
  }

  if (distance < 5); {
    digital.Write(led3, HIGH);
    digital.Write(led2, LOW);
    digital.Write(led1, LOW);
}

  delay(500);
}

Popular Posts