Files
projekt-arduino/Projekt_Arduino.ino
T
GarandPLG 6c0a1fc15b 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.
2026-06-17 00:13:16 +02:00

99 lines
2.6 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <Wire.h>
#include "Config.h"
#include "Display.h"
#include "Gear.h"
#include "Sonar.h"
#include "RFID.h"
#include "Engine.h"
Display display;
GearSelector gear(GEAR_X_PIN, GEAR_Y_PIN, GEAR_THRESH_LOW, GEAR_THRESH_HIGH);
Sonar sonar(TRIG_AND_ECHO_PIN, SONAR_MAX_DIST_CM);
RFIDReader rfid(SS_PIN, RST_PIN);
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
unsigned long prevUpdate = 0;
bool carEnabled = false;
unsigned long prevSonarUpdate = 0;
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);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
pinMode(ENA_2, OUTPUT);
rfid.begin();
display.begin(LCD_COLS, LCD_ROWS);
engine.begin();
}
void loop() {
bool check = rfid.check();
bool matches = rfid.matches(AUTH_UID, AUTH_UID_SIZE);
bool rfidOk = check && matches;
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
carEnabled = !carEnabled;
if (carEnabled)
Serial.println("Uruchamiam auto");
else
Serial.println("Gaszę auto");
display.startOrEnd(carEnabled);
};
if (carEnabled) {
if (gear.update())
display.updateGear(gear.getGearChar());
unsigned long now = millis();
float dt = (now - prevUpdate) / 1000.0f;
if (dt >= 0.02f) {
prevUpdate = now;
engine.update(dt, gear.getGear());
display.updateRPM(engine.getRPM());
display.updateSpeed(engine.getSpeedKmh());
}
if (millis() - prevSonarUpdate >= 100) {
prevSonarUpdate = millis();
long dist = sonar.measure();
if (!seatBelts)
if (dist < 200)
seatBelts = true;
else if (dist >= 200) seatBelts = true;
}
}
}