Advertisement
filip710

drugi rssv

May 6th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. // ---------------------------------------------------------------------------
  2.  
  3. #include <NewPing.h>
  4.  
  5. #define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  6. #define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
  7. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  8. #define LED_PIN 13
  9.  
  10. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  11.  
  12. void setup() {
  13.   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  14.   pinMode(LED_PIN, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18.   delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  19.   Serial.print("Ping: ");
  20.   Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  21.   Serial.println("cm");
  22.  
  23.   if(sonar.ping_cm() < 50 && sonar.ping_cm() != 0) {
  24.     digitalWrite(ledPin, HIGH);
  25.   }
  26.   else {
  27.     digitalWrite(ledPin, LOW);
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement