קודם נסיים את המכונית ש"נוהגת לבד", ואז ניקח את ההגה: נלמד שלוש דרכים לשלוט במכונית מרחוק — באור, בגלי רדיו, ומהטלפון. First we finish the car that "drives itself", then we take the wheel: three ways to control the car from a distance — with light, radio waves, and your phone.
ניווט: חצים ← → במקלדת, או הכפתורים למטה · לחצו EN לבדיקת מונחים באנגלית Navigate: ← → arrow keys, or the buttons below · press עב for Hebrew
בנינו מכונית עם 4 מנועים, מגשר L298N שנותן להם כוח, וחיישן HC-SR04 מלפנים. הקוד השתמש ב-if / else: אם קרוב — לעצור, אחרת — לנסוע. We built a car with 4 motors, an L298N H-bridge to power them, and an HC-SR04 sensor up front. The code used if / else: if close — stop, else — drive.
| חיבורConnection | פיןPin |
|---|---|
| HC-SR04 Trig / Echo | D9 / D10 |
| ENA (מהירות שמאלleft speed) | D5 |
| IN1 / IN2 (שמאלleft) | D2 / D3 |
| IN3 / IN4 (ימיןright) | D4 / D7 |
| ENB (מהירות ימיןright speed) | D6 |
⚠️ הסוללה של המנועים והארדואינו חייבים גראונד משותף.⚠️ The motor battery and the Arduino must share a common ground.
פין דיגיטלי יודע רק שני מצבים: דלוק / כבוי. הוא יכול להדליק מנוע — אבל לא להפוך אותו. כדי לנסוע אחורה צריך להחליף את הקוטביות (+/–) של המנוע. A digital pin has only two states: on / off. It can turn a motor on — but it can't reverse it. To go backward you must swap the motor's polarity (+/–).
בדיוק לזה נועד המגשר H (H-bridge): לכל ערוץ יש זוג פינים (IN1+IN2) שקובעים לאן זורם הזרם — כלומר את הכיוון. פין EN נפרד שולט במהירות (PWM). That's exactly what an H-bridge is for: each channel has a pair of pins (IN1+IN2) that set which way the current flows — i.e. the direction. A separate EN pin sets the speed (PWM).
| IN1 | IN2 | תוצאהResult |
|---|---|---|
| 1 | 0 | קדימהForward |
| 0 | 1 | אחורהReverse |
| 0 | 0 | עצירה (חופשי)Stop (coast) |
| 1 | 1 | בלימהBrake |
שנו את IN1 / IN2 ואת המהירות — וראו את המנוע:Toggle IN1 / IN2 and the speed — and watch the motor:
מכונית שנוהגת לבד צריכה לצאת מהצרה: אם חסום — לסגת קצת, להסתובב, ולהמשיך. בשביל זה צריך שני כיוונים חדשים. A self-driving car must get out of trouble: if blocked — back up a bit, turn, and carry on. For that we need new directions.
void forward() {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void backward() {
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
void turnRight() { // left fwd, right back
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}אותה מהירות ל-ENA/ENB בכל תנועה.Same speed on ENA/ENB for every move.
אותו if / else משיעור 2 — אבל בענף ה"חסום" יש עכשיו רצף קטן של פעולות במקום סתם עצירה. The same if / else from Lesson 2 — but the "blocked" branch now runs a small sequence instead of just stopping.
if (distanceCm <= 15) { // blocked
stopCar(); delay(200);
backward(); delay(400);
turnRight(); delay(400);
} else {
forward(); // clear -> drive
}גררו את המרחק וראו מה המכונית מחליטה:Drag the distance and watch the car decide:
שימו לב לדבר הכי חשוב היום: פונקציות התנועה נשארות בדיוק אותו דבר. מה שמשתנה זה רק מי מחליט איזו פונקציה להפעיל. Notice the most important thing today: the motion functions stay exactly the same. All that changes is who decides which function to call.
// self-driving: the SENSOR decides
if (distance <= 15) turnRight();
else forward();
// remote: YOU decide (a button)
if (button == UP) forward();
else if (button == RIGHT) turnRight();אותו "גוף", מוח אחר. זה מה שהופך רובוט לגמיש.Same "body", different brain. That's what makes a robot flexible.
| IR | RF 433 | Bluetooth | |
|---|---|---|---|
| מדיוםMedium | אורLight | רדיוRadio | רדיוRadio |
| קו ראייה?Line of sight? | חובהRequired | לאNo | לאNo |
| טווחRange | ~5 מ'~5 m | עשרות-מאות מ'10s–100s m | ~10 מ'~10 m |
| השלטThe remote | מוכןReady-made | בונים בעצמנוWe build it | הטלפוןYour phone |
| כיווןDirection | חד-כיווניOne-way | חד-כיווניOne-way | דו-כיווניTwo-way |
בדיוק כמו שלט הטלוויזיה: השלט מהבהב אור אינפרא-אדום שאנחנו לא רואים. כל כפתור שולח קוד מספרי אחר. המקלט על המכונית מפענח את הקוד. Just like your TV remote: it flashes infrared light we can't see. Each button sends a different numeric code. The receiver on the car decodes it.
נסו את השלט הווירטואלי — לחצו על החצים והמכונית תזוז:Try the virtual remote — press the arrows and the car moves:
כל שלט שולח מספרים אחרים, אז קודם קוראים אותם פעם אחת ורושמים. מעלים את הסקצ' הקצר, פותחים Serial Monitor, ולוחצים על כל כפתור. Every remote sends different numbers, so first we read them once and write them down. Upload the short sketch, open the Serial Monitor, and press each button.
#include <IRremote.h>
const int IR_RECEIVE_PIN = 8;
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN);
}
void loop() {
if (IrReceiver.decode()) {
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.resume();
}
}📄 code/level1_ir_read_codes.ino · רושמים את הקוד של ▲ ▼ ◀ ▶ ו-OK.📄 code/level1_ir_read_codes.ino · write down the code for ▲ ▼ ◀ ▶ and OK.
ממלאים את חמשת הקודים שרשמנו, וכל כפתור מפעיל אחת מפונקציות התנועה שכבר יש לנו. Fill in the five codes we wrote down, and each button calls one of the motion functions we already have.
if (IrReceiver.decode()) {
uint8_t cmd = IrReceiver.decodedIRData.command;
if (cmd == BTN_UP) forward();
else if (cmd == BTN_DOWN) backward();
else if (cmd == BTN_LEFT) turnLeft();
else if (cmd == BTN_RIGHT) turnRight();
else if (cmd == BTN_OK) stopCar();
lastCmd = millis();
IrReceiver.resume();
}
// no button for a moment -> stop, so it won't run away
if (millis() - lastCmd > 200) stopCar();| חיבורConnection | פיןPin |
|---|---|
| מקלט IRIR receiver OUT | D8 |
| ENA / ENB | D5 / D6 |
| IN1 / IN2 | D2 / D3 |
| IN3 / IN4 | D4 / D7 |
RF משתמש בגלי רדיו בתדר 433MHz. בניגוד ל-IR, רדיו לא צריך קו ראייה — הוא עובר דרך קירות ומגיע רחוק הרבה יותר. RF uses radio waves at 433MHz. Unlike IR, radio needs no line of sight — it passes through walls and reaches much farther.
לחצו "שלח" עם ובלי קיר — ותראו את ההבדל:Press "send" with and without a wall — and see the difference:
ב-IR השלט היה מוכן. ב-RF יש לנו זוג מודולים — משדר (TX) ומקלט (RX) — אז המשדר הוא ארדואינו שני עם כפתורים, והמקלט על המכונית. With IR the remote came ready. With RF we get a pair of modules — a transmitter (TX) and a receiver (RX) — so the remote is a second Arduino with buttons, and the receiver is on the car.
long code = CODE_STOP;
if (digitalRead(BTN_UP)==LOW) code=CODE_UP;
else if(digitalRead(BTN_LEFT)==LOW) code=CODE_LEFT;
// ...
mySwitch.send(code, 24); // over the airif (mySwitch.available()) {
long code = mySwitch.getReceivedValue();
if (code==CODE_UP) forward();
// ... map codes to moves
mySwitch.resetAvailable();
}📄 level2_rf_transmitter.ino · level2_rf_car.ino📄 level2_rf_transmitter.ino · level2_rf_car.ino
הפעם אין שלט נפרד בכלל — הטלפון הוא השלט. מודול HC-05/HC-06 על המכונית מדבר עם אפליקציית Dabble החינמית, שיש בה ג'ויסטיק על המסך. This time there's no separate remote at all — your phone is the remote. An HC-05/HC-06 module on the car talks to the free Dabble app, which has an on-screen gamepad.
#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>
void loop() {
Dabble.processInput(); // read the phone
if (GamePad.isUpPressed()) forward();
else if (GamePad.isDownPressed()) backward();
else if (GamePad.isLeftPressed()) turnLeft();
else if (GamePad.isRightPressed()) turnRight();
else stopCar();
}📄 code/bonus_bluetooth_car.ino · נחבר ונסיע מהטלפון בשיעור הבא.📄 code/bonus_bluetooth_car.ino · we'll wire it and drive from phones next lesson.
מכונית אחת, כמה "מוחות": חיישן, IR, RF, טלפון. פונקציות התנועה תמיד אותן — מפרידים בין "מה לעשות" ל"איך הפקודה מגיעה". One car, several "brains": sensor, IR, RF, phone. The motion functions are always the same — separate "what to do" from "how the command arrives".
💻 כל קבצי הקוד של השיעור — הורדה והסברAll of this lesson's code — download & walkthrough
תודה — נתראה בשיעור הבא! 👋Thanks — see you next lesson! 👋