Fix major bugs caused by minor code oversights
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 10 Dec 2024 15:15:10 +0000 (16:15 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 10 Dec 2024 15:15:10 +0000 (16:15 +0100)
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

index d4347a4dcd6b160c7077d9a8dc5e146bec6a4f54..164e9303b67fbd91e656c4bbd72d221ed502ea73 100644 (file)
@@ -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;
        }
 }