I'm trying to make a liquid dispenser that allows the user to input the amount they would like it to dispense. I'm using a 12V 1A diaphragm pump and a 12V external power supply. For some reason, while the pump is programmed to turn on to dispense a certain amount of liquid, it suddenly stops even before the set time for the pump to open and resets the entire thing. Where did I go wrong?
This is the schematic diagram for it.
This is the code I used for it.
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int TIP120pin = 2;
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7};
byte colPins[COLS] = {6, 5, 4, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char customKey;
String amtStr = "";
byte column = 0;
int amt;
void setup()
{
Serial.begin(9600);
pinMode(TIP120pin, OUTPUT);
analogWrite(TIP120pin, 0);
lcd.init();
lcd.clear();
lcd.backlight();
//print instructions
lcd.home();
lcd.print("How much liquid");
lcd.setCursor(0,1);
lcd.print("do you want?");
delay(3000);
lcd.clear();
lcd.home();
lcd.print("min:200 max:1000");
lcd.setCursor(0,1);
lcd.print("Press D to enter");
delay(5000);
lcd.clear();
}
void loop()
{
//print guiding text
lcd.home();
lcd.print("amount (in mL):");
lcd.setCursor(0,1);
customKey = customKeypad.getKey();
//user enters amount
if (customKey) {
if(customKey == 'D'){
Serial.println("user has entered amount");
Serial.println("amount contained in amtStr is");
Serial.println(amtStr);
amt = amtStr.toInt();
Serial.println("calculated amount is");
Serial.println(amt);
openPump();
Serial.println("left openPump function");
clearData();
Serial.println("clearData function executed");
}
else {
amtStr += customKey;
Serial.println("amtStr currently contains");
Serial.println(amtStr);
lcd.setCursor(column, 1);
lcd.print(customKey);
column++;
}
}
}
void openPump (){
Serial.println("openPump function was called");
float flowRateRaw = 1.63; //in L/min
float flowRate = (flowRateRaw * 1000) / 60; //convert to mL/sec
Serial.println("calculated flow rate is");
Serial.println(flowRate);
float time = amt / flowRate;
int time_int = time;
float time_dec = time - time_int;
Serial.println("calculated time to open is");
Serial.println(time);
Serial.println("calculated time_int is");
Serial.println(time_int);
Serial.println("calculated time_dec is");
Serial.println(time_dec);
analogWrite(TIP120pin, 255);
Serial.println("pump turned on");
Serial.println("timer...");
for (float i = 1; i <= time_int; i++){
delay(1000);
Serial.println(i);
}
delay(time_dec*1000);
Serial.println(time);
//delay(time * 1000);
analogWrite(TIP120pin, 0);
Serial.println("pump turned off");
}
void clearData (){
amtStr = "";
amt = 0;
column = 0;
lcd.clear();
}


