Program an ultrasound sensor
Program an ultrasound sensor to turn an LED on. When motion is detected within a range of the ultrasound sensor the LED with turn on. Follow this patch to connect a push button to your Arduino board.
// Use an ultrasound sensor to turn an LED on. Sensor sends a wave out and then listens for it to bounce back. Stand at different distances from the sensor until you trigger the LED to turn on.
int ultraSoundpin = 7;
int ledPin = 13;
unsigned long ultrasoundDuration;
void setup() {
beginSerial(9600);
}
void loop() {
// switch pin to output
pinMode(ultraSoundpin, OUTPUT);
pinMode(ledPin, OUTPUT);
// send a low, wait 2 microseconds, send a high then wait 10 microseconds
digitalWrite(ultraSoundpin, LOW);
delayMicroseconds(2);
digitalWrite(ultraSoundpin, HIGH);
delayMicroseconds(10);
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();
if(ultrasoundDuration < 4000){ // if the first sensor works make the LED flash.
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100);
}
Comments (0)
You don't have permission to comment on this page.