#include #include #include #include "Barrier.cpp"; #include "DistSensor.cpp"; #include "LCDScreen.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 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; unsigned long Debounce_Delay = 1000, Debounce_Time = 0; bool Debounce; int SLOTS = PARK_SLOTS, Button_State = LOW, Last_Button_State = LOW; float temperature_in_c = 0; enum CarState {ENTER, EXIT, STOP} Car_State; 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); Car_State = STOP; } void loop() { current_time = millis(); Button_State = digitalRead(BUTTON_PIN); (current_time - Debounce_Time) > Debounce_Delay ? Debounce = true : Debounce = false; // Car enters (Motion Sensor, Servo, Buzzer, and LCD): if (dist_sensor.is_in_range() && barrier.is_closed() && SLOTS != 0 && Car_State == STOP){ sensor_last_time_activated = current_time; barrier.open(); Car_State = ENTER; } else if (dist_sensor.is_in_range() && SLOTS == 0) { screen.display_message_full(); tone(BUZZER_PIN, 100); delayMicroseconds(500); noTone(BUZZER_PIN); tone(BUZZER_PIN, 100); delayMicroseconds(500); noTone(BUZZER_PIN); } else if (!dist_sensor.is_in_range() && barrier.is_open() && Car_State == ENTER){ if (current_time - sensor_last_time_activated >= barrier.open_time_seconds * 1000){ barrier.close(); screen.display_message_SLOTS(--SLOTS); Car_State = STOP; } } // Car leaves (Push Button and LCD): if (Button_State != Last_Button_State && barrier.is_closed() && Debounce && Car_State == STOP){ barrier.open(); if (SLOTS < PARK_SLOTS) SLOTS++; screen.display_message_SLOTS(SLOTS); Debounce_Time = millis(); Car_State = EXIT; } else if (Button_State != Last_Button_State && barrier.is_open() && Debounce && Car_State == EXIT){ barrier.close(); screen.display_message("Ate a proxima!"); Debounce_Time = millis(); Car_State = STOP; } // Temperature (Temperature sensor, and LCD): 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); Last_Button_State = Button_State; }