add button logic to open the barrier and decrement occupied spots

This commit is contained in:
rafa 2025-05-24 13:35:47 +01:00
parent 6b4835d1da
commit 62d95a7799

View File

@ -7,6 +7,7 @@
#include "LCDScreen.cpp"; #include "LCDScreen.cpp";
#include "CarSpotController.cpp"; #include "CarSpotController.cpp";
#include "JoyStickController.cpp"; #include "JoyStickController.cpp";
#include "ButtonController.cpp";
#define PARK_SLOTS 3 #define PARK_SLOTS 3
#define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_TRIG 11
@ -16,6 +17,7 @@
#define JOYSTICK_X_PIN A1 #define JOYSTICK_X_PIN A1
#define JOYSTICK_Y_PIN A0 #define JOYSTICK_Y_PIN A0
#define BUZZER_PIN 3 #define BUZZER_PIN 3
#define BUTTON_PIN 8
OneWire oneWire(TEMP_SENSOR_PIN); OneWire oneWire(TEMP_SENSOR_PIN);
DallasTemperature temperature_sensors(&oneWire); DallasTemperature temperature_sensors(&oneWire);
@ -25,8 +27,10 @@ Barrier barrier = Barrier(SERVO_PIN, 2);
LCDScreen screen = LCDScreen(); LCDScreen screen = LCDScreen();
CarSpotController car_spot_controller = CarSpotController(2); CarSpotController car_spot_controller = CarSpotController(2);
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN); JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
ButtonController button = ButtonController(BUTTON_PIN);
unsigned long current_time, sensor_last_time_readed, sensor_last_time_activated, temperature_last_time_measured, joystick_last_time_activated, joystick_last_time_readed, screen_last_time_updated;
unsigned long current_time, sensor_last_time_readed, barrier_last_time_opened, temperature_last_time_measured, joystick_last_time_activated, joystick_last_time_readed, screen_last_time_updated;
float temperature_in_c = 0; float temperature_in_c = 0;
@ -36,16 +40,28 @@ void setup() {
barrier.configure_pins(); barrier.configure_pins();
temperature_sensors.begin(); temperature_sensors.begin();
joystick.configure_pins(); joystick.configure_pins();
button.configure_pins();
screen.init(); screen.init();
} }
void loop() { void loop() {
current_time = millis(); current_time = millis();
button.read();
if (button.is_pressed()){
barrier_last_time_opened = current_time;
if (!car_spot_controller.is_empty() && barrier.is_closed()){
barrier.open();
car_spot_controller.decrement_occupied_spots();
}
}
if (current_time - sensor_last_time_readed >= 1000){ if (current_time - sensor_last_time_readed >= 1000){
if (dist_sensor.is_in_range()){ if (dist_sensor.is_in_range()){
sensor_last_time_activated = current_time; barrier_last_time_opened = current_time;
if(barrier.is_closed()){ if(barrier.is_closed()){
if (!car_spot_controller.is_full()){ if (!car_spot_controller.is_full()){
barrier.open(); barrier.open();
@ -55,7 +71,7 @@ void loop() {
} }
} }
} else if (!dist_sensor.is_in_range() && barrier.is_open()){ } else if (!dist_sensor.is_in_range() && barrier.is_open()){
if (current_time - sensor_last_time_activated >= barrier.open_time_seconds * 1000){ if (current_time - barrier_last_time_opened >= barrier.open_time_seconds * 1000 && !button.is_pressed()){
barrier.close(); barrier.close();
} }
} }