Created
September 19, 2014 10:16
-
-
Save cowdinosaur/fbc70cc2f054a2c312fc to your computer and use it in GitHub Desktop.
Tinkerbot 3.5: Controlling the proximity sensor
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
#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