Skip to content

Commit c28353c

Browse files
authored
feat: trying clean code methods in code and updating readme
1 parent 5f5837c commit c28353c

File tree

2 files changed

+168
-102
lines changed

2 files changed

+168
-102
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
The **FlowCycle Pump** is an Arduino-based controller designed to automate precise cyclic timing intervals for laboratory pump operations. It was developed as part of a scientific study at the Center for Health Sciences (CCS) at the Federal University of Pernambuco (UFPE), Brazil.
1010

1111
### Technical vision
12-
This project implements a 30-minute cyclic timer that switches a pump on and off at predefined intervals, providing visual feedback on an LCD and LEDs. The cycle only startsor restartswhen the user presses the button, allowing flexible pausing between runs.
12+
This project implements a 30-minute cyclic timer that switches a pump on and off at predefined intervals, providing visual feedback on an LCD and LEDs. The cycle only startsor restartswhen the user presses the button, allowing flexible pausing during and between runs.
1313

1414
### Scientific Context
1515

@@ -49,9 +49,10 @@ Explore and simulate the circuit directly on Tinkercad:
4949
## Features
5050

5151
- **Automated Timing:** Precise pump cycling intervals (default: 2 seconds ON / 30 seconds OFF for 30 minutes).
52+
- **Flexible Pausing:** Pause cycle during and between runs, which also will inform at what time did you pause it.
5253
- **One-button Operation:** Simple user interface for starting and restarting cycles.
5354
- **LCD Feedback:** Real-time status updates and countdown timers (MM:SS format).
54-
- **LED Indicators:** Visual status indication (Green LED: Pump ON, Red LED: Pump OFF, Yellow LED: End of 30 minutes cycle).
55+
- **LED Indicators:** Visual status indication (Green LED: Pump ON, Red LED: Pump OFF, Yellow LED: End of 30 minutes cycle and Pause state).
5556
- **Isolated Power Supply:** Dedicated 6×AA battery pack for the pump ensures reliable operation without interference.
5657

5758
## Repository Structure

src/FlowCyclePump.ino

Lines changed: 165 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// === Declaring variables ===
55
// PINS
6-
const int motorPin = 13;
6+
const int pump = 13;
77
const int yellowLed = 6;
88
const int greenLed = 4;
99
const int redLed = 3;
@@ -12,7 +12,8 @@ const int button = 2;
1212
// TIMER
1313
int seconds = 0;
1414
int minutes = 0;
15-
int count = 0;
15+
int totalTime = 0;
16+
bool paused = false;
1617
// Length of each period in seconds
1718
const int onTime = 2; // 2seg
1819
const int breakTime = 30; // 30seg
@@ -21,136 +22,200 @@ const int replayPeriod = 1800; // 30min
2122
// DISPLAY
2223
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
2324

24-
25-
void setup()
26-
{
25+
void setup() {
2726
// Set up the number of columns and rows on the LCD.
28-
lcd.begin(16, 2);
27+
lcd.begin(16, 2);
2928

30-
pinMode(motorPin, OUTPUT);
29+
// Pins
30+
pinMode(pump, OUTPUT);
3131
pinMode(yellowLed, OUTPUT);
3232
pinMode(greenLed, OUTPUT);
3333
pinMode(redLed, OUTPUT);
34-
pinMode(button, INPUT_PULLUP);
35-
34+
pinMode(button, INPUT);
35+
3636
// Display initial message to user
37-
lcd.setCursor(1,0);
37+
lcd.setCursor(1, 0);
3838
lcd.print("Aperte o botao");
39-
lcd.setCursor(2,1);
39+
lcd.setCursor(2, 1);
4040
lcd.print("para iniciar!");
41-
41+
4242
// Wait for button press to start
43-
while(digitalRead(button) == LOW){
43+
while (digitalRead(button) == LOW) {
4444
// do nothing before click the button
4545
}
4646
}
4747

48-
void loop()
49-
{
50-
runPumpCycle();
51-
48+
void loop() {
49+
runPumpCycle();
50+
5251
// ===== 30MIN CYCLE ENDED =====
53-
printEndMessage();
54-
55-
// === Wait 1 minute before ask for restart process ===
56-
delay(60000);
57-
askForRestart();
52+
startEndPhase();
53+
54+
// Wait 1 minute before ask for restart process
55+
delay(60000);
56+
askForRestart();
5857
}
5958

60-
void printEndMessage()
61-
{
62-
lcd.setCursor(0, 0);
63-
lcd.print(" Ciclo de 30min ");
64-
lcd.setCursor(0, 1);
65-
lcd.print(" Finalizado! ");
66-
67-
digitalWrite(yellowLed, HIGH);
68-
digitalWrite(motorPin, LOW);
69-
digitalWrite(greenLed, LOW);
70-
digitalWrite(redLed, LOW);
59+
void startEndPhase() {
60+
lcd.setCursor(0, 0);
61+
lcd.print(" Ciclo de 30min ");
62+
lcd.setCursor(0, 1);
63+
lcd.print(" Finalizado! ");
64+
65+
digitalWrite(yellowLed, HIGH);
66+
digitalWrite(pump, LOW);
67+
digitalWrite(greenLed, LOW);
68+
digitalWrite(redLed, LOW);
7169
}
72-
73-
void askForRestart()
74-
{
75-
lcd.clear();
76-
lcd.setCursor(1, 0);
77-
lcd.print("Aperte o botao");
78-
lcd.setCursor(1, 1);
79-
lcd.print("para reiniciar");
80-
81-
// === Wait click in button to restart ===
82-
while (digitalRead(button) == LOW) {
70+
71+
void askForRestart() {
72+
lcd.clear();
73+
lcd.setCursor(1, 0);
74+
lcd.print("Aperte o botao");
75+
lcd.setCursor(1, 1);
76+
lcd.print("para reiniciar");
77+
78+
// Wait click in button to restart
79+
while (digitalRead(button) == LOW) {
80+
// do nothing
81+
}
82+
delay(50); // debounce 50ms
83+
84+
while (digitalRead(button) == HIGH) {
8385
// do nothing
84-
}
85-
delay(50); // debounce 50ms
86-
87-
while (digitalRead(button) == HIGH) {
88-
// do nothing
89-
}
86+
}
9087
delay(50);
91-
count = 0;
88+
89+
totalTime = 0;
9290
digitalWrite(yellowLed, LOW);
9391
}
9492

95-
void runPumpCycle()
96-
{
97-
while (count < replayPeriod) {
93+
void runPumpCycle() {
94+
while (totalTime < replayPeriod) {
9895
// ===== PUMP ON =====
99-
lcd.clear();
100-
lcd.setCursor(0, 0);
101-
lcd.print("Bomba LIGADA!");
102-
lcd.setCursor(0, 1);
103-
lcd.print("Desliga em");
104-
105-
digitalWrite(motorPin, HIGH);
106-
digitalWrite(greenLed, HIGH);
107-
digitalWrite(redLed, LOW);
96+
startPumpOnPhase();
10897
controlDisplayTimer(onTime);
10998

11099
// ===== PUMP OFF =====
111-
lcd.clear();
112-
lcd.setCursor(0, 0);
113-
lcd.print("Bomba DESLIGADA!");
114-
lcd.setCursor(0, 1);
115-
lcd.print("Liga em");
116-
117-
118-
digitalWrite(motorPin, LOW);
119-
digitalWrite(greenLed, LOW);
120-
digitalWrite(redLed, HIGH);
100+
startPumpOffPhase();
121101
controlDisplayTimer(breakTime);
122102
}
123103
}
124104

125-
void controlDisplayTimer(int cycleTime)
126-
{
105+
void controlDisplayTimer(int cycleTime) {
127106
seconds = cycleTime;
128107

108+
digitalWrite(button, LOW);
109+
paused = false;
110+
129111
while (seconds >= 0) {
130-
// Calculate minutes and seconds
131-
int displayMinutes = seconds / 60;
132-
int displaySeconds = seconds % 60;
133-
134-
// Shows in MM:SS format
135-
if(cycleTime == 2){
136-
lcd.setCursor(11, 1);
137-
} else {
138-
lcd.setCursor(8, 1);
139-
}
140-
141-
if (displayMinutes < 10) {
142-
lcd.print("0");
143-
}
144-
lcd.print(displayMinutes);
145-
lcd.print(":");
146-
if (displaySeconds < 10) {
147-
lcd.print("0");
148-
}
149-
lcd.print(displaySeconds);
150-
151-
// Wait 1 second to decrement seconds
152-
delay(1000);
112+
if (cycleTime == onTime) {
113+
lcd.setCursor(11, 1);
114+
} else {
115+
lcd.setCursor(8, 1);
116+
}
117+
118+
if (!paused) {
119+
formatTime(seconds); // Format MM:SS
120+
}
121+
122+
// Break 1seg into ten 100 ms chunks
123+
// To still detect a button press
124+
for (int i = 0; i < 10; i++) {
125+
delay(100);
126+
handlePause(cycleTime);
127+
}
128+
129+
// Only decrement when not paused
130+
if (!paused) {
153131
seconds--;
154-
count++;
155-
}
132+
totalTime++;
133+
}
134+
}
135+
}
136+
137+
void handlePause(int cycleTime) {
138+
bool prevState = paused;
139+
140+
if (digitalRead(button) == HIGH) {
141+
delay(50);
142+
while (digitalRead(button) == HIGH) {}
143+
delay(50);
144+
paused = !paused;
145+
}
146+
147+
bool continueProcess = prevState != paused && !paused;
148+
149+
if (continueProcess) {
150+
continueBeforePhase(cycleTime);
151+
}
152+
153+
if (paused) {
154+
startPausedPhase();
155+
}
156+
}
157+
158+
void startPausedPhase() {
159+
lcd.setCursor(0, 0);
160+
lcd.print(" PAUSADO no ");
161+
lcd.setCursor(0, 1);
162+
lcd.print(" tempo ");
163+
lcd.setCursor(8, 1);
164+
formatTime(totalTime);
165+
lcd.setCursor(13, 1);
166+
lcd.print(" ");
167+
168+
digitalWrite(yellowLed, HIGH);
169+
digitalWrite(pump, LOW);
170+
digitalWrite(greenLed, LOW);
171+
digitalWrite(redLed, LOW);
172+
}
173+
174+
void continueBeforePhase(int cycleTime) {
175+
if (cycleTime == onTime) {
176+
startPumpOnPhase();
177+
} else {
178+
startPumpOffPhase();
179+
}
180+
}
181+
182+
void startPumpOnPhase() {
183+
lcd.clear();
184+
lcd.setCursor(0, 0);
185+
lcd.print("Bomba LIGADA!");
186+
lcd.setCursor(0, 1);
187+
lcd.print("Desliga em");
188+
189+
digitalWrite(yellowLed, LOW);
190+
digitalWrite(pump, HIGH);
191+
digitalWrite(greenLed, HIGH);
192+
digitalWrite(redLed, LOW);
193+
}
194+
195+
void startPumpOffPhase() {
196+
lcd.clear();
197+
lcd.setCursor(0, 0);
198+
lcd.print("Bomba DESLIGADA!");
199+
lcd.setCursor(0, 1);
200+
lcd.print("Liga em");
201+
202+
digitalWrite(yellowLed, LOW);
203+
digitalWrite(pump, LOW);
204+
digitalWrite(greenLed, LOW);
205+
digitalWrite(redLed, HIGH);
206+
}
207+
208+
void formatTime(int seconds) {
209+
int displayMinutes = seconds / 60;
210+
int displaySeconds = seconds % 60;
211+
212+
if (displayMinutes < 10) {
213+
lcd.print('0');
214+
}
215+
lcd.print(displayMinutes);
216+
lcd.print(':');
217+
if (displaySeconds < 10) {
218+
lcd.print('0');
219+
}
220+
lcd.print(displaySeconds);
156221
}

0 commit comments

Comments
 (0)