diff --git a/code/Barrier.cpp b/code/Barrier.cpp index 4be3e2a..bf5ce20 100644 --- a/code/Barrier.cpp +++ b/code/Barrier.cpp @@ -24,13 +24,17 @@ class Barrier { } void open(){ - this->state = OPEN; - this->servo.write(0); + if (this->state != OPEN){ + this->state = OPEN; + this->servo.write(0); + } } void close(){ - this->state = CLOSED; - this->servo.write(90); + if(this->state != CLOSED){ + this->state = CLOSED; + this->servo.write(90); + } } bool is_open() const { diff --git a/code/DistSensor.cpp b/code/DistSensor.cpp index 36ddef5..abe839b 100644 --- a/code/DistSensor.cpp +++ b/code/DistSensor.cpp @@ -3,34 +3,34 @@ class DistSensor { private: - int trigPin; - int echoPin; + int trig_pin; + int echo_pin; const int ULTRASONIC_CM_FACTOR = 58; void make_measurement(){ - digitalWrite(this->trigPin, HIGH); + digitalWrite(this->trig_pin, HIGH); delayMicroseconds(10); - digitalWrite(this->trigPin, LOW); + digitalWrite(this->trig_pin, LOW); } public: int duration; float default_distance; - DistSensor(int trigPin, int echoPin, float default_distance){ - this->trigPin = trigPin; - this->echoPin = echoPin; + DistSensor(int trig_pin, int echo_pin, float default_distance){ + this->trig_pin = trig_pin; + this->echo_pin = echo_pin; this->default_distance = default_distance; } void configure_pins(){ - pinMode(this->trigPin, OUTPUT); - pinMode(this->echoPin, INPUT); + pinMode(this->trig_pin, OUTPUT); + pinMode(this->echo_pin, INPUT); } int get_duration(){ this->make_measurement(); - this->duration = pulseIn(this->echoPin, HIGH); + this->duration = pulseIn(this->echo_pin, HIGH); return this->duration; } diff --git a/code/LCDScreen.cpp b/code/LCDScreen.cpp index ecdc9bb..e9e2f64 100644 --- a/code/LCDScreen.cpp +++ b/code/LCDScreen.cpp @@ -32,17 +32,22 @@ class LCDScreen { } void display_temperature(float temp){ - this->state = DISPLAY_TEMPERATURE; + if (this->state != DISPLAY_TEMPERATURE){ + this->clear(); + this->state = DISPLAY_TEMPERATURE; + } lcd.setCursor(0, 0); - this->clear(); - lcd.print(temp); - lcd.print(" C"); + lcd.print("Temp: "); + lcd.print(temp,1); + lcd.print(" C "); } void display_default_message(){ - this->state = DISPLAY_DEFAULT_MESSAGE; + if (this->state != DISPLAY_DEFAULT_MESSAGE){ + this->clear(); + this->state = DISPLAY_DEFAULT_MESSAGE; + } lcd.setCursor(0, 0); - this->clear(); lcd.print(this->default_message); } diff --git a/code/code.ino b/code/code.ino index 76306f8..0e4d921 100644 --- a/code/code.ino +++ b/code/code.ino @@ -9,23 +9,24 @@ #define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_ECHO 12 #define SERVO_PIN 10 -#define SERVO_PIN 10 #define TEMP_SENSOR_PIN 9 OneWire oneWire(TEMP_SENSOR_PIN); -DallasTemperature sensors(&oneWire); +DallasTemperature temperature_sensors(&oneWire); DistSensor dist_sensor = DistSensor(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO, 25.0); -Barrier barrier = Barrier(SERVO_PIN, 3); +Barrier barrier = Barrier(SERVO_PIN, 4); LCDScreen screen = LCDScreen(); unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured; +float temperature_in_c = 0; + void setup() { Serial.begin(9600); dist_sensor.configure_pins(); barrier.configure_pins(); - sensors.begin(); + temperature_sensors.begin(); screen.init(); } @@ -47,11 +48,9 @@ void loop() { if (current_time - temperature_last_time_measured >= 1000){ temperature_last_time_measured = current_time; - sensors.requestTemperatures(); - float tempC = sensors.getTempCByIndex(0); - screen.display_temperature(tempC); + temperature_sensors.requestTemperatures(); + temperature_in_c = temperature_sensors.getTempCByIndex(0); } - - + screen.display_temperature(temperature_in_c); }