Price:
- 1 x Ultrasonic Module HC-SR04 Distance Measuring Transducer Sensor - $2.56 / piece
- 1 x Funduino Nano 3.0 controller - $7.89 / piece.
After that I wrote simple sketch.
#define TRIG_PIN 3
#define ECHO_PIN 2
#define BUZZ_PIN 4
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, HIGH);
delay(1);
digitalWrite(TRIG_PIN, LOW);
int distance = pulseIn(ECHO_PIN, HIGH) / 2 / 29.1;
if (distance < 100)
tone(BUZZ_PIN, map(distance, 0, 100, 1000, 15), 100);
else
delay(100);
}
As the result I've got working example, which can detect an object closer 1 meter and react using sound. My dog found it quite interesting to play with it.
Keine Kommentare:
Kommentar veröffentlichen