Created
February 14, 2016 01:48
-
-
Save mandyRae/9b08cc6896fcd89db1fa to your computer and use it in GitHub Desktop.
Sample sketch for using ultrasonic sensors with Arduino
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
//This sketch demonstrates how to interface with ultrasonic sensors using the Arduino and NewPing library | |
#include <NewPing.h> | |
int TRIGGER = 12; | |
int ECHO = 11; | |
//Declare a sonar object specifying the trigger and echo pins | |
NewPing sonar(TRIGGER, ECHO); | |
void setup(){ | |
//Start serial at 115200 baud rate | |
Serial.begin(115200); | |
} | |
void loop(){ | |
//get current measurement from sensor in centimeters | |
int distance = sonar.ping_cm(); | |
//print it to the serial monitor | |
Serial.print(distance); | |
Serial.println("cm"); | |
//pause slightly before rereading the sensor again, don't make this value less than 30 | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment