Make car mass configurable at runtime

Replace the compile‑time CAR_MASS constant with a runtime‑settable
_member _carMass. Add Engine::setCarMass, default it to 1200 kg, and
use it in the acceleration calculation. Prompt the user for the
car mass on startup via Serial and update the enable/disable messages.
This commit is contained in:
2026-06-17 00:13:16 +02:00
parent c053a296a5
commit 6c0a1fc15b
3 changed files with 33 additions and 4 deletions
+29 -2
View File
@@ -21,6 +21,29 @@ bool seatBelts = false;
void setup() {
Serial.begin(9600);
// -----------------------------------------------------------------
// 1. Ask the user for the car mass (kg) via the Serial monitor.
// We block only until a valid number is received this is OK
// because it happens once at startup.
// -----------------------------------------------------------------
Serial.println(F("Enter car mass in kg (e.g. 1200):"));
while (true) {
// Wait until something is typed
if (Serial.available() > 0) {
float mass = Serial.parseFloat(); // reads the number
if (mass > 0) { // basic sanity check
engine.setCarMass(mass);
Serial.print(F("Car mass set to "));
Serial.print(mass);
Serial.println(F(" kg"));
break;
} else {
Serial.println(F("Invalid value try again:"));
}
}
}
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(ENA_1, OUTPUT);
@@ -41,8 +64,12 @@ void loop() {
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
carEnabled = !carEnabled;
Serial.print("Auto działa: ");
Serial.println(carEnabled);
if (carEnabled)
Serial.println("Uruchamiam auto");
else
Serial.println("Gaszę auto");
display.startOrEnd(carEnabled);
};