sensor surround ultrasonic mobil kelas x MADU PUI

#define trigmadu 2 //depan
#define echomadu 3
#define trigmadu1 4 // belakang
#define echomadu1 5
#define trigmadu2 6  // kiri
#define echomadu2 7
#define trigmadu3 8 // kanan
#define echomadu3 9

#define led1 10 // depan
#define led2 11 // belakang
#define led3 12  // kiri
#define led4 13 // kanan




void setup() {
  Serial.begin (9600);
  pinMode(trigmadu, OUTPUT);
  pinMode(echomadu, INPUT);
  pinMode(trigmadu1, OUTPUT);
  pinMode(echomadu1, INPUT);
  pinMode(trigmadu2, OUTPUT);
  pinMode(echomadu2, INPUT);
  pinMode(trigmadu3, OUTPUT);
  pinMode(echomadu3, INPUT);
 
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, 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);
    }

////////////////////////

{
  long duration, distance;
  digitalWrite(trigmadu1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigmadu1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigmadu1, LOW);
  duration = pulseIn(echomadu1, HIGH);
  distance = (duration/2) / 29.1;


  if (distance < 50)
     {
      digitalWrite(led2, HIGH);
      delay(10); 
      }
 
  if (distance > 50)
   {
    digitalWrite(led2, LOW);
    delay(10);
    }
 //////////////////////////////////
{
  long duration, distance;
  digitalWrite(trigmadu2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigmadu2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigmadu2, LOW);
  duration = pulseIn(echomadu2, HIGH);
  distance = (duration/2) / 29.1;


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

//////////////////////////////
{
  long duration, distance;
  digitalWrite(trigmadu3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigmadu3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigmadu3, LOW);
  duration = pulseIn(echomadu3, HIGH);
  distance = (duration/2) / 29.1;


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


}
}
}
}
}

Popular Posts