Compare commits
4 Commits
1fecdaee61
...
5b2e505571
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b2e505571 | |||
| 1518fbf51a | |||
| 3269642002 | |||
| bb933c847b |
@ -29,6 +29,14 @@ class CarSpotController {
|
|||||||
this->occupied_spots++;
|
this->occupied_spots++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void increment_total_spots () {
|
||||||
|
this->total_spots++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void decrement_total_spots () {
|
||||||
|
this->total_spots--;
|
||||||
|
}
|
||||||
|
|
||||||
void decrement_occupied_spots () {
|
void decrement_occupied_spots () {
|
||||||
this->occupied_spots--;
|
this->occupied_spots--;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ class DistSensor {
|
|||||||
|
|
||||||
void make_measurement(){
|
void make_measurement(){
|
||||||
digitalWrite(this->trig_pin, HIGH);
|
digitalWrite(this->trig_pin, HIGH);
|
||||||
delayMicroseconds(10);
|
//delayMicroseconds(10);
|
||||||
digitalWrite(this->trig_pin, LOW);
|
digitalWrite(this->trig_pin, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
90
code/EncoderController.cpp
Normal file
90
code/EncoderController.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
enum EncoderDirection {
|
||||||
|
COUNTERCLOCKWISE = 2,
|
||||||
|
STOPPED = 0,
|
||||||
|
CLOCKWISE = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ButtonState {
|
||||||
|
PRESSED = 0,
|
||||||
|
RELEASED = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
class EncoderController {
|
||||||
|
|
||||||
|
private:
|
||||||
|
int pin_clk;
|
||||||
|
int pin_dt;
|
||||||
|
int last_clk = HIGH;
|
||||||
|
int new_clk;
|
||||||
|
int dt_value;
|
||||||
|
EncoderDirection current_direction;
|
||||||
|
int pin_btn;
|
||||||
|
int button;
|
||||||
|
bool last_button_state = HIGH; // começa como solto
|
||||||
|
bool button_click_registered = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
EncoderController(int pin_dt, int pin_clk, int pin_btn) {
|
||||||
|
this->pin_dt = pin_dt;
|
||||||
|
this->pin_clk = pin_clk;
|
||||||
|
this->pin_btn = pin_btn;
|
||||||
|
}
|
||||||
|
|
||||||
|
void configure_pins() {
|
||||||
|
pinMode(this->pin_dt, INPUT_PULLUP);
|
||||||
|
pinMode(this->pin_clk, INPUT_PULLUP);
|
||||||
|
pinMode(this->pin_btn, INPUT_PULLUP);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool clk_pin_changed(){
|
||||||
|
this->new_clk = digitalRead(this->pin_clk);
|
||||||
|
return this->new_clk != this->last_clk;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(){
|
||||||
|
if (this->clk_pin_changed()) {
|
||||||
|
this->last_clk = this->new_clk;
|
||||||
|
this->dt_value = digitalRead(this->pin_dt);
|
||||||
|
if (this->new_clk == LOW && this->dt_value == HIGH) {
|
||||||
|
|
||||||
|
this->current_direction = CLOCKWISE;
|
||||||
|
}
|
||||||
|
if (this->new_clk == LOW && this->dt_value == LOW) {
|
||||||
|
|
||||||
|
this->current_direction = COUNTERCLOCKWISE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int current_state = digitalRead(this->pin_btn);
|
||||||
|
if (this->last_button_state == HIGH && current_state == LOW) {
|
||||||
|
this->button_click_registered = true;
|
||||||
|
}
|
||||||
|
this->last_button_state = current_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rotated_clockwise(){
|
||||||
|
if(this->current_direction == CLOCKWISE){
|
||||||
|
this->current_direction = STOPPED;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rotated_counterclockwise(){
|
||||||
|
if(this->current_direction == COUNTERCLOCKWISE){
|
||||||
|
this->current_direction = STOPPED;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool button_pressed(){
|
||||||
|
if (this->button_click_registered) {
|
||||||
|
this->button_click_registered = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -4,14 +4,15 @@
|
|||||||
enum LCDScreenState {
|
enum LCDScreenState {
|
||||||
DISPLAY_DEFAULT_MESSAGE = 0,
|
DISPLAY_DEFAULT_MESSAGE = 0,
|
||||||
DISPLAY_TEMPERATURE,
|
DISPLAY_TEMPERATURE,
|
||||||
DISPLAY_PARKING_SPOTS
|
DISPLAY_PARKING_SPOTS,
|
||||||
|
CONFIGURATION_SCREEN
|
||||||
};
|
};
|
||||||
|
|
||||||
class LCDScreen {
|
class LCDScreen {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
|
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
|
||||||
LCDScreenState current_state;
|
LCDScreenState current_state = 0;
|
||||||
String default_message = "Bem vindo!";
|
String default_message = "Bem vindo!";
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -64,6 +65,23 @@ class LCDScreen {
|
|||||||
lcd.print(free_spots);
|
lcd.print(free_spots);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void display_configuration_screen(int total_spots){
|
||||||
|
if (this->current_state != CONFIGURATION_SCREEN){
|
||||||
|
this->current_state = CONFIGURATION_SCREEN;
|
||||||
|
this->clear();
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("Conf. Mode");
|
||||||
|
}
|
||||||
|
lcd.setCursor(0, 1);
|
||||||
|
lcd.print("Total Spots = ");
|
||||||
|
if (total_spots < 10){
|
||||||
|
lcd.print("0");
|
||||||
|
lcd.print(total_spots);
|
||||||
|
}else {
|
||||||
|
lcd.print(total_spots);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LCDScreenState get_next_state(){
|
LCDScreenState get_next_state(){
|
||||||
if (this->current_state == 2){
|
if (this->current_state == 2){
|
||||||
return this->ordered_states[0];
|
return this->ordered_states[0];
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include "CarSpotController.cpp";
|
#include "CarSpotController.cpp";
|
||||||
#include "JoyStickController.cpp";
|
#include "JoyStickController.cpp";
|
||||||
#include "ButtonController.cpp";
|
#include "ButtonController.cpp";
|
||||||
|
#include "EncoderController.cpp";
|
||||||
|
|
||||||
#define ULTRASONIC_SENSOR_PIN_TRIG 11
|
#define ULTRASONIC_SENSOR_PIN_TRIG 11
|
||||||
#define ULTRASONIC_SENSOR_PIN_ECHO 12
|
#define ULTRASONIC_SENSOR_PIN_ECHO 12
|
||||||
@ -22,10 +23,18 @@
|
|||||||
#define STEPPER_B_PLUS_PIN 5
|
#define STEPPER_B_PLUS_PIN 5
|
||||||
#define STEPPER_A_PLUS_PIN 4
|
#define STEPPER_A_PLUS_PIN 4
|
||||||
#define STEPPER_A_MINUS_PIN 2
|
#define STEPPER_A_MINUS_PIN 2
|
||||||
|
#define ENCODER_CLK_PIN 7
|
||||||
|
#define ENCODER_DT_PIN A2
|
||||||
|
#define ENCODER_SW_PIN A3
|
||||||
|
|
||||||
#define PARK_SLOTS 3
|
#define PARK_SLOTS 3
|
||||||
#define STEP_MOTOR_STEPS 200
|
#define STEP_MOTOR_STEPS 200
|
||||||
|
|
||||||
|
enum ProgramState {
|
||||||
|
CONFIG_MODE,
|
||||||
|
APP_MODE
|
||||||
|
};
|
||||||
|
|
||||||
OneWire oneWire(TEMP_SENSOR_PIN);
|
OneWire oneWire(TEMP_SENSOR_PIN);
|
||||||
DallasTemperature temperature_sensors(&oneWire);
|
DallasTemperature temperature_sensors(&oneWire);
|
||||||
|
|
||||||
@ -36,29 +45,69 @@ CarSpotController car_spot_controller = CarSpotController(PARK_SLOTS);
|
|||||||
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
|
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
|
||||||
ButtonController button = ButtonController(BUTTON_PIN);
|
ButtonController button = ButtonController(BUTTON_PIN);
|
||||||
Stepper stepper(STEP_MOTOR_STEPS, STEPPER_B_MINUS_PIN, STEPPER_B_PLUS_PIN, STEPPER_A_PLUS_PIN, STEPPER_A_MINUS_PIN);
|
Stepper stepper(STEP_MOTOR_STEPS, STEPPER_B_MINUS_PIN, STEPPER_B_PLUS_PIN, STEPPER_A_PLUS_PIN, STEPPER_A_MINUS_PIN);
|
||||||
|
EncoderController encoder = EncoderController(ENCODER_DT_PIN, ENCODER_CLK_PIN,ENCODER_SW_PIN);
|
||||||
|
|
||||||
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;
|
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, encoder_last_time_readed;
|
||||||
|
|
||||||
float temperature_in_c = 0;
|
float temperature_in_c = 0;
|
||||||
|
|
||||||
|
ProgramState program_state = CONFIG_MODE;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(115200);
|
||||||
dist_sensor.configure_pins();
|
dist_sensor.configure_pins();
|
||||||
barrier.configure_pins();
|
barrier.configure_pins();
|
||||||
temperature_sensors.begin();
|
temperature_sensors.begin();
|
||||||
joystick.configure_pins();
|
joystick.configure_pins();
|
||||||
button.configure_pins();
|
button.configure_pins();
|
||||||
stepper.setSpeed(60);
|
stepper.setSpeed(60);
|
||||||
|
encoder.configure_pins();
|
||||||
screen.init();
|
screen.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
current_time = millis();
|
current_time = millis();
|
||||||
|
|
||||||
button.read();
|
switch (program_state){
|
||||||
|
case CONFIG_MODE:
|
||||||
|
run_config_mode();
|
||||||
|
break;
|
||||||
|
case APP_MODE:
|
||||||
|
run_program();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_config_mode (){
|
||||||
|
static bool is_first_time = true;
|
||||||
|
if (is_first_time){
|
||||||
|
screen.display_configuration_screen(car_spot_controller.get_total_spots());
|
||||||
|
is_first_time = false;
|
||||||
|
}
|
||||||
|
encoder.read();
|
||||||
|
if (encoder.rotated_clockwise()){
|
||||||
|
if (car_spot_controller.get_total_spots() < 15){
|
||||||
|
car_spot_controller.increment_total_spots();
|
||||||
|
screen.display_configuration_screen(car_spot_controller.get_total_spots());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (encoder.rotated_counterclockwise()){
|
||||||
|
if (car_spot_controller.get_total_spots() > 1 && car_spot_controller.get_occupied_spots() < car_spot_controller.get_total_spots()){
|
||||||
|
car_spot_controller.decrement_total_spots();
|
||||||
|
screen.display_configuration_screen(car_spot_controller.get_total_spots());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (encoder.button_pressed()){
|
||||||
|
program_state = APP_MODE;
|
||||||
|
screen.set_state(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_program(){
|
||||||
|
|
||||||
|
button.read();
|
||||||
if (button.is_pressed()){
|
if (button.is_pressed()){
|
||||||
barrier_last_time_opened = current_time;
|
barrier_last_time_opened = current_time;
|
||||||
if (!car_spot_controller.is_empty() && barrier.is_closed()){
|
if (!car_spot_controller.is_empty() && barrier.is_closed()){
|
||||||
@ -88,7 +137,6 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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.setWaitForConversion(false);
|
||||||
@ -96,7 +144,6 @@ void loop() {
|
|||||||
temperature_in_c = temperature_sensors.getTempCByIndex(0);
|
temperature_in_c = temperature_sensors.getTempCByIndex(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (joystick.is_moving_right() && current_time - joystick_last_time_activated >= 450){
|
if (joystick.is_moving_right() && current_time - joystick_last_time_activated >= 450){
|
||||||
joystick_last_time_activated = current_time;
|
joystick_last_time_activated = current_time;
|
||||||
screen.set_state((screen.get_next_state()));
|
screen.set_state((screen.get_next_state()));
|
||||||
@ -106,10 +153,15 @@ void loop() {
|
|||||||
screen.set_state(screen.get_previous_state());
|
screen.set_state(screen.get_previous_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (current_time - screen_last_time_updated >= 500){
|
if (current_time - screen_last_time_updated >= 500){
|
||||||
screen_last_time_updated = current_time;
|
screen_last_time_updated = current_time;
|
||||||
screen.display(temperature_in_c,car_spot_controller.get_total_spots(), car_spot_controller.get_occupied_spots());
|
screen.display(temperature_in_c,car_spot_controller.get_total_spots(), car_spot_controller.get_occupied_spots());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
encoder.read();
|
||||||
|
if (encoder.button_pressed()){
|
||||||
|
program_state = CONFIG_MODE;
|
||||||
|
screen.display_configuration_screen(car_spot_controller.get_total_spots());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user