מרכז שליטה: המכונית בדפדפן · כל קובץ בעמוד משלו, עם הקוד המלא וכפתור הורדה.Command center: the car in the browser · one file per page, with the full code and a download button.
⬇ להוריד הכל (zip)Download all (zip)
מדווחת D=מרחק,M=תנועה,S=מהירות חמש פעמים בשנייה ומקבלת F B L R S ו-1–9. זהה לשיעור 4, שלב 4.Reports D=distance,M=move,S=speed five times a second and accepts F B L R S and 1–9. Same as Lesson 4, Level 4.
/* Robotika – Lesson 5 · The car for the command center (telemetry) -------------------------------------------------------------- Bluetooth is TWO-WAY: the car listens for commands AND reports what it sees, 5 times a second, as one line of text: D=23,M=F,S=160 | | '-- S = speed (0..255) | '------ M = current move (F/B/L/R/S) '----------- D = distance in cm (999 = nothing in front) One line = one message. key=value pairs, separated by commas - simple for any program (even a web page) to read. New commands: '1'..'9' = speed (1 slow ... 9 full) New idea: millis() - a clock that does NOT stop the program. delay(200) would freeze the car and miss commands. This is the same sketch as Lesson 4, Level 4 (level4_bt_telemetry.ino). Wiring (the car): HC-SR04: Trig -> D9 Echo -> D10 L298N ch. A (both LEFT motors): ENA D5, IN1 D2, IN2 D3 L298N ch. B (both RIGHT motors): ENB D6, IN3 D4, IN4 D7 HC-05: TXD -> D0 (RX) RXD <- D1 (TX) via the 1k/2k divider VCC -> 5V GND -> GND Testing on the USB cable instead? Unplug the HC-05 data wires - the page then talks to the car through the Arduino's USB port. !! Unplug the HC-05 data wires (D0/D1) while uploading.*/const int trigPin = 9;const int echoPin = 10;const int ENA = 5, IN1 = 2, IN2 = 3, IN3 = 4, IN4 = 7, ENB = 6;const int STOP_CM = 15;const unsigned long REPORT_MS = 200; // send a report every 200 msint speed = 160; // not const any more - commands change itchar move = 'S';int distanceCm = 999;unsigned long lastReport = 0;void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); stopCar();}void loop() { // 1) INPUT: commands from the computer if (Serial.available() > 0) { char c = toupper(Serial.read()); if (c == 'F' || c == 'B' || c == 'L' || c == 'R' || c == 'S') { move = c; } else if (c >= '1' && c <= '9') { speed = map(c - '0', 1, 9, 80, 255); // '1' -> 80 ... '9' -> 255 } // anything else ('\n', '\r', typos) is ignored } // 2) SENSOR + SAFETY distanceCm = getDistanceCm(); if (move == 'F' && distanceCm <= STOP_CM) move = 'S'; // 3) OUTPUT: motors switch (move) { case 'F': forward(); break; case 'B': backward(); break; case 'L': turnLeft(); break; case 'R': turnRight(); break; default: stopCar(); break; } // 4) TELEMETRY: report without stopping the program if (millis() - lastReport >= REPORT_MS) { lastReport = millis(); Serial.print("D="); Serial.print(distanceCm); Serial.print(",M="); Serial.print(move); Serial.print(",S="); Serial.println(speed); }}// ---------- the four moves (now with a changing speed) ----------void drive(int a1, int a2, int b1, int b2) { digitalWrite(IN1, a1); digitalWrite(IN2, a2); digitalWrite(IN3, b1); digitalWrite(IN4, b2); analogWrite(ENA, speed); analogWrite(ENB, speed);}void forward() { drive(HIGH, LOW, HIGH, LOW); }void backward() { drive(LOW, HIGH, LOW, HIGH); }void turnRight() { drive(HIGH, LOW, LOW, HIGH); }void turnLeft() { drive(LOW, HIGH, HIGH, LOW); }void stopCar() { analogWrite(ENA, 0); analogWrite(ENB, 0); digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);}int getDistanceCm() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH, 25000); if (duration == 0) return 999; return duration * 0.0343 / 2;}
בלי מכונית: ארדואינו בכבל USB שמדבר באותה שפה. הפוטנציומטר ב-A0 הוא "המרחק".No car needed: an Arduino on USB that speaks the same language. The potentiometer on A0 is the "distance".
/* Robotika – Lesson 5 · LEVEL 0: A "fake car" for the command center ------------------------------------------------------------------ No car needed - just the Arduino on the USB cable. This sketch speaks EXACTLY the same language as the real car, so you can build and test the command center page at your desk: sends, 5 times a second: D=23,M=F,S=160 accepts: F B L R S and 1..9 (speed) The "distance" comes from a potentiometer: turn the knob = move the wall. The built-in "L" LED (pin 13) is on while the fake car is "driving". Same safety as the car: driving forward closer than STOP_CM -> stop. Wiring: Potentiometer: middle leg -> A0, outer legs -> 5V and GND (No potentiometer? Leave A0 unconnected - the numbers will just wander.) Use: upload, CLOSE the Serial Monitor, open the command center, Connect, pick the Arduino's USB port.*/const int POT_PIN = A0;const int LED_PIN = LED_BUILTIN;const int STOP_CM = 15;const unsigned long REPORT_MS = 200;char move = 'S';int speed = 160;unsigned long lastReport = 0;void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); Serial.println("Fake car ready. F B L R S");}void loop() { // 1) INPUT: commands from the page if (Serial.available() > 0) { char c = toupper(Serial.read()); if (c == 'F' || c == 'B' || c == 'L' || c == 'R' || c == 'S') { move = c; } else if (c >= '1' && c <= '9') { speed = map(c - '0', 1, 9, 80, 255); } } // 2) "SENSOR": the knob plays the distance sensor (5..120 cm) int distanceCm = map(analogRead(POT_PIN), 0, 1023, 5, 120); if (distanceCm > 110) distanceCm = 999; // far away = "no echo" // 3) SAFETY - same rule as the real car if (move == 'F' && distanceCm <= STOP_CM) move = 'S'; // 4) OUTPUT: the LED shows "driving" digitalWrite(LED_PIN, move != 'S' ? HIGH : LOW); // 5) TELEMETRY: one line every 200 ms if (millis() - lastReport >= REPORT_MS) { lastReport = millis(); Serial.print("D="); Serial.print(distanceCm); Serial.print(",M="); Serial.print(move); Serial.print(",S="); Serial.println(speed); }}