One verified technique:**
#include#define TRIGGER_PIN 11 #define ECHO_PIN 12 #define MAX_DISTANCE 200 int ledPin = 10; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); void setup() { Serial.begin(115200); pinMode(ledPin, OUTPUT); } void loop() { delay(50); unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); float objDist = uS / US_ROUNDTRIP_CM; Serial.print(objDist); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo) Serial.println("cm"); if(objDist > 0.1 && objDist <= 25){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } }
**The html formatting to post the code removes the library name from the #include line. The library name
No comments:
Post a Comment