Created
May 7, 2014 21:55
-
-
Save lazychino/f165212b302e2298d8e3 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
/* | |
MeasureDistance by Pedro A. Melendez | |
based on Analog input, analog output, serial output by Tom Igoe | |
Reads Analog input from proximity sensor, flicker the pin 13 LED with period of the sensor value in ms, | |
and alsa maps the sensor value to the distance and sends the data via serial. | |
*/ | |
// These constants won't change. They're used to give names | |
// to the pins used: | |
const int proximitySensor = A0; // Analog input pin that the potentiometer is attached to | |
const int ledPin = 13; // integrated LED on the Arduino Leonardo | |
int sensorValue = 0; // value read from the proximity sensor | |
int distance = 0; | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
Serial.begin(9600); | |
// initialize the led pin | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() { | |
// read the analog in value: | |
sensorValue = analogRead(proximitySensor); | |
// map read 10 bit value to distance range | |
//~ distance = map(sensorValue, 634, 81, 4, 30); // short distance sensor | |
distance = map(sensorValue, 634, 81, 10, 80); // medium distance sensor | |
// turn on and off the LED using as interval the value of the sensor | |
digitalWrite(ledPin, HIGH); | |
delay(distance*2); | |
digitalWrite(ledPin, LOW); | |
delay(distance*2); | |
// print the results to the serial monitor: | |
Serial.print("sensor = " ); | |
Serial.print(sensorValue); | |
Serial.print("\t distance = "); | |
Serial.println(distance); | |
// wait 2 milliseconds before the next loop | |
// for the analog-to-digital converter to settle | |
// after the last reading: | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment