Object detector using Ardiuno

 #define trigpin 8#define echopin 7

#define ledpin 6

#define buzzer 3

void setup() {

  // put your setup code here, to run once: pinMode(trigpin,OUTPUT);

 pinMode(echopin,INPUT);

 pinMode(ledpin,OUTPUT); 

 pinMode(buzzer,OUTPUT);

 Serial.begin(9600);}

void loop() {

  // put your main code here, to run repeatedly:

  digitalWrite(trigpin,LOW);  delayMicroseconds(2);

  digitalWrite(trigpin,HIGH);  delayMicroseconds(10);

  digitalWrite(trigpin,LOW);

  int duration = pulseIn(echopin,HIGH);

  int distance =(duration/2) / 29.1;

  if(distance<=20)  {

    Serial.println("Object Nearby");    digitalWrite(ledpin,HIGH);

    tone(buzzer,10000,500);    

  }  else

  {    Serial.println("Path Clear");

    digitalWrite(ledpin,LOW);    noTone(buzzer);

      }

}

Comments