From fb8efa30eadb5a01d5972fc9190bcddc9588cebd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 10 Dec 2024 16:15:10 +0100 Subject: [PATCH] Fix major bugs caused by minor code oversights The if statement did not use curly braces, so the following block was executed unconditionally. The three-way valve had its values mixed up, which is corrected now. The sleep is now encapsulated in a while loop to prevent any interrupt from waking the processor up (other than the one caused by flipping the switch). --- Servomatic.ino | 81 ++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/Servomatic.ino b/Servomatic.ino index d4347a4..164e930 100644 --- a/Servomatic.ino +++ b/Servomatic.ino @@ -46,7 +46,7 @@ void main_loop() Measurement measurement{sensor_1}; Valve vacuum_valve{VACUUM_VALVE_PIN, 2295, 1494, 802}; Valve cell_valve{CELL_VALVE_PIN, 2000, 1535, 1276}; - Valve gas_threeway_valve{GAS_VALVE_PIN, 1301, 1641, 2036}; + Valve gas_threeway_valve{GAS_VALVE_PIN, 1301, 2036, 1641}; vacuum_valve.close(); cell_valve.close(); @@ -54,45 +54,47 @@ void main_loop() while (1) { if (!interrupt_happened && digitalRead(SWITCH_PIN) == HIGH) - // aparature initialization - valve positioning, evacuation - vacuum_valve.open(); - gas_threeway_valve.open(); - cell_valve.open_barely(); - delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); - cell_valve.open(); - cell_valve.close_open_clean(); - vacuum_valve.close_open_clean(); - interruptable_delay(EVACUATION_LONG_MS); - vacuum_valve.close(); - cell_valve.close(); - while (!interrupt_happened && digitalRead(SWITCH_PIN) == HIGH) { - while (!interrupt_happened - && sensor_1.get_pressure() < MEASUREMENT_UPPER_LIMIT) - { - // absorption cycle - threeway_fill(gas_threeway_valve, THREEWAY_TURNS_TO_FILL); - cell_valve.open_barely(); - delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); - cell_valve.open(); - measurement.wait_for_equilibrium(0.05); - measurement.clear(); - cell_valve.close(); - } - while (!interrupt_happened - && sensor_1.get_pressure() > MEASUREMENT_LOWER_LIMIT) + // aparature initialization - valve positioning, evacuation + vacuum_valve.open(); + gas_threeway_valve.open(); + cell_valve.open_barely(); + delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); + cell_valve.open(); + cell_valve.close_open_clean(); + vacuum_valve.close_open_clean(); + interruptable_delay(EVACUATION_LONG_MS); + vacuum_valve.close(); + cell_valve.close(); + while (!interrupt_happened && digitalRead(SWITCH_PIN) == HIGH) { - // desorption cycle - vacuum_valve.open(); - vacuum_valve.close_open_clean(); - interruptable_delay(EVACUATION_MS); - vacuum_valve.close(); - cell_valve.open_barely(); - delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); - cell_valve.open(); - measurement.wait_for_equilibrium(0.05); - measurement.clear(); - cell_valve.close(); + while (!interrupt_happened + && sensor_1.get_pressure() < MEASUREMENT_UPPER_LIMIT) + { + // absorption cycle + threeway_fill(gas_threeway_valve, THREEWAY_TURNS_TO_FILL); + cell_valve.open_barely(); + delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); + cell_valve.open(); + measurement.wait_for_equilibrium(0.05); + measurement.clear(); + cell_valve.close(); + } + while (!interrupt_happened + && sensor_1.get_pressure() > MEASUREMENT_LOWER_LIMIT) + { + // desorption cycle + vacuum_valve.open(); + vacuum_valve.close_open_clean(); + interruptable_delay(EVACUATION_MS); + vacuum_valve.close(); + cell_valve.open_barely(); + delay(CELL_BARELY_OPEN_EQUILIBRATION_MS); + cell_valve.open(); + measurement.wait_for_equilibrium(0.05); + measurement.clear(); + cell_valve.close(); + } } } vacuum_valve.open(); @@ -103,7 +105,8 @@ void main_loop() delay(10000); vacuum_valve.close(); signal_going_to_sleep(); - sleep(); + while (digitalRead(SWITCH_PIN) == LOW) + sleep(); interrupt_happened = 0; } } -- 2.30.2