Lesson 17: Parking sensor
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
Hear the distance
- Many cars have a parking sensor at the back. It beeps slowly when a wall is far away and faster when it is close.
- Here the knob on A0 plays the distance sensor. Turn it down and the "wall" comes closer.
- The buzzer on pin 8 beeps. You decide how long the pause between two beeps is.
Words for this lesson
| Word | Meaning |
|---|---|
| parking sensor | a sensor that tells the driver how close a wall or car is |
| distance | how far away something is |
| pause | a short time of quiet |
| cm | centimetre; 100 cm = 1 metre |
| steady tone | one sound that does not stop |
English
The rule: pause = 10 × distance
| Distance | Pause | Beeps in 2 s |
|---|---|---|
| 81 cm | 810 ms | about 2 |
| 40 cm | 400 ms | about 4 |
| 20 cm | 200 ms | about 8 |
| under 10 cm | no pause | one long tone: STOP! |
- Each beep is 50 ms long. One beep and its pause take 50 + pause ms, so at 20 cm a beep comes every 250 ms.
- The sketch prints the distance and the pause every time, so you can watch the rule work while you turn the knob.
English
One beep
void setup() {
}
void loop() {
tone(8, 1000); // start a 1000 Hz sound on pin 8
delay(50);
noTone(8); // stop it
delay(950);
}
tone(8, 1000)starts the sound andnoTone(8)stops it. In between,delaydecides how long it lasts.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
A car parking sensor beeps faster as the car gets closer to a wall. The knob on A0 is the distance sensor: analogRead(A0) / 10 is the distance in cm. Make the pause between beeps 10 ms for every cm: pause = distance * 10. Turn the knob while it runs and listen.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 4/5 · 280 מטבעות + בונוס קטן בהשלמה הראשונה היום
Under 10 cm the car must stop. Then the sensor does not beep any more: it plays one long tone and prints STOP!. Add an if: closer than 10 cm, play tone(8, 1000) without noTone, print STOP! and wait 100 ms; else beep as before.
לחץ על הרץ כדי לראות את התוצא כאן.