Skip to content

Instantly share code, notes, and snippets.

@rburnett3
Created October 15, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rburnett3/4c8dc29f5f8b628e057766019d8ce5c0 to your computer and use it in GitHub Desktop.
Save rburnett3/4c8dc29f5f8b628e057766019d8ce5c0 to your computer and use it in GitHub Desktop.
maxsonar-mb1240-pulse-width
/* 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