89 lines
2.7 KiB
C++
89 lines
2.7 KiB
C++
#include <Wire.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
#include "Barrier.cpp";
|
|
#include "DistSensor.cpp";
|
|
#include "LCDScreen.cpp";
|
|
|
|
#define PARK_SLOTS 8
|
|
#define ULTRASONIC_SENSOR_PIN_TRIG 11
|
|
#define ULTRASONIC_SENSOR_PIN_ECHO 12
|
|
#define SERVO_PIN 10
|
|
#define TEMP_SENSOR_PIN 9
|
|
#define BUTTON_PIN 8
|
|
#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, 4);
|
|
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;
|
|
int OCCUPIED_SLOTS = 0, Button_State = 0, Last_Button_State = 0;
|
|
float temperature_in_c = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
dist_sensor.configure_pins();
|
|
barrier.configure_pins();
|
|
temperature_sensors.begin();
|
|
screen.init();
|
|
pinMode(BUTTON_PIN, INPUT);
|
|
pinMode(BUZZER_PIN, OUTPUT);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
current_time = millis();
|
|
Button_State = digitalRead(BUTTON_PIN);
|
|
|
|
// Carro entra:
|
|
if (dist_sensor.is_in_range()){
|
|
sensor_last_time_activated_IN = current_time;
|
|
if(barrier.is_closed()){
|
|
barrier.open();
|
|
}
|
|
} else if (!dist_sensor.is_in_range() && barrier.is_open()){
|
|
if (current_time - sensor_last_time_activated_IN >= barrier.open_time_seconds * 1000){
|
|
barrier.close();
|
|
OCCUPIED_SLOTS++;
|
|
}
|
|
}
|
|
|
|
// Carro sai:
|
|
if (Button_State != Last_Button_State){
|
|
sensor_last_time_activated_OUT = current_time;
|
|
if(barrier.is_closed()){
|
|
barrier.open();
|
|
if (Button_State == HIGH) {
|
|
OCCUPIED_SLOTS--;
|
|
screen.display_message(OCCUPIED_SLOTS);
|
|
if (OCCUPIED_SLOTS == PARK_SLOTS) {
|
|
screen.display_message_full();
|
|
tone(BUZZER_PIN, 1000); delayMicroseconds(500);
|
|
noTone(BUZZER_PIN);
|
|
tone(BUZZER_PIN, 1000); delayMicroseconds(500);
|
|
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:
|
|
if (current_time - temperature_last_time_measured >= 1000){
|
|
temperature_last_time_measured = current_time;
|
|
temperature_sensors.requestTemperatures();
|
|
temperature_in_c = temperature_sensors.getTempCByIndex(0);
|
|
}
|
|
|
|
screen.display_temperature(temperature_in_c);
|
|
}
|