Compare commits

...

4 Commits

Author SHA1 Message Date
35d38aeba7 Merge branch 'main' into dev 2025-05-23 11:40:03 +01:00
87a69782e5 change to snake_case 2025-05-18 11:32:38 +01:00
f16a853b42 Change temperature related variables names 2025-05-17 17:30:44 +01:00
8c78890058 Add state checking 2025-05-17 17:24:02 +01:00
4 changed files with 37 additions and 29 deletions

View File

@ -24,13 +24,17 @@ class Barrier {
} }
void open(){ void open(){
this->state = OPEN; if (this->state != OPEN){
this->servo.write(0); this->state = OPEN;
this->servo.write(0);
}
} }
void close(){ void close(){
this->state = CLOSED; if(this->state != CLOSED){
this->servo.write(90); this->state = CLOSED;
this->servo.write(90);
}
} }
bool is_open() const { bool is_open() const {

View File

@ -3,34 +3,34 @@
class DistSensor { class DistSensor {
private: private:
int trigPin; int trig_pin;
int echoPin; int echo_pin;
const int ULTRASONIC_CM_FACTOR = 58; const int ULTRASONIC_CM_FACTOR = 58;
void make_measurement(){ void make_measurement(){
digitalWrite(this->trigPin, HIGH); digitalWrite(this->trig_pin, HIGH);
delayMicroseconds(10); delayMicroseconds(10);
digitalWrite(this->trigPin, LOW); digitalWrite(this->trig_pin, LOW);
} }
public: public:
int duration; int duration;
float default_distance; float default_distance;
DistSensor(int trigPin, int echoPin, float default_distance){ DistSensor(int trig_pin, int echo_pin, float default_distance){
this->trigPin = trigPin; this->trig_pin = trig_pin;
this->echoPin = echoPin; this->echo_pin = echo_pin;
this->default_distance = default_distance; this->default_distance = default_distance;
} }
void configure_pins(){ void configure_pins(){
pinMode(this->trigPin, OUTPUT); pinMode(this->trig_pin, OUTPUT);
pinMode(this->echoPin, INPUT); pinMode(this->echo_pin, INPUT);
} }
int get_duration(){ int get_duration(){
this->make_measurement(); this->make_measurement();
this->duration = pulseIn(this->echoPin, HIGH); this->duration = pulseIn(this->echo_pin, HIGH);
return this->duration; return this->duration;
} }

View File

@ -32,17 +32,22 @@ class LCDScreen {
} }
void display_temperature(float temp){ void display_temperature(float temp){
this->state = DISPLAY_TEMPERATURE; if (this->state != DISPLAY_TEMPERATURE){
this->clear();
this->state = DISPLAY_TEMPERATURE;
}
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
this->clear(); lcd.print("Temp: ");
lcd.print(temp); lcd.print(temp,1);
lcd.print(" C"); lcd.print(" C ");
} }
void display_default_message(){ 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); lcd.setCursor(0, 0);
this->clear();
lcd.print(this->default_message); lcd.print(this->default_message);
} }

View File

@ -9,23 +9,24 @@
#define ULTRASONIC_SENSOR_PIN_TRIG 11 #define ULTRASONIC_SENSOR_PIN_TRIG 11
#define ULTRASONIC_SENSOR_PIN_ECHO 12 #define ULTRASONIC_SENSOR_PIN_ECHO 12
#define SERVO_PIN 10 #define SERVO_PIN 10
#define SERVO_PIN 10
#define TEMP_SENSOR_PIN 9 #define TEMP_SENSOR_PIN 9
OneWire oneWire(TEMP_SENSOR_PIN); 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); 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(); LCDScreen screen = LCDScreen();
unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured; unsigned long current_time, sensor_last_time_activated, temperature_last_time_measured;
float temperature_in_c = 0;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
dist_sensor.configure_pins(); dist_sensor.configure_pins();
barrier.configure_pins(); barrier.configure_pins();
sensors.begin(); temperature_sensors.begin();
screen.init(); screen.init();
} }
@ -47,11 +48,9 @@ void loop() {
if (current_time - temperature_last_time_measured >= 1000){ if (current_time - temperature_last_time_measured >= 1000){
temperature_last_time_measured = current_time; temperature_last_time_measured = current_time;
sensors.requestTemperatures(); temperature_sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0); temperature_in_c = temperature_sensors.getTempCByIndex(0);
screen.display_temperature(tempC);
} }
screen.display_temperature(temperature_in_c);
} }