ultrasinic basic

#define trigmadu 2 //depan
#define echomadu 3
#define led1 10 // depan

void setup() {
  Serial.begin (9600);
  pinMode(trigmadu, OUTPUT);
  pinMode(echomadu, INPUT);
  pinMode(led1, OUTPUT);
}

void loop() {
{
  long duration, distance;
  digitalWrite(trigmadu, LOW);
  delayMicroseconds(2);
  digitalWrite(trigmadu, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigmadu, LOW);
  duration = pulseIn(echomadu, HIGH);
  distance = (duration/2) / 29.1;


  if (distance < 50)
     {
      digitalWrite(led1, HIGH);
      delay(10); 
      }
 
  if (distance > 50)
   {
    digitalWrite(led1, LOW);
    delay(10);
    }


}
}

Comments