Created
February 2, 2017 21:41
-
-
Save angelaperrone/f4226a267517d5d03f4514cdc8e10727 to your computer and use it in GitHub Desktop.
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
//reference: https://itp.nyu.edu/physcomp/labs/motors-and-transistors/using-a-transistor-to-control-high-current-loads-with-an-arduino/ | |
//reference: http://www.tautvidas.com/blog/2012/08/distance-sensing-with-ultrasonic-sensor-and-arduino/ | |
//reference: https://www.arduino.cc/en/Tutorial/Ping | |
//reference: http://art-research2010summer.blogspot.com.es/2010/06/tutorial-01-isadora-and-arduino.html | |
const int trigPin = 12; | |
const int echoPin = 13; | |
void setup() { | |
Serial.begin (9600); | |
} | |
void loop() { | |
//trigger/pulse | |
long duration, inches, cm; | |
pinMode(trigPin, OUTPUT); | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
//read sensor | |
pinMode(echoPin, INPUT); | |
duration = pulseIn(echoPin, HIGH, 185000); | |
//convert time to distance | |
inches = microsecondsToInches (duration); | |
cm = microsecondsToCentimeters (duration); | |
Serial.println(cm); | |
delay(100); | |
Serial.println(1, DEC); | |
Serial.println(cm); | |
delay(10); | |
long microsecondsToInches (long microseconds) { | |
return microseconds / 74 / 2; | |
} | |
long microsecondsToCentimeters (long microseconds) { | |
return microsecond |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment