Update stable version 24/05/2025 #2

Merged
rafa merged 29 commits from dev into main 2025-05-24 20:00:44 +02:00
4 changed files with 88 additions and 66 deletions
Showing only changes of commit 8765fc7a07 - Show all commits

View File

@ -21,6 +21,8 @@ class Barrier {
void configure_pins(){ void configure_pins(){
this->servo.attach(pin); this->servo.attach(pin);
this->servo.write(90);
this->state = CLOSED;
} }
void open(){ void open(){

View File

@ -3,6 +3,7 @@
enum LCDScreenState { enum LCDScreenState {
DISPLAY_DEFAULT_MESSAGE, DISPLAY_DEFAULT_MESSAGE,
DISPLAY_MESSAGE,
DISPLAY_TEMPERATURE, DISPLAY_TEMPERATURE,
DISPLAY_PARKING_SPOTS_FULL, DISPLAY_PARKING_SPOTS_FULL,
DISPLAY_PARKING_SPOTS DISPLAY_PARKING_SPOTS
@ -13,19 +14,20 @@ class LCDScreen {
private: private:
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4); LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
LCDScreenState state; LCDScreenState state;
String default_message = "Bem vindo!"; String default_message = "Welcome!";
public: public:
LCDScreen(String default_message = ""){ LCDScreen(String default_message = ""){
if (default_message != ""){ if (default_message != ""){
this->default_message = default_message; this->default_message = default_message;
this->state = DISPLAY_DEFAULT_MESSAGE;
} }
} }
void init(){ void init(){
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
this->display_default_message(); this->display_message(default_message);
} }
void clear(){ void clear(){
@ -43,27 +45,34 @@ class LCDScreen {
lcd.print(" C "); lcd.print(" C ");
} }
void display_default_message(){ void display_message(String message){
if (this->state != DISPLAY_DEFAULT_MESSAGE){ if (this->state != DISPLAY_MESSAGE){
this->clear(); this->clear();
this->state = DISPLAY_DEFAULT_MESSAGE; this->state = DISPLAY_MESSAGE;
} }
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print(this->default_message); lcd.print(message);
} }
void display_message_full(){ 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); lcd.setCursor(0, 0);
this->clear(); lcd.print("Park is FULL.");
lcd.print("Parking Lot is FULL.\nPlease come back later.\n"); lcd.setCursor(0, 1);
lcd.print("Come back later.");
} }
void display_message(int slots){ void display_message_SLOTS(int slots){
this->state = DISPLAY_PARKING_SPOTS; if (this->state != DISPLAY_PARKING_SPOTS){
this->clear();
this->state = DISPLAY_PARKING_SPOTS;
}
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
this->clear();
lcd.print("Available slots: "); lcd.print("Available slots: ");
lcd.print(slots); lcd.print('\n'); lcd.setCursor(0, 1);
lcd.print(slots);
} }
}; };

View File

@ -6,7 +6,7 @@
#include "DistSensor.cpp"; #include "DistSensor.cpp";
#include "LCDScreen.cpp"; #include "LCDScreen.cpp";
#define PARK_SLOTS 8 #define PARK_SLOTS 3
#define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_TRIG 11
#define ULTRASONIC_SENSOR_PIN_ECHO 12 #define ULTRASONIC_SENSOR_PIN_ECHO 12
#define SERVO_PIN 10 #define SERVO_PIN 10
@ -21,9 +21,12 @@ DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSO
Barrier barrier = Barrier(SERVO_PIN, 4); Barrier barrier = Barrier(SERVO_PIN, 4);
LCDScreen screen = LCDScreen(); 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; unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured;
int OCCUPIED_SLOTS = 0, Button_State = 0, Last_Button_State = 0; 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; float temperature_in_c = 0;
enum CarState {ENTER, EXIT, STOP} Car_State;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -33,56 +36,55 @@ void setup() {
screen.init(); screen.init();
pinMode(BUTTON_PIN, INPUT); pinMode(BUTTON_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT); pinMode(BUZZER_PIN, OUTPUT);
Car_State = STOP;
} }
void loop() { void loop() {
current_time = millis(); current_time = millis();
Button_State = digitalRead(BUTTON_PIN); Button_State = digitalRead(BUTTON_PIN);
(current_time - Debounce_Time) > Debounce_Delay ? Debounce = true : Debounce = false;
// Carro entra: // Car enters (Motion Sensor, Servo, Buzzer, and LCD):
if (dist_sensor.is_in_range()){ if (dist_sensor.is_in_range() && barrier.is_closed() && SLOTS != 0 && Car_State == STOP){
sensor_last_time_activated_IN = current_time; sensor_last_time_activated = current_time;
if(barrier.is_closed()){ barrier.open();
barrier.open(); Car_State = ENTER;
} } else if (dist_sensor.is_in_range() && SLOTS == 0) {
} else if (!dist_sensor.is_in_range() && barrier.is_open()){ screen.display_message_full();
if (current_time - sensor_last_time_activated_IN >= barrier.open_time_seconds * 1000){ 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(); barrier.close();
OCCUPIED_SLOTS++; screen.display_message_SLOTS(--SLOTS);
Car_State = STOP;
} }
} }
// Carro sai: // Car leaves (Push Button and LCD):
if (Button_State != Last_Button_State){ if (Button_State != Last_Button_State && barrier.is_closed() && Debounce && Car_State == STOP){
sensor_last_time_activated_OUT = current_time; barrier.open();
if(barrier.is_closed()){ if (SLOTS < PARK_SLOTS) SLOTS++;
barrier.open(); screen.display_message_SLOTS(SLOTS);
if (Button_State == HIGH) { Debounce_Time = millis();
OCCUPIED_SLOTS--; Car_State = EXIT;
screen.display_message(OCCUPIED_SLOTS); }
if (OCCUPIED_SLOTS == PARK_SLOTS) { else if (Button_State != Last_Button_State && barrier.is_open() && Debounce && Car_State == EXIT){
screen.display_message_full(); barrier.close();
tone(BUZZER_PIN, 1000); delayMicroseconds(500); screen.display_message("Ate a proxima!");
noTone(BUZZER_PIN); Debounce_Time = millis();
tone(BUZZER_PIN, 1000); delayMicroseconds(500); Car_State = STOP;
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: // Temperature (Temperature sensor, and LCD):
if (current_time - temperature_last_time_measured >= 1000){ if (current_time - temperature_last_time_measured >= 1000){
temperature_last_time_measured = current_time; temperature_last_time_measured = current_time;
temperature_sensors.requestTemperatures(); temperature_sensors.requestTemperatures();
temperature_in_c = temperature_sensors.getTempCByIndex(0); temperature_in_c = temperature_sensors.getTempCByIndex(0);
} }
screen.display_temperature(temperature_in_c); //screen.display_temperature(temperature_in_c);
Last_Button_State = Button_State;
} }

View File

@ -3,7 +3,7 @@
"author": "Anonymous maker", "author": "Anonymous maker",
"editor": "wokwi", "editor": "wokwi",
"parts": [ "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-arduino-uno", "id": "uno", "top": -201, "left": -691.8, "attrs": {} },
{ {
"type": "wokwi-servo", "type": "wokwi-servo",
@ -13,13 +13,13 @@
"attrs": { "hornColor": "#FFFF00" } "attrs": { "hornColor": "#FFFF00" }
}, },
{ "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -56.1, "left": -378.5, "attrs": {} }, { "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-ky-040", "id": "encoder1", "top": 164.9, "left": -711.2, "attrs": {} },
{ {
"type": "wokwi-pushbutton", "type": "wokwi-pushbutton",
"id": "btn1", "id": "btn1",
"top": 153.5, "top": 153,
"left": -514.9, "left": -515.4,
"rotate": 90, "rotate": 90,
"attrs": { "color": "yellow", "xray": "1", "bounce": "1" } "attrs": { "color": "yellow", "xray": "1", "bounce": "1" }
}, },
@ -49,10 +49,17 @@
{ {
"type": "wokwi-resistor", "type": "wokwi-resistor",
"id": "r1", "id": "r1",
"top": 225.6, "top": 227.2,
"left": 18.65, "left": 17.45,
"rotate": 90, "rotate": 90,
"attrs": { "value": "4700" } "attrs": { "value": "4700" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 215.45,
"left": -471.8,
"attrs": { "value": "10000" }
} }
], ],
"connections": [ "connections": [
@ -63,8 +70,6 @@
[ "uno:GND.2", "bb1:tn.1", "black", [ "v0" ] ], [ "uno:GND.2", "bb1:tn.1", "black", [ "v0" ] ],
[ "bb1:tn.50", "bb1:bn.50", "black", [ "h28", "v174" ] ], [ "bb1:tn.50", "bb1:bn.50", "black", [ "h28", "v174" ] ],
[ "bb1:tp.50", "bb1:bp.50", "red", [ "h37.6", "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:59b.j", "bb1:bp.48", "red", [ "v0" ] ],
[ "bb1:57b.j", "bb1:bn.46", "black", [ "v0" ] ], [ "bb1:57b.j", "bb1:bn.46", "black", [ "v0" ] ],
[ "encoder1:VCC", "bb1:bp.1", "red", [ "h57.6", "v58.9" ] ], [ "encoder1:VCC", "bb1:bp.1", "red", [ "h57.6", "v58.9" ] ],
@ -94,16 +99,20 @@
[ "bb1:57b.f", "bb1:57t.e", "black", [ "v0" ] ], [ "bb1:57b.f", "bb1:57t.e", "black", [ "v0" ] ],
[ "uno:9", "bb1:58t.d", "blue", [ "v480", "h519.2", "v-136" ] ], [ "uno:9", "bb1:58t.d", "blue", [ "v480", "h519.2", "v-136" ] ],
[ "bb1:58b.f", "bb1:58t.e", "violet", [ "v0" ] ], [ "bb1:58b.f", "bb1:58t.e", "violet", [ "v0" ] ],
[ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ], [ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ],
[ "btn1:2.l", "bb1:2t.c", "", [ "$bb" ] ], [ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ],
[ "btn1:1.r", "bb1:4b.h", "", [ "$bb" ] ], [ "bb1:10b.j", "bb1:bn.7", "black", [ "v0" ] ],
[ "btn1:2.r", "bb1:2b.h", "", [ "$bb" ] ],
[ "r1:1", "bb1:58b.g", "", [ "$bb" ] ], [ "r1:1", "bb1:58b.g", "", [ "$bb" ] ],
[ "temp1:GND", "bb1:57t.c", "", [ "$bb" ] ], [ "temp1:GND", "bb1:57t.c", "", [ "$bb" ] ],
[ "temp1:DQ", "bb1:58t.c", "", [ "$bb" ] ], [ "temp1:DQ", "bb1:58t.c", "", [ "$bb" ] ],
[ "temp1:VCC", "bb1:59t.c", "", [ "$bb" ] ], [ "temp1:VCC", "bb1:59t.c", "", [ "$bb" ] ],
[ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ], [ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ],
[ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ] [ "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": {} "dependencies": {}
} }