diff --git a/code/Barrier.cpp b/code/Barrier.cpp index bf5ce20..4f6b283 100644 --- a/code/Barrier.cpp +++ b/code/Barrier.cpp @@ -21,6 +21,8 @@ class Barrier { void configure_pins(){ this->servo.attach(pin); + this->servo.write(90); + this->state = CLOSED; } void open(){ diff --git a/code/LCDScreen.cpp b/code/LCDScreen.cpp index 3554a61..e7e8b13 100644 --- a/code/LCDScreen.cpp +++ b/code/LCDScreen.cpp @@ -3,6 +3,7 @@ enum LCDScreenState { DISPLAY_DEFAULT_MESSAGE, + DISPLAY_MESSAGE, DISPLAY_TEMPERATURE, DISPLAY_PARKING_SPOTS_FULL, DISPLAY_PARKING_SPOTS @@ -13,19 +14,20 @@ class LCDScreen { private: LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4); LCDScreenState state; - String default_message = "Bem vindo!"; + String default_message = "Welcome!"; public: LCDScreen(String default_message = ""){ if (default_message != ""){ this->default_message = default_message; + this->state = DISPLAY_DEFAULT_MESSAGE; } } void init(){ lcd.init(); lcd.backlight(); - this->display_default_message(); + this->display_message(default_message); } void clear(){ @@ -43,27 +45,34 @@ class LCDScreen { lcd.print(" C "); } - void display_default_message(){ - if (this->state != DISPLAY_DEFAULT_MESSAGE){ + void display_message(String message){ + if (this->state != DISPLAY_MESSAGE){ this->clear(); - this->state = DISPLAY_DEFAULT_MESSAGE; + this->state = DISPLAY_MESSAGE; } lcd.setCursor(0, 0); - lcd.print(this->default_message); + lcd.print(message); } void display_message_full(){ - this->state = DISPLAY_PARKING_SPOTS_FULL; + if (this->state != DISPLAY_PARKING_SPOTS_FULL){ + this->clear(); + this->state = DISPLAY_PARKING_SPOTS_FULL; + } lcd.setCursor(0, 0); - this->clear(); - lcd.print("Parking Lot is FULL.\nPlease come back later.\n"); + lcd.print("Park is FULL."); + lcd.setCursor(0, 1); + lcd.print("Come back later."); } - void display_message(int slots){ - this->state = DISPLAY_PARKING_SPOTS; + void display_message_SLOTS(int slots){ + if (this->state != DISPLAY_PARKING_SPOTS){ + this->clear(); + this->state = DISPLAY_PARKING_SPOTS; + } lcd.setCursor(0, 0); - this->clear(); lcd.print("Available slots: "); - lcd.print(slots); lcd.print('\n'); + lcd.setCursor(0, 1); + lcd.print(slots); } }; \ No newline at end of file diff --git a/code/code.ino b/code/code.ino index 99a3695..cea5041 100644 --- a/code/code.ino +++ b/code/code.ino @@ -6,7 +6,7 @@ #include "DistSensor.cpp"; #include "LCDScreen.cpp"; -#define PARK_SLOTS 8 +#define PARK_SLOTS 3 #define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_ECHO 12 #define SERVO_PIN 10 @@ -21,9 +21,12 @@ DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSO Barrier barrier = Barrier(SERVO_PIN, 4); LCDScreen screen = LCDScreen(); -unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured, sensor_last_time_activated_IN, sensor_last_time_activated_OUT; -int OCCUPIED_SLOTS = 0, Button_State = 0, Last_Button_State = 0; +unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured; +unsigned long Debounce_Delay = 1000, Debounce_Time = 0; +bool Debounce; +int SLOTS = PARK_SLOTS, Button_State = LOW, Last_Button_State = LOW; float temperature_in_c = 0; +enum CarState {ENTER, EXIT, STOP} Car_State; void setup() { Serial.begin(9600); @@ -33,56 +36,55 @@ void setup() { screen.init(); pinMode(BUTTON_PIN, INPUT); pinMode(BUZZER_PIN, OUTPUT); + Car_State = STOP; } void loop() { - current_time = millis(); Button_State = digitalRead(BUTTON_PIN); - - // Carro entra: - if (dist_sensor.is_in_range()){ - 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_IN >= barrier.open_time_seconds * 1000){ - barrier.close(); - OCCUPIED_SLOTS++; - } - } + (current_time - Debounce_Time) > Debounce_Delay ? Debounce = true : Debounce = false; - // 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){ + // Car enters (Motion Sensor, Servo, Buzzer, and LCD): + if (dist_sensor.is_in_range() && barrier.is_closed() && SLOTS != 0 && Car_State == STOP){ + sensor_last_time_activated = current_time; + barrier.open(); + Car_State = ENTER; + } else if (dist_sensor.is_in_range() && SLOTS == 0) { + screen.display_message_full(); + tone(BUZZER_PIN, 100); delayMicroseconds(500); + noTone(BUZZER_PIN); + tone(BUZZER_PIN, 100); delayMicroseconds(500); + noTone(BUZZER_PIN); + } else if (!dist_sensor.is_in_range() && barrier.is_open() && Car_State == ENTER){ + if (current_time - sensor_last_time_activated >= barrier.open_time_seconds * 1000){ barrier.close(); + screen.display_message_SLOTS(--SLOTS); + Car_State = STOP; } } - Last_Button_State = Button_State; - // Temperatura: + // Car leaves (Push Button and LCD): + if (Button_State != Last_Button_State && barrier.is_closed() && Debounce && Car_State == STOP){ + barrier.open(); + if (SLOTS < PARK_SLOTS) SLOTS++; + screen.display_message_SLOTS(SLOTS); + Debounce_Time = millis(); + Car_State = EXIT; + } + else if (Button_State != Last_Button_State && barrier.is_open() && Debounce && Car_State == EXIT){ + barrier.close(); + screen.display_message("Ate a proxima!"); + Debounce_Time = millis(); + Car_State = STOP; + } + + // Temperature (Temperature sensor, and LCD): if (current_time - temperature_last_time_measured >= 1000){ temperature_last_time_measured = current_time; temperature_sensors.requestTemperatures(); temperature_in_c = temperature_sensors.getTempCByIndex(0); } - screen.display_temperature(temperature_in_c); + //screen.display_temperature(temperature_in_c); + Last_Button_State = Button_State; } diff --git a/diagram.json b/diagram.json index 4553d45..1dfc1bf 100644 --- a/diagram.json +++ b/diagram.json @@ -3,7 +3,7 @@ "author": "Anonymous maker", "editor": "wokwi", "parts": [ - { "type": "wokwi-breadboard", "id": "bb1", "top": 72.2, "left": -524, "attrs": {} }, + { "type": "wokwi-breadboard", "id": "bb1", "top": 73.8, "left": -525.2, "attrs": {} }, { "type": "wokwi-arduino-uno", "id": "uno", "top": -201, "left": -691.8, "attrs": {} }, { "type": "wokwi-servo", @@ -13,13 +13,13 @@ "attrs": { "hornColor": "#FFFF00" } }, { "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -56.1, "left": -378.5, "attrs": {} }, - { "type": "board-ds18b20", "id": "temp1", "top": 94.87, "left": 32.88, "attrs": {} }, + { "type": "board-ds18b20", "id": "temp1", "top": 96.47, "left": 31.68, "attrs": {} }, { "type": "wokwi-ky-040", "id": "encoder1", "top": 164.9, "left": -711.2, "attrs": {} }, { "type": "wokwi-pushbutton", "id": "btn1", - "top": 153.5, - "left": -514.9, + "top": 153, + "left": -515.4, "rotate": 90, "attrs": { "color": "yellow", "xray": "1", "bounce": "1" } }, @@ -49,10 +49,17 @@ { "type": "wokwi-resistor", "id": "r1", - "top": 225.6, - "left": 18.65, + "top": 227.2, + "left": 17.45, "rotate": 90, "attrs": { "value": "4700" } + }, + { + "type": "wokwi-resistor", + "id": "r2", + "top": 215.45, + "left": -471.8, + "attrs": { "value": "10000" } } ], "connections": [ @@ -63,8 +70,6 @@ [ "uno:GND.2", "bb1:tn.1", "black", [ "v0" ] ], [ "bb1:tn.50", "bb1:bn.50", "black", [ "h28", "v174" ] ], [ "bb1:tp.50", "bb1:bp.50", "red", [ "h37.6", "v174" ] ], - [ "bb1:bn.2", "bb1:4b.j", "black", [ "v0" ] ], - [ "bb1:2t.b", "uno:8", "gold", [ "v-240", "h-19.2", "v-86.4" ] ], [ "bb1:59b.j", "bb1:bp.48", "red", [ "v0" ] ], [ "bb1:57b.j", "bb1:bn.46", "black", [ "v0" ] ], [ "encoder1:VCC", "bb1:bp.1", "red", [ "h57.6", "v58.9" ] ], @@ -94,16 +99,20 @@ [ "bb1:57b.f", "bb1:57t.e", "black", [ "v0" ] ], [ "uno:9", "bb1:58t.d", "blue", [ "v480", "h519.2", "v-136" ] ], [ "bb1:58b.f", "bb1:58t.e", "violet", [ "v0" ] ], - [ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ], - [ "btn1:2.l", "bb1:2t.c", "", [ "$bb" ] ], - [ "btn1:1.r", "bb1:4b.h", "", [ "$bb" ] ], - [ "btn1:2.r", "bb1:2b.h", "", [ "$bb" ] ], + [ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ], + [ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ], + [ "bb1:10b.j", "bb1:bn.7", "black", [ "v0" ] ], [ "r1:1", "bb1:58b.g", "", [ "$bb" ] ], [ "temp1:GND", "bb1:57t.c", "", [ "$bb" ] ], [ "temp1:DQ", "bb1:58t.c", "", [ "$bb" ] ], [ "temp1:VCC", "bb1:59t.c", "", [ "$bb" ] ], - [ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ], - [ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ] + [ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ], + [ "btn1:2.l", "bb1:2t.c", "", [ "$bb" ] ], + [ "btn1:1.r", "bb1:4b.h", "", [ "$bb" ] ], + [ "btn1:2.r", "bb1:2b.h", "", [ "$bb" ] ], + [ "uno:8", "bb1:2t.b", "yellow", [ "h0.4", "v326.4" ] ], + [ "bb1:tp.2", "bb1:4t.b", "red", [ "v0" ] ], + [ "r2:2", "bb1:7b.f", "", [ "$bb" ] ] ], "dependencies": {} } \ No newline at end of file