Program an ultrasound sensor and integrate a potentiometer
Program an ultrasound sensor to send out an ultrasound wave and then listen for it to bounce back while counting time. This is the same technique that bats use to locate their prey. Program an LED to turn on at particular distance or range from the sensor. Instead of using your body or an object to turn your LED to turn on, use a potentiometer to locate this distance at a certain point in its rotation. The potentiometer has a range of 0 to 1023. This range can be narrow (making bigger steps) by dividing it by an integer. (See sample code)
Download the diagram and code: ultrasound_led_pot.pdf
// Send a wave out with an ultrasound sensor; Use a potentiometer to turn on an LED at a particular distance from the sensor.
int ultraSoundpin = 7;
int ledPin = 13;
unsigned long ultrasoundDuration;
int potPin = 2; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the potentiometer
void setup() {
beginSerial(9600);
}
void loop() {
// switch pin to output
// sensor sends an ultrasound wave out and then waits for the wave to bounce back
// same technique as dolphins and bats
pinMode(ultraSoundpin, OUTPUT); // notice that pinmode is usually set in void loop
pinMode(ledPin, OUTPUT);
// send a low, wait 2 microseconds, send a high then wait 10 microseconds
// switches ultrasound sensor pin from output to input; sensor waits to receive ultrasound wave; counts how long the ultrasound wave has been gone (time) before bouncing back
digitalWrite(ultraSoundpin, LOW);
delayMicroseconds(2);
digitalWrite(ultraSoundpin, HIGH);
delayMicroseconds(10); // length of wave
digitalWrite(ultraSoundpin, LOW);
// switch pin to input
pinMode(ultraSoundpin, INPUT);
// wait for a pulse to come in as high
ultrasoundDuration = pulseIn(ultraSoundpin, HIGH);
// output
Serial.print(ultrasoundDuration); //
//Serial.print("\t");
Serial.print(ultrasoundDuration/58, DEC);
//Serial.print(" cm");
Serial.println();
val = analogRead(potPin); // read the value from the sensor, fine-tune sensor
//val = val / 3; // to get a narrower range with bigger steps in between "uncomment" this line of code to divide your analog value by 3 or any number
if(ultrasoundDuration < val){
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100);
}
Comments (0)
You don't have permission to comment on this page.