AutoBots_Code_LiquidDispenser_I2C-LCD_Uno-R3
AutoBots_Code_LiquidDispenser_I2C-LCD_Uno-R3
h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Keypad configuration
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'.','0','#','D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; // Rows 1 to 4
byte colPins[COLS] = {9, 10, 11, 12}; // Columns 9 to 12
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Dispensing variables
float desiredMilliliters = 0.0; // Desired amount of water in milliliters
float totalMilliliters = 0.0; // Total amount of water dispensed
bool dispensing = false; // Flag to check if dispensing is ongoing
float percentageComplete = 0.0; // Percentage of completion
bool manualMode = false; // Flag for manual dispensing mode
void setup() {
Serial.begin(9600);
// Initialize keypad
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Ensure relay is initially off
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
manualMode = !manualMode;
lcd.clear();
lcd.print(manualMode ? "Manual Mode" : "Auto Mode");
delay(2000);
lcd.clear();
lcd.print("Enter Volume:");
lcd.setCursor(0, 1);
desiredMilliliters = 0.0;
} else if (manualMode) {
handleManualMode(key);
} else {
handleAutoMode(key);
}
}
flowPulseCount = 0;
oldTime = currentTime;
if (dispensing) {
unsigned long dispensingTime = currentTime - dispensingStartTime;
float secondsDispensing = dispensingTime / 1000.0;
float minutesDispensing = secondsDispensing / 60.0;
lcd.clear();
lcd.print("Time: ");
lcd.print(minutesDispensing, 2);
lcd.print(" min");
delay(2000);
lcd.clear();
lcd.print("Enter Volume:");
desiredMilliliters = 0.0;
totalMilliliters = 0.0; // Reset totalMilliliters
percentageComplete = 0.0; // Reset percentage completion
}
}
}
}
void pulseCounter() {
flowPulseCount++;
}
void triggerRelay() {
digitalWrite(relayPin, LOW); // Turn on relay
}
void turnOffRelay() {
digitalWrite(relayPin, HIGH); // Turn off relay
}
void resetFlowSensor() {
flowPulseCount = 0; // Reset flow sensor pulse count
}