Skip to content

Instantly share code, notes, and snippets.

@angelaperrone
Created February 2, 2017 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelaperrone/f4226a267517d5d03f4514cdc8e10727 to your computer and use it in GitHub Desktop.
Save angelaperrone/f4226a267517d5d03f4514cdc8e10727 to your computer and use it in GitHub Desktop.
//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