#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address, dimensions
Servo servo;
const int buttonPowerPin = 4;
const int buttonRatePin = 5;
const int buttonStartStopPin = 6;
int flowRate = 10; // Default flow rate: 10 ml/hour
bool isSystemOn = false;
bool isPumpRunning = false;
unsigned long startTime = 0;
unsigned long countdownTime = 0;
void setup() {
pinMode(buttonPowerPin, INPUT_PULLUP);
pinMode(buttonRatePin, INPUT_PULLUP);
pinMode(buttonStartStopPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Syringe Pumpzzz");
servo.attach(3); // Servo connected to pin 3
servo.write(0); // Servo initialized at position 0 degrees
}
void loop() {
// Power button
if (digitalRead(buttonPowerPin) == LOW) {
isSystemOn = !isSystemOn;
if (isSystemOn) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System ON");
delay(1000);
lcd.clear();
displayFlowRate();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System OFF");
servo.write(0); // Reset servo to 0 degrees
isPumpRunning = false; // Stop pump when system is turned off
}
delay(500); // Debouncing
}
// Flow rate button
if (isSystemOn && digitalRead(buttonRatePin) == LOW) {
changeFlowRate();
displayFlowRate();
delay(500); // Debouncing
}
// Start/stop button
if (isSystemOn && digitalRead(buttonStartStopPin) == LOW) {
if (!isPumpRunning) {
startPump();
} else {
stopPump();
}
delay(500); // Debouncing
}
// Timer countdown
if (isPumpRunning) {
unsigned long elapsedTime = millis() - startTime;
if (elapsedTime < countdownTime) {
unsigned long remainingTime = (countdownTime - elapsedTime) / 1000;
lcd.setCursor(0, 1);
lcd.print("Timer: ");
lcd.print(remainingTime);
lcd.print(" s ");
} else {
stopPump();
}
}
}
void startPump() {
isPumpRunning = true;
lcd.setCursor(0, 1);
lcd.print("Start Pump ");
startTime = millis();
// Calculate servo end position based on flow rate
int servoEndPosition;
unsigned long pumpTime;
switch(flowRate) {
case 10:
servoEndPosition = 20;
pumpTime = 5000; // 5 seconds
break;
case 20:
servoEndPosition = 70;
pumpTime = 10000; // 10 seconds
break;
case 30:
servoEndPosition = 120;
pumpTime = 15000; // 15 seconds
break;
case 40:
servoEndPosition = 170;
pumpTime = 20000; // 20 seconds
break;
case 50:
servoEndPosition = 180;
pumpTime = 25000; // 25 seconds
break;
default:
servoEndPosition = 0;
pumpTime = 0;
}
countdownTime = pumpTime;
servo.writeMicroseconds(map(servoEndPosition, 0, 180, 1000, 2000)); // Move servo to the end position with adjustable speed
}
void stopPump() {
isPumpRunning = false;
lcd.setCursor(0, 1);
lcd.print("Stop Pump ");
servo.write(0); // Reset servo to 0 degrees
}
void displayFlowRate() {
lcd.setCursor(0, 1);
lcd.print("Rate: ");
lcd.print(flowRate);
lcd.print(" ml/hour");
}
void changeFlowRate() {
flowRate += 10;
if (flowRate > 50) {
flowRate = 10;
}
}