Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ECHOPIN 9 // Pin to receive echo pulse
- #define TRIGPIN 8 // Pin to send trigger pulse
- void setup()
- {
- Serial.begin(115200);
- pinMode(ECHOPIN, INPUT);
- pinMode(TRIGPIN, OUTPUT);
- }
- void loop()
- {
- digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
- delayMicroseconds(10);
- digitalWrite(TRIGPIN, LOW); // Send pin low again
- long distance = pulseInLong(ECHOPIN, HIGH, 30000); // Read in times pulse
- Serial.print("pulseIn=");
- Serial.print(distance);
- Serial.print("\t");
- distance = distance / 58; // Calculate distance from time of pulse
- Serial.print("Distance=");
- Serial.println(distance);
- delay(50); // Wait 50mS before next ranging
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement