0% found this document useful (0 votes)
5 views

AutoBots_Code_LiquidDispenser_I2C-LCD_Uno-R3

Code for arduino liquid dispenser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

AutoBots_Code_LiquidDispenser_I2C-LCD_Uno-R3

Code for arduino liquid dispenser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

// I2C LCD address and dimensions


LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address to match your module

// 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);

// Flow sensor and relay pin configuration


const int flowSensorPin = 2; // Digital pin 2 for flow sensor signal
const int relayPin = 7; // Digital pin 7 for relay control

// Flow sensor variables


volatile int flowPulseCount = 0;
unsigned long oldTime = 0;
float calibrationFactor = 7; // Calibration factor for flow sensor

// 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

// Dispensing start time


unsigned long dispensingStartTime = 0;

void setup() {
Serial.begin(9600);

// Initialize I2C LCD


lcd.init();
lcd.backlight();
lcd.clear();
lcd.print(" Welcome");
lcd.setCursor(0, 1);
lcd.print(" 1Autobots");
delay(4000);
lcd.clear();

// Initialize keypad
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Ensure relay is initially off

// Initialize flow sensor pin


pinMode(flowSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
lcd.print("Enter Volume:");
}

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);
}
}

unsigned long currentTime = millis();


unsigned long elapsedTime = currentTime - oldTime;

if (elapsedTime > 1000) {


detachInterrupt(digitalPinToInterrupt(flowSensorPin));

float flowRate = ((1000.0 / elapsedTime) * flowPulseCount) / calibrationFactor;


totalMilliliters += (flowRate / 60.0) * 1000; // Convert to milliliters per
second

flowPulseCount = 0;
oldTime = currentTime;

attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);

if (dispensing) {
unsigned long dispensingTime = currentTime - dispensingStartTime;
float secondsDispensing = dispensingTime / 1000.0;
float minutesDispensing = secondsDispensing / 60.0;

percentageComplete = (totalMilliliters / desiredMilliliters) * 100;


lcd.clear();
lcd.print("Flow: ");
lcd.print(flowRate, 2);
lcd.print(" L/m");
lcd.setCursor(0, 1);
lcd.print(totalMilliliters / 1000, 2);
lcd.print(" L ");
lcd.print(percentageComplete, 1);
lcd.print("%");

if (totalMilliliters >= desiredMilliliters) {


turnOffRelay();
dispensing = false;
lcd.clear();
lcd.print("Flow: ");
lcd.print(flowRate, 2);
lcd.print(" L/m");
lcd.setCursor(0, 1);
lcd.print("Dispensed: ");
lcd.print(totalMilliliters / 1000, 2);
lcd.print(" L");
delay(4000);

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
}

void handleAutoMode(char key) {


if (!dispensing) {
if (key >= '0' && key <= '9') {
desiredMilliliters = desiredMilliliters * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(desiredMilliliters);
lcd.print(" ml");
} else if (key == '*') {
if (String(desiredMilliliters).indexOf('.') == -1) {
desiredMilliliters += 0.0;
lcd.setCursor(0, 1);
lcd.print(desiredMilliliters);
lcd.print(" ml");
}
} else if (key == 'C') {
desiredMilliliters = 0.0;
lcd.clear();
lcd.print("Enter Volume:");
} else if (key == 'A') {
if (desiredMilliliters > 0 && desiredMilliliters <= 100000) {
lcd.clear();
lcd.print("Place");
lcd.setCursor(0, 1);
lcd.print("Container");
delay(3000);
triggerRelay();
resetFlowSensor(); // Reset flow sensor count when starting dispensing
dispensingStartTime = millis(); // Record dispensing start time
delay(1500); // 1.5 second delay before flow sensor starts counting
dispensing = true;
totalMilliliters = 0.0; // Reset totalMilliliters before starting new
dispensing
percentageComplete = 0.0; // Reset percentage completion
lcd.clear();
lcd.print("Dispensing...");
} else {
lcd.clear();
lcd.print("Over Flow");
delay(2000);
lcd.clear();
lcd.print("Enter Volume:");
}
}
} else if (key == 'B') {
turnOffRelay();
dispensing = false;
totalMilliliters = 0.0; // Reset totalMilliliters
percentageComplete = 0.0; // Reset percentage completion
lcd.clear();
lcd.print("Canceled");
delay(2000);
lcd.clear();
lcd.print("Enter Volume:");
desiredMilliliters = 0.0;
}
}

void handleManualMode(char key) {


if (key == 'A') {
lcd.clear();
lcd.print("Place");
lcd.setCursor(0, 1);
lcd.print("Container");
delay(2000);
triggerRelay();
resetFlowSensor(); // Reset flow sensor count when starting dispensing
dispensingStartTime = millis(); // Record dispensing start time
delay(1500); // 1.5 second delay before flow sensor starts counting
dispensing = true;
totalMilliliters = 0.0; // Reset totalMilliliters before starting new
dispensing
percentageComplete = 0.0; // Reset percentage completion
lcd.clear();
lcd.print("Dispensing...");
} else if (key == 'B') {
turnOffRelay();
dispensing = false;
totalMilliliters = 0.0; // Reset totalMilliliters
percentageComplete = 0.0; // Reset percentage completion
lcd.clear();
lcd.print("Stopped");
delay(2000);
lcd.clear();
lcd.print("Manual Mode");
}
}

You might also like