diff --git a/code/LCDScreen.cpp b/code/LCDScreen.cpp index ecdc9bb..7f5430f 100644 --- a/code/LCDScreen.cpp +++ b/code/LCDScreen.cpp @@ -4,6 +4,7 @@ enum LCDScreenState { DISPLAY_DEFAULT_MESSAGE, DISPLAY_TEMPERATURE, + DISPLAY_PARKING_SPOTS_FULL, DISPLAY_PARKING_SPOTS }; @@ -46,4 +47,18 @@ class LCDScreen { lcd.print(this->default_message); } + void display_message_full(){ + this->state = DISPLAY_PARKING_SPOTS_FULL; + lcd.setCursor(0, 0); + this->clear(); + lcd.print("Parking Lot is FULL.\nPlease come back later.\n"); + } + + void display_message(int slots){ + this->state = DISPLAY_PARKING_SPOTS; + lcd.setCursor(0, 0); + this->clear(); + lcd.print("Available slots: "); + lcd.print(slots); lcd.print('\n'); + } }; \ No newline at end of file diff --git a/code/code.ino b/code/code.ino index 76306f8..44b560d 100644 --- a/code/code.ino +++ b/code/code.ino @@ -6,11 +6,14 @@ #include "DistSensor.cpp"; #include "LCDScreen.cpp"; +#define PARK_SLOTS = 8; #define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_ECHO 12 #define SERVO_PIN 10 #define SERVO_PIN 10 #define TEMP_SENSOR_PIN 9 +#define BUTTON_PIN = 8; +#define BUZZER_PIN = 3; OneWire oneWire(TEMP_SENSOR_PIN); DallasTemperature sensors(&oneWire); @@ -20,6 +23,7 @@ Barrier barrier = Barrier(SERVO_PIN, 3); LCDScreen screen = LCDScreen(); unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured; +int OCCUPIED_SLOTS = 0, Button_State = 0, Last_Button_State = 0; void setup() { Serial.begin(9600); @@ -27,31 +31,57 @@ void setup() { barrier.configure_pins(); sensors.begin(); screen.init(); - + pinMode(BUTTON_PIN, INPUT); + pinMode(BUZZER_PIN, OUTPUT); } void loop() { current_time = millis(); + Button_State = digitalRead(BUTTON_PIN); + // Carro entra: if (dist_sensor.is_in_range()){ - sensor_last_time_activated = current_time; + sensor_last_time_activated_IN = current_time; if(barrier.is_closed()){ barrier.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 - sensor_last_time_activated_IN >= barrier.open_time_seconds * 1000){ + barrier.close(); + OCCUPIED_SLOTS++; + } + } + + // Carro sai: + if (Button_State != Last_Button_State){ + sensor_last_time_activated_OUT = current_time; + if(barrier.is_closed()){ + barrier.open(); + if (Button_State == HIGH) { + OCCUPIED_SLOTS--; + screen.display_message(OCCUPIED_SLOTS); + if (OCCUPIED_SLOTS == PARK_SLOTS) { + screen.display_message_full(); + tone(BUZZER_PIN, 1000); delayMicroseconds(500); + noTone(BUZZER_PIN); + tone(BUZZER_PIN, 1000); delayMicroseconds(500); + noTone(BUZZER_PIN); + } + } + } + } else if (!dist_sensor.is_in_range() && barrier.is_open()){ + if (current_time - sensor_last_time_activated_OUT >= barrier.open_time_seconds * 1000){ barrier.close(); } } + Last_Button_State = Button_State; + // Temperatura: if (current_time - temperature_last_time_measured >= 1000){ temperature_last_time_measured = current_time; sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); screen.display_temperature(tempC); } - - - }