Created
October 15, 2019 13:06
-
-
Save rburnett3/4c8dc29f5f8b628e057766019d8ce5c0 to your computer and use it in GitHub Desktop.
maxsonar-mb1240-pulse-width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Arduino example code for MaxBotix MB1240 XL-MaxSonar-EZ4 ultrasonic distance sensor: pulse width output. More info: www.makerguides.com */ | |
#define sensorPin 2 | |
long distance = 0; | |
long duration = 0; | |
void setup() { | |
pinMode(sensorPin, INPUT); | |
Serial.begin(9600); | |
} | |
void read_sensor() { | |
duration = pulseIn(sensorPin, HIGH); | |
distance = duration / 58; | |
} | |
void print_data() { | |
Serial.print("distance = "); | |
Serial.print(distance); | |
Serial.println(" cm"); | |
} | |
void loop() { | |
read_sensor(); | |
print_data(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment