Skip to content

Instantly share code, notes, and snippets.

@cowdinosaur
Created September 19, 2014 10:16
Show Gist options
  • Save cowdinosaur/fbc70cc2f054a2c312fc to your computer and use it in GitHub Desktop.
Save cowdinosaur/fbc70cc2f054a2c312fc to your computer and use it in GitHub Desktop.
Tinkerbot 3.5: Controlling the proximity sensor
#include <NewPing.h>
const int ULTRASONIC_TRIGGER = 6;
const int ULTRASONIC_ECHO = 7;
#define MAX_DISTANCE 100
NewPing sonar(ULTRASONIC_TRIGGER, ULTRASONIC_ECHO, MAX_DISTANCE);
void setup() {
pinMode(ULTRASONIC_TRIGGER, OUTPUT);
pinMode(ULTRASONIC_ECHO, INPUT);
Serial.begin(9600);
}
void loop() {
int distance = sonar.ping_cm();
Serial.println(distance);
delay(500);
/*
* What happens within the library
* http://www.circuitstoday.com/wp-content/uploads/2012/12/HC-SR04-timing-diagram.png
*/
/*
long duration, distance;
digitalWrite(ULTRASONIC_TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIGGER, LOW);
duration = pulseIn(ULTRASONIC_ECHO, HIGH);
distance = (duration/2) / 29.1;
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment