SHARE
TWEET

Test ping

thetechhacker27 Apr 14th, 2013 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Define the pins you want to use as trigger and echo.
  3.  */
  4.  
  5. #define ECHOPIN 2        // Pin to receive echo pulse
  6. #define TRIGPIN 3        // Pin to send trigger pulse
  7.  
  8. /*
  9.  * setup function
  10.  * Initialize the serial line (D0 & D1) at 115200.
  11.  * Then set the pin defined to receive echo in INPUT
  12.  * and the pin to trigger to OUTPUT.
  13.  */
  14.  
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(115200);
  19.   pinMode(ECHOPIN, INPUT);
  20.   pinMode(TRIGPIN, OUTPUT);
  21.   pinMode(11, OUTPUT);
  22.  
  23. }
  24.  
  25. /*
  26.  * loop function.
  27.  *
  28.  */
  29. void loop()
  30. {
  31.   // Start Ranging
  32.   digitalWrite(TRIGPIN, LOW);
  33.   delayMicroseconds(2);
  34.   digitalWrite(TRIGPIN, HIGH);
  35.   delayMicroseconds(10);
  36.   digitalWrite(TRIGPIN, LOW);
  37.   // Compute distance
  38.   float distance = pulseIn(ECHOPIN, HIGH);
  39.   distance= distance/58;
  40.   Serial.println(distance);//print distance to Serial
  41.   Serial.println("cm");
  42.   if(distance <= 90.00){// if the distance is more than 90cm
  43.     digitalWrite(11, HIGH); //pin 11 0n
  44.    
  45.     delay(1000);//wait
  46.    
  47.   }
  48.   if(distance >= 90.00){//if less than 90
  49.     digitalWrite(11, LOW); //pin 11 off
  50.    
  51.    
  52.   }
  53.   delay(200);
  54. }
RAW Paste Data
Top