Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pingPin = 6;
- const int echoPin = 5;
- void setup() {
- Serial.begin(9600);
- pinMode(6, OUTPUT);
- pinMode(5, INPUT);
- pinMode(11, OUTPUT);
- }
- void loop() {
- long duration, inches, cm;
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(pingPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- // convert the time into a distance
- inches = microsecondsToInches(duration);
- cm = microsecondsToCentimeters(duration);
- if (cm < 30){
- digitalWrite(11, LOW);
- }
- else
- digitalWrite(11, HIGH);
- Serial.print(inches);
- Serial.print("in, ");
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- delay(100);
- }
- long microsecondsToInches(long microseconds) {
- return microseconds / 74 / 2;
- }
- long microsecondsToCentimeters(long microseconds) {
- return microseconds / 29 / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement