SHARE
TWEET

Untitled

a guest Sep 13th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int trigPin = 9;
  2. int echoPin = 10;
  3. int led = 7;
  4.  
  5.  
  6. void setup() {
  7.   Serial.begin(9600);
  8.    pinMode(led, OUTPUT);
  9.    pinMode(trigPin, OUTPUT);
  10.   pinMode(echoPin, INPUT);
  11. }
  12.  
  13. void loop() {
  14.   long duration, distance;
  15.   digitalWrite(trigPin,HIGH);
  16.   delayMicroseconds(1000);
  17.   digitalWrite(trigPin, LOW);
  18.   duration=pulseIn(echoPin, HIGH);
  19.   distance =(duration/2)/29.1;
  20.   Serial.print(distance);
  21.   Serial.println("CM");
  22.   delay(10);
  23.  
  24.  if((distance<=10))
  25.   {
  26.     digitalWrite(led, HIGH);
  27. }
  28.    else if(distance>10)
  29.  {
  30.      digitalWrite(led, LOW);
  31.    }
  32. }
RAW Paste Data
Top