aytdev

AURDINO SONAR SENSOR PROJECT

Oct 7th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. int trigpin=A1, echopin = A2,ledpin =A3;
  2. float duration,distance;
  3. void setup(){
  4.   pinMode(trigpin,OUTPUT);
  5.   pinMode(echopin,INPUT);
  6.   pinMode(ledpin,OUTPUT);
  7.   Serial.begin(9600);
  8.  
  9. }
  10. void loop(){
  11.  
  12.  digitalWrite(trigpin,LOW);
  13.   delay(2);
  14.   digitalWrite(trigpin,HIGH);
  15.   delayMicroseconds(10);
  16.   digitalWrite(trigpin,LOW);
  17.   duration = pulseIn(echopin,HIGH);
  18.   distance =( duration * 0.034 * 0.5);
  19.   if(distance<=100){
  20.      digitalWrite(ledpin,HIGH);
  21.  
  22.   }else{
  23.  
  24.     digitalWrite(ledpin,LOW);
  25.   }
  26.   Serial.print("The distance is: ");
  27.   Serial.print(distance);
  28.   Serial.print("cm\n");
  29.   delay(500);
  30. }
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment