היום נלמד איך המחשב "רואה" מרחק בעזרת קול, נציג את המספר על תצוגה, ונוסיף נורית אזהרה חכמה. Today: how the computer "sees" distance using sound, shows the number on a display, and adds a smart warning light.
ניווט: חצים ← → במקלדת, או הכפתורים למטה · לחצו EN לבדיקת מונחים באנגלית Navigate: ← → arrow keys, or the buttons below · press עב for Hebrew
שולטים בפין ומדליקים/מכבים — נורית שהבהבה.Drive a pin on/off — the blinking LED.
קוראים ערך משתנה — חיישן האור.Read a changing value — the light sensor.
setup רץ פעם אחת, loop חוזר שוב ושוב.setup runs once, loop repeats forever.
התוכנית מקבלת החלטה: אם התנאי נכון — עושים דבר אחד; אחרת — דבר אחר. The program makes a decision: if the condition is true — do one thing; else — do another.
if (distance > 30) {
// far — all clear
ledOff();
} else {
// close — warn!
blink();
}גררו את המרחק ותראו איזה ענף "נדלק":Drag the distance and watch which branch lights up:
החיישן צועק צליל על-קולי (אנחנו לא שומעים אותו), הצליל פוגע בעצם וחוזר כהד. החיישן מודד כמה זמן לקח לצליל לצאת ולחזור. The sensor shouts an ultrasonic sound (we can't hear it), it hits an object and echoes back. The sensor measures the time for the sound to go and return.
distance = (time × 0.0343) / 2למה חלקי 2? כי הזמן שמדדנו הוא לשם ובחזרה — הלוך ושוב. Why divide by 2? Because the time measured is for the trip there AND back.
לחיישן הסונאר יש 4 פינים. שניים לחשמל, ושניים ל"שיחה" עם הארדואינו: The sonar has 4 pins. Two for power, two to "talk" with the Arduino:
// 1) fire the trigger
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// 2) time the echo (round trip, µs)
long t = pulseIn(echo, HIGH);
// 3) time -> distance
float cm = t * 0.0343 / 2;תצוגת הספרות מקבלת מידע בשני חוטים:The digit display receives data on two wires:
בלי שעון משותף, הצד המקבל לא יודע מתי כל ביט תקף. השעון הוא המטרונום ששני הצדדים סופרים לפיו — זו "תקשורת טורית". Without a shared clock, the receiver wouldn't know when each bit is valid. The clock is the metronome both sides count to — this is "serial communication".
| Component | Pin | Arduino |
|---|---|---|
| HC-SR04 | Trig | D9 |
| HC-SR04 | Echo | D10 |
| TM1637 | CLK | D4 |
| TM1637 | DIO | D5 |
| LED (+) | via 220Ω | D6 |
| All | VCC / GND | 5V / GND |
המטרה: מספר מרחק מתעדכן בחלון המוניטור הטורי.Goal: a live distance number in the Serial Monitor.
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distanceCm = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.println(" cm");
delay(200);
}מעבירים את הקריאה לפונקציה משלנו, ומדפיסים על התצוגה. צריך את הספרייה TM1637Display. Move the reading into our own function, and print on the display. Needs the TM1637Display library.
#include <TM1637Display.h>
const int trigPin=9, echoPin=10, CLK=4, DIO=5;
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
display.setBrightness(5);
}
void loop() {
int distanceCm = getDistanceCm(); // our function
Serial.println(distanceCm);
display.showNumberDec(distanceCm); // show on display
delay(200);
}
int getDistanceCm() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.0343 / 2;
}כאן נכנס ה-if / else וגם map(): הופכים "מרחק" ל"קצב הבהוב". Here the if / else and map() come in: turn "distance" into "blink speed".
const int START_CM = 30;
if (distanceCm > START_CM) {
digitalWrite(ledPin, LOW); // far: off
delay(200);
} else {
int d = map(distanceCm, 0, START_CM, 40, 500);
digitalWrite(ledPin, HIGH); delay(d);
digitalWrite(ledPin, LOW); delay(d);
}map: מרחק 0 → 40ms (מהיר) · מרחק 30 → 500ms (איטי) map: distance 0 → 40ms (fast) · distance 30 → 500ms (slow)
מרכיבים את החיישן על חזית המכונית. היא מחכה 2 שניות, מתחילה לנסוע קדימה, ועוצרת כשמשהו נמצא ממש לפניה (בערך 5 ס״מ). The sensor sits on the front of the car. It waits 2 seconds, drives forward, and stops when something is right in front of it (about 5 cm).
זה בדיוק ה-if / else עם הסונאר מהשיעור, אבל במקום נורית — 4 מנועים. אם קרוב מ-5 ס״מ → עצור, אחרת → סע. It's exactly the if / else with the sonar from the lesson, but instead of an LED — 4 motors. If closer than 5 cm → stop, else → drive.
חשוב: ה-GND של ה-L298N והארדואינו חייבים להיות מחוברים יחד. Important: the L298N's GND and the Arduino's GND must be connected together.
▶ youtube.com/watch?v=LO-tLG4t5Dg · אין אינטרנט בכיתה? הקישור נפתח בכל מחשבno internet in class? the link opens anywhere
הכרטיס — מה כל חלק (לזיהוי)The board — what each part is (for identification)
| L298N | Arduino | |
|---|---|---|
| ENA | → | D5 PWM · remove jumper |
| IN1 | → | D2 |
| IN2 | → | D3 |
| IN3 | → | D4 |
| IN4 | → | D7 |
| ENB | → | D6 PWM · remove jumper |
| Vs (5–35V) | → | battery + |
| GND | → | battery − & Arduino GND |
| Motor A / B | → | left / right motors |
ג׳מפר 12V: משאירים לסוללה עד 12V.12V jumper: keep it on for a battery up to 12V.
תודה — נתראה בשיעור הבא! 👋Thanks — see you next lesson! 👋