From 3cdfff0a1035e3484b123f49dbc5114b053c55dd Mon Sep 17 00:00:00 2001 From: rafa Date: Fri, 23 May 2025 12:45:57 +0100 Subject: [PATCH] Create a car spot controller --- code/CarSpotController.cpp | 31 +++++++++++++++++++++++++++++++ code/LCDScreen.cpp | 16 ++++++++++++++++ code/code.ino | 8 +++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 code/CarSpotController.cpp diff --git a/code/CarSpotController.cpp b/code/CarSpotController.cpp new file mode 100644 index 0000000..bb040e2 --- /dev/null +++ b/code/CarSpotController.cpp @@ -0,0 +1,31 @@ +class CarSpotCOntroller { + + private: + int total_spots; + int occupied_spots; + + public: + CarSpotCOntroller(int default_total_spots) { + this->set_total_spots(default_total_spots); + } + + int get_total_spots() { + return this->total_spots; + } + + void set_total_spots(int total_spots) { + this->total_spots = total_spots; + } + + int get_occupied_spots() { + return this->occupied_spots; + } + + void set_occupied_spots(int occupied_spots) { + this->occupied_spots = occupied_spots; + } + + void increment_occupied_spots () { + this->occupied_spots++; + } +}; \ No newline at end of file diff --git a/code/LCDScreen.cpp b/code/LCDScreen.cpp index e9e2f64..c81db86 100644 --- a/code/LCDScreen.cpp +++ b/code/LCDScreen.cpp @@ -51,4 +51,20 @@ class LCDScreen { lcd.print(this->default_message); } + void display_parking_spots(int total_spots, int occupied_spots){ + if (this->state != DISPLAY_PARKING_SPOTS){ + this->clear(); + this->state = DISPLAY_PARKING_SPOTS; + } + int free_spots = total_spots - occupied_spots; + lcd.setCursor(0, 0); + lcd.print("Occupied: "); + lcd.print(occupied_spots); + lcd.print(" / "); + lcd.print(total_spots); + lcd.setCursor(0, 1); + lcd.print("Free: "); + lcd.print(free_spots); + } + }; \ No newline at end of file diff --git a/code/code.ino b/code/code.ino index 0e4d921..616ecac 100644 --- a/code/code.ino +++ b/code/code.ino @@ -5,6 +5,7 @@ #include "Barrier.cpp"; #include "DistSensor.cpp"; #include "LCDScreen.cpp"; +#include "CarSpotController.cpp"; #define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_ECHO 12 @@ -17,6 +18,7 @@ DallasTemperature temperature_sensors(&oneWire); DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0); Barrier barrier = Barrier(SERVO_PIN, 4); LCDScreen screen = LCDScreen(); +CarSpotCOntroller car_spot_controller = CarSpotCOntroller(24); unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured; @@ -39,6 +41,7 @@ void loop() { sensor_last_time_activated = current_time; if(barrier.is_closed()){ barrier.open(); + car_spot_controller.increment_occupied_spots(); } } else if (!dist_sensor.is_in_range() && barrier.is_open()){ if (current_time - sensor_last_time_activated >= barrier.open_time_seconds * 1000){ @@ -52,5 +55,8 @@ void loop() { temperature_in_c = temperature_sensors.getTempCByIndex(0); } - screen.display_temperature(temperature_in_c); + //screen.display_temperature(temperature_in_c); + + screen.display_parking_spots(car_spot_controller.get_total_spots(), car_spot_controller.get_occupied_spots()); + }