From 5fadd482224f4493a301143984b86e32a682b7e8 Mon Sep 17 00:00:00 2001 From: pfolhento Date: Mon, 19 May 2025 01:00:44 +0100 Subject: [PATCH 1/3] Added Button and Buzzer and other additions on LCD. --- code/LCDScreen.cpp | 15 +++++++++++++++ code/code.ino | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) 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); } - - - } From 68285fc89207be8a6f577ac3bdaaade20043242b Mon Sep 17 00:00:00 2001 From: rafa Date: Mon, 19 May 2025 01:21:04 +0100 Subject: [PATCH 2/3] Fix errors related to push putthon --- code/code.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/code.ino b/code/code.ino index e2b5a1d..99a3695 100644 --- a/code/code.ino +++ b/code/code.ino @@ -6,13 +6,13 @@ #include "DistSensor.cpp"; #include "LCDScreen.cpp"; -#define PARK_SLOTS = 8; +#define PARK_SLOTS 8 #define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_ECHO 12 #define SERVO_PIN 10 #define TEMP_SENSOR_PIN 9 -#define BUTTON_PIN = 8; -#define BUZZER_PIN = 3; +#define BUTTON_PIN 8 +#define BUZZER_PIN 3 OneWire oneWire(TEMP_SENSOR_PIN); DallasTemperature temperature_sensors(&oneWire); @@ -21,7 +21,7 @@ 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; +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; float temperature_in_c = 0; From 8765fc7a071598025e4ee441669b4f054a1a321d Mon Sep 17 00:00:00 2001 From: pfolhento Date: Wed, 21 May 2025 01:14:43 +0100 Subject: [PATCH 3/3] Fix Push Button, Modified Car state Entering Exiting. --- code/Barrier.cpp | 2 ++ code/LCDScreen.cpp | 35 ++++++++++++-------- code/code.ino | 80 ++++++++++++++++++++++++---------------------- diagram.json | 37 +++++++++++++-------- 4 files changed, 88 insertions(+), 66 deletions(-) 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