SE-TP2/code/code.ino

89 lines
2.9 KiB
C++

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "Barrier.cpp";
#include "DistSensor.cpp";
#include "LCDScreen.cpp";
#include "CarSpotController.cpp";
#include "JoyStickController.cpp";
#define PARK_SLOTS 3
#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
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(2);
JoystickController joystick = JoystickController(JOYSTICK_X_PIN, JOYSTICK_Y_PIN);
unsigned long current_time, sensor_last_time_readed, sensor_last_time_activated, temperature_last_time_measured, joystick_last_time_activated, joystick_last_time_readed, screen_last_time_updated;
float temperature_in_c = 0;
void setup() {
Serial.begin(9600);
dist_sensor.configure_pins();
barrier.configure_pins();
temperature_sensors.begin();
joystick.configure_pins();
screen.init();
}
void loop() {
current_time = millis();
if (current_time - sensor_last_time_readed >= 1000){
if (dist_sensor.is_in_range()){
sensor_last_time_activated = 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 - sensor_last_time_activated >= barrier.open_time_seconds * 1000){
barrier.close();
}
}
}
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());
}
}