/* 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); } }