Skip to content

Instantly share code, notes, and snippets.

@nix010
Last active April 6, 2018 10:31
Show Gist options
  • Save nix010/c6b2d425f9962ee79c54956a5d71fc5f to your computer and use it in GitHub Desktop.
Save nix010/c6b2d425f9962ee79c54956a5d71fc5f to your computer and use it in GitHub Desktop.
// https://docs.google.com/presentation/d/1rrYLHVR3_Fc7mmvc8I8zGOhtoBDN02RjF_-aecimZdg/edit?usp=sharing
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor
#define US_ROUNDTRIP_CM 58
void setup() {
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
pinMode(ECHO_PIN, OUTPUT);// Open serial monitor at 115200 baud to see ping results.
}
void loop() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
unsigned int distance = uS / US_ROUNDTRIP_CM;
Serial.print("Ping: ");
Serial.print(distance); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if(distance < 30 && distance > 0 ){
// distance from 0-30 cm
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment