167 lines
5.4 KiB
C++
167 lines
5.4 KiB
C++
#include <Wire.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include <Stepper.h>
|
|
|
|
#include "Barrier.cpp";
|
|
#include "DistSensor.cpp";
|
|
#include "LCDScreen.cpp";
|
|
#include "CarSpotController.cpp";
|
|
#include "JoyStickController.cpp";
|
|
#include "ButtonController.cpp";
|
|
#include "EncoderController.cpp";
|
|
|
|
#define ULTRASONIC_SENSOR_PIN_TRIG 11
|
|
#define ULTRASONIC_SENSOR_PIN_ECHO 12
|
|
#define SERVO_PIN 10
|
|
#define TEMP_SENSOR_PIN 9
|
|
#define JOYSTICK_X_PIN A1
|
|
#define JOYSTICK_Y_PIN A0
|
|
#define BUZZER_PIN 3
|
|
#define BUTTON_PIN 8
|
|
#define STEPPER_B_MINUS_PIN 6
|
|
#define STEPPER_B_PLUS_PIN 5
|
|
#define STEPPER_A_PLUS_PIN 4
|
|
#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 STEP_MOTOR_STEPS 200
|
|
|
|
enum ProgramState {
|
|
CONFIG_MODE,
|
|
APP_MODE
|
|
};
|
|
|
|
OneWire oneWire(TEMP_SENSOR_PIN);
|
|
DallasTemperature temperature_sensors(&oneWire);
|
|
|
|
DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0);
|
|
Barrier barrier = Barrier(SERVO_PIN, 2);
|
|
LCDScreen screen = LCDScreen();
|
|
CarSpotController car_spot_controller = CarSpotController(PARK_SLOTS);
|
|
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_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);
|
|
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, encoder_last_time_readed;
|
|
|
|
float temperature_in_c = 0;
|
|
|
|
ProgramState program_state = CONFIG_MODE;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
dist_sensor.configure_pins();
|
|
barrier.configure_pins();
|
|
temperature_sensors.begin();
|
|
joystick.configure_pins();
|
|
button.configure_pins();
|
|
stepper.setSpeed(60);
|
|
encoder.configure_pins();
|
|
screen.init();
|
|
}
|
|
|
|
void loop() {
|
|
|
|
current_time = millis();
|
|
|
|
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()){
|
|
barrier_last_time_opened = current_time;
|
|
if (!car_spot_controller.is_empty() && barrier.is_closed()){
|
|
barrier.open();
|
|
stepper.step(STEP_MOTOR_STEPS);
|
|
car_spot_controller.decrement_occupied_spots();
|
|
}
|
|
}
|
|
|
|
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();
|
|
stepper.step(STEP_MOTOR_STEPS);
|
|
}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();
|
|
stepper.step(-STEP_MOTOR_STEPS);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (current_time - temperature_last_time_measured >= 1000){
|
|
temperature_last_time_measured = current_time;
|
|
temperature_sensors.setWaitForConversion(false);
|
|
temperature_sensors.requestTemperatures();
|
|
temperature_in_c = temperature_sensors.getTempCByIndex(0);
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
encoder.read();
|
|
if (encoder.button_pressed()){
|
|
program_state = CONFIG_MODE;
|
|
screen.display_configuration_screen(car_spot_controller.get_total_spots());
|
|
return;
|
|
}
|
|
} |