Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pingPin = 7;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- int duration, cm; // Variable for duration and cm
- pinMode(pingPin, OUTPUT); // setting pingPin as an OUTPUT for ultrasonic sensor outward pulse
- digitalWrite(pingPin, LOW); // short LOW state to ensure clean HIGH pulse
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH); // send outward pulse
- delayMicroseconds(5); // keep ultrasonic sensor at HIGH for 5 microseconds
- digitalWrite(pingPin, LOW); // turn ultrasonic sensor outwards pulse off
- pinMode(pingPin, INPUT); // setting pingPin as an INPUT for ultrasonic sensor to recieve echo pulse
- duration = pulseIn(pingPin, HIGH); // variable duration to be
- cm = (duration/2) /29.3866996;
- Serial.print(cm);
- Serial.println(" cm");
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement