Add single‑pin Sonar support with periodic reading
Implemented Sonar constructor to use a single shared pin, added mode flag and helper method for single‑pin measurements. Updated measure() to select the appropriate implementation and added timeout to pulseIn. Integrated periodic sonar updates in the main loop, tracking last update time and toggling a seatBelts flag based on distance thresholds.
This commit is contained in:
+12
-5
@@ -3,19 +3,20 @@
|
||||
#include "Config.h"
|
||||
#include "Display.h"
|
||||
#include "Gear.h"
|
||||
// #include "Sonar.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_PIN, ECHO_PIN, SONAR_MAX_DIST_CM);
|
||||
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);
|
||||
@@ -58,7 +59,13 @@ void loop() {
|
||||
display.updateSpeed(engine.getSpeedKmh());
|
||||
}
|
||||
|
||||
// long dist = sonar.measure();
|
||||
// Serial.println(dist);
|
||||
if (millis() - prevSonarUpdate >= 100) {
|
||||
prevSonarUpdate = millis();
|
||||
long dist = sonar.measure();
|
||||
if (!seatBelts)
|
||||
if (dist < 200)
|
||||
seatBelts = true;
|
||||
else if (dist >= 200) seatBelts = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user