Compare commits

..

No commits in common. "62d95a77999c9767e8d6c0aeeaac7acf616ffb53" and "35d38aeba78cb4acea025d0899c4275c5da775ff" have entirely different histories.

7 changed files with 35 additions and 282 deletions

View File

@ -21,8 +21,6 @@ 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

@ -1,40 +0,0 @@
#include <Arduino.h>
class ButtonController {
private:
int pin;
int new_value;
int old_value = HIGH;
public:
ButtonController(int pin){
this->set_pin(pin);
};
void configure_pins(){
pinMode(this->pin, INPUT_PULLUP);
}
void set_pin(int pin){
this->pin = pin;
}
void read(){
this->new_value = digitalRead(this->pin);
}
bool value_changed(){
return (this->new_value != this->old_value);
}
bool is_pressed(){
if(this->value_changed()){
bool is_pressed = (this->new_value == LOW);
this->old_value = this->new_value;
return is_pressed;
}
return this->new_value == LOW;
}
};

View File

@ -1,43 +0,0 @@
class CarSpotController {
private:
int total_spots;
int occupied_spots;
public:
CarSpotController(int default_total_spots) {
this->set_total_spots(default_total_spots);
}
int get_total_spots() {
return this->total_spots;
}
void set_total_spots(int total_spots) {
this->total_spots = total_spots;
}
int get_occupied_spots() {
return this->occupied_spots;
}
void set_occupied_spots(int occupied_spots) {
this->occupied_spots = occupied_spots;
}
void increment_occupied_spots () {
this->occupied_spots++;
}
void decrement_occupied_spots () {
this->occupied_spots--;
}
bool is_full() {
return this->occupied_spots == this->total_spots;
}
bool is_empty() {
return this->occupied_spots == 0;
}
};

View File

@ -1,69 +0,0 @@
#import "Arduino.h"
class JoystickController {
private:
int pin_x;
int pin_y;
int x = 1024 / 2;
int y = 1024 / 2;
public:
JoystickController(int pin_x, int pin_y) {
this->set_pin_x(pin_x);
this->set_pin_y(pin_y);
}
void configure_pins() {
pinMode(this->pin_x, INPUT);
pinMode(this->pin_y, INPUT);
}
int get_x() {
return this->x;
}
int get_y() {
return this->y;
}
void set_x(int x) {
this->x = x;
}
void set_y(int y) {
this->y = y;
}
int get_pin_x() {
return this->pin_x;
}
void set_pin_x(int pin_x) {
this->pin_x = pin_x;
}
int get_pin_y() {
return this->pin_y;
}
void set_pin_y(int pin_y) {
this->pin_y = pin_y;
}
void read(){
this->set_y(analogRead(this->pin_y));
this->set_x(analogRead(this->pin_x));
}
bool is_moving_right(){
this->read();
return this->get_x() < 512;
}
bool is_moving_left(){
this->read();
return this->get_x() > 512;
}
};

View File

@ -2,7 +2,7 @@
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
enum LCDScreenState { enum LCDScreenState {
DISPLAY_DEFAULT_MESSAGE = 0, DISPLAY_DEFAULT_MESSAGE,
DISPLAY_TEMPERATURE, DISPLAY_TEMPERATURE,
DISPLAY_PARKING_SPOTS DISPLAY_PARKING_SPOTS
}; };
@ -11,16 +11,10 @@ class LCDScreen {
private: private:
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4); LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
LCDScreenState current_state; LCDScreenState state;
String default_message = "Bem vindo!"; String default_message = "Bem vindo!";
public: public:
LCDScreenState ordered_states[3] = {
DISPLAY_DEFAULT_MESSAGE,
DISPLAY_TEMPERATURE,
DISPLAY_PARKING_SPOTS
};
LCDScreen(String default_message = ""){ LCDScreen(String default_message = ""){
if (default_message != ""){ if (default_message != ""){
this->default_message = default_message; this->default_message = default_message;
@ -38,7 +32,10 @@ class LCDScreen {
} }
void display_temperature(float temp){ void display_temperature(float temp){
if (this->state != DISPLAY_TEMPERATURE){
this->clear();
this->state = DISPLAY_TEMPERATURE;
}
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print("Temp: "); lcd.print("Temp: ");
lcd.print(temp,1); lcd.print(temp,1);
@ -46,55 +43,12 @@ class LCDScreen {
} }
void display_default_message(){ void display_default_message(){
if (this->state != DISPLAY_DEFAULT_MESSAGE){
this->clear();
this->state = DISPLAY_DEFAULT_MESSAGE;
}
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.print(this->default_message); lcd.print(this->default_message);
} }
void display_parking_spots(int total_spots, int occupied_spots){ };
int free_spots = total_spots - occupied_spots;
lcd.setCursor(0, 0);
lcd.print("Occupied: ");
lcd.print(occupied_spots);
lcd.print(" / ");
lcd.print(total_spots);
lcd.setCursor(0, 1);
lcd.print("Free: ");
lcd.print(free_spots);
}
LCDScreenState get_next_state(){
if (this->current_state == 2){
return this->ordered_states[0];
}
return this->ordered_states[this->current_state + 1];
}
LCDScreenState get_previous_state(){
if (this->current_state == 0){
return this->ordered_states[2];
}
return this->ordered_states[this->current_state - 1];
}
void set_state(LCDScreenState state){
this->clear();
this->current_state = state;
}
void display(float temperature_in_c, int total_spots, int occupied_spots){
switch(this->current_state){
case DISPLAY_DEFAULT_MESSAGE:
this->display_default_message();
break;
case DISPLAY_TEMPERATURE:
this->display_temperature(temperature_in_c);
break;
case DISPLAY_PARKING_SPOTS:
this->display_parking_spots(total_spots, occupied_spots);
break;
}
}
};

View File

@ -5,32 +5,20 @@
#include "Barrier.cpp"; #include "Barrier.cpp";
#include "DistSensor.cpp"; #include "DistSensor.cpp";
#include "LCDScreen.cpp"; #include "LCDScreen.cpp";
#include "CarSpotController.cpp";
#include "JoyStickController.cpp";
#include "ButtonController.cpp";
#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
#define TEMP_SENSOR_PIN 9 #define TEMP_SENSOR_PIN 9
#define JOYSTICK_X_PIN A1
#define JOYSTICK_Y_PIN A0
#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);
DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0); DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0);
Barrier barrier = Barrier(SERVO_PIN, 2); Barrier barrier = Barrier(SERVO_PIN, 4);
LCDScreen screen = LCDScreen(); LCDScreen screen = LCDScreen();
CarSpotController car_spot_controller = CarSpotController(2);
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
ButtonController button = ButtonController(BUTTON_PIN);
unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured;
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;
@ -39,66 +27,30 @@ void setup() {
dist_sensor.configure_pins(); dist_sensor.configure_pins();
barrier.configure_pins(); barrier.configure_pins();
temperature_sensors.begin(); temperature_sensors.begin();
joystick.configure_pins();
button.configure_pins();
screen.init(); screen.init();
}
}
void loop() { void loop() {
current_time = millis(); current_time = millis();
button.read(); if (dist_sensor.is_in_range()){
sensor_last_time_activated = current_time;
if (button.is_pressed()){ if(barrier.is_closed()){
barrier_last_time_opened = current_time;
if (!car_spot_controller.is_empty() && barrier.is_closed()){
barrier.open(); barrier.open();
car_spot_controller.decrement_occupied_spots(); }
} else if (!dist_sensor.is_in_range() && barrier.is_open()){
if (current_time - sensor_last_time_activated >= barrier.open_time_seconds * 1000){
barrier.close();
} }
} }
if (current_time - sensor_last_time_readed >= 1000){
if (dist_sensor.is_in_range()){
barrier_last_time_opened = current_time;
if(barrier.is_closed()){
if (!car_spot_controller.is_full()){
barrier.open();
car_spot_controller.increment_occupied_spots();
}else{
tone(BUZZER_PIN, 262, 500);
}
}
} else if (!dist_sensor.is_in_range() && barrier.is_open()){
if (current_time - barrier_last_time_opened >= barrier.open_time_seconds * 1000 && !button.is_pressed()){
barrier.close();
}
}
}
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.setWaitForConversion(false);
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);
if (joystick.is_moving_right() && current_time - joystick_last_time_activated >= 450){
joystick_last_time_activated = current_time;
screen.set_state((screen.get_next_state()));
}
if (joystick.is_moving_left() && current_time - joystick_last_time_activated >= 450){
joystick_last_time_activated = current_time;
screen.set_state(screen.get_previous_state());
}
if (current_time - screen_last_time_updated >= 500){
screen_last_time_updated = current_time;
screen.display(temperature_in_c,car_spot_controller.get_total_spots(), car_spot_controller.get_occupied_spots());
}
} }

View File

@ -34,8 +34,9 @@
{ {
"type": "wokwi-analog-joystick", "type": "wokwi-analog-joystick",
"id": "joystick1", "id": "joystick1",
"top": -115.8, "top": -99.1,
"left": -90.6, "left": -62.9,
"rotate": 90,
"attrs": {} "attrs": {}
}, },
{ {
@ -87,10 +88,10 @@
[ "bb1:tp.9", "servo1:V+", "red", [ "v-94.1", "h7.6", "v-105.7" ] ], [ "bb1:tp.9", "servo1:V+", "red", [ "v-94.1", "h7.6", "v-105.7" ] ],
[ "bb1:tn.10", "servo1:GND", "black", [ "v0" ] ], [ "bb1:tn.10", "servo1:GND", "black", [ "v0" ] ],
[ "servo1:PWM", "uno:10", "orange", [ "h-124.8", "v-19", "h-58.3" ] ], [ "servo1:PWM", "uno:10", "orange", [ "h-124.8", "v-19", "h-58.3" ] ],
[ "joystick1:GND", "bb1:tn.36", "black", [ "v57.6", "h-55.6" ] ], [ "joystick1:GND", "bb1:tn.36", "black", [ "h0" ] ],
[ "joystick1:VCC", "bb1:tp.35", "red", [ "v48", "h-36.4" ] ], [ "joystick1:VCC", "bb1:tp.35", "red", [ "h0" ] ],
[ "joystick1:VERT", "bb1:42t.a", "green", [ "v67.2", "h-56.4" ] ], [ "joystick1:VERT", "bb1:42t.a", "green", [ "h0" ] ],
[ "joystick1:HORZ", "bb1:44t.a", "blue", [ "v67.2", "h-46.8" ] ], [ "joystick1:HORZ", "bb1:44t.a", "blue", [ "h0" ] ],
[ "bb1:42t.b", "uno:A0", "green", [ "v1.6", "h-327.6", "v-86.4", "h-51.8" ] ], [ "bb1:42t.b", "uno:A0", "green", [ "v1.6", "h-327.6", "v-86.4", "h-51.8" ] ],
[ "bb1:44t.c", "uno:A1", "blue", [ "v1.6", "h-337.2", "v-105.6", "h-51.9" ] ], [ "bb1:44t.c", "uno:A1", "blue", [ "v1.6", "h-337.2", "v-105.6", "h-51.9" ] ],
[ "lcd1:GND", "bb1:tn.28", "black", [ "h0" ] ], [ "lcd1:GND", "bb1:tn.28", "black", [ "h0" ] ],
@ -103,18 +104,18 @@
[ "bb1:58b.f", "bb1:58t.e", "violet", [ "v0" ] ], [ "bb1:58b.f", "bb1:58t.e", "violet", [ "v0" ] ],
[ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ], [ "uno:A4", "bb1:37b.g", "gray", [ "v0" ] ],
[ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ], [ "uno:A5", "bb1:38b.h", "purple", [ "v0" ] ],
[ "stepper1:A-", "uno:2", "green", [ "h0" ] ],
[ "stepper1:A+", "uno:4", "green", [ "h0" ] ],
[ "stepper1:B+", "uno:5", "green", [ "h0" ] ],
[ "stepper1:B-", "uno:6", "green", [ "h0" ] ],
[ "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" ] ],
[ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ], [ "btn1:1.l", "bb1:4t.c", "", [ "$bb" ] ],
[ "btn1:2.l", "bb1:2t.c", "", [ "$bb" ] ], [ "btn1:2.l", "bb1:2t.c", "", [ "$bb" ] ],
[ "btn1:1.r", "bb1:4b.h", "", [ "$bb" ] ], [ "btn1:1.r", "bb1:4b.h", "", [ "$bb" ] ],
[ "btn1:2.r", "bb1:2b.h", "", [ "$bb" ] ] [ "btn1:2.r", "bb1:2b.h", "", [ "$bb" ] ],
[ "r1:1", "bb1:58b.g", "", [ "$bb" ] ],
[ "stepper1:A-", "uno:2", "green", [ "h0" ] ],
[ "stepper1:A+", "uno:4", "green", [ "h0" ] ],
[ "stepper1:B+", "uno:5", "green", [ "h0" ] ],
[ "stepper1:B-", "uno:6", "green", [ "h0" ] ]
], ],
"dependencies": {} "dependencies": {}
} }