#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//#include <TimerOne.h>
// Define the connections between the keypad and Arduino
const byte ROWS = 4; // Number of rows on the keypad
const byte COLS = 4; // Number of columns on the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int motorPin = 10; // PWM pin connected to the motor driver input
const int potPin = A0; // Analog input pin connected to the potentiometer
const int limitPotPin = A1; // Analog input pin connected to the limit potentiometer
const int motorEnablePin = 5; // Digital pin connected to the ENA pin of the motor driver
const int motorStopThreshold = 200; // Threshold value to stop the motor (adjust as needed)
double volume = 0;
const int buzzerPin = 11; // Digital pin connected to the buzzer
// LCD module settings
//const int lcdColumns = 16; // Number of LCD columns
//const int lcdRows = 2; // Number of LCD rows
//const int lcdAddr = 0x27; // I2C address of the LCD module
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(lcdAddr, lcdColumns, lcdRows);
String flowRate = ""; // Variable to store the entered flow rate
float syringeValue = 5.00; // syringe value 100 ML
void setup() {
Serial.begin(9600);
// Initialize LCD module
lcd.init();
// lcd.begin(16,2);
lcd.setBacklight(HIGH); // Turn on the backlight
// Create comma character
pinMode(motorPin, OUTPUT);
pinMode(motorEnablePin, OUTPUT);
pinMode(potPin, INPUT);
pinMode(buzzerPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Welcome");
lcd.setCursor(0, 1);
delay(2000);
}
void loop() {
//lcd.clear();
char key = keypad.getKey();
lcd.setCursor(0, 0);
lcd.print("Syringe Val:");
lcd.print(syringeValue);
lcd.setCursor(0, 1);
lcd.print("B kybrd | A Pot");
// Read the keypad input
if (key != NO_KEY) {
flowType(key);
}
}
void program (double myFlow) {
double potValue = 1023.00;
Serial.println(potValue);
// Read potentiometer value (0-1023)
double motorSpeed = map(potValue, 0.00, 1023.00, 0.00, 255.00); // Map potentiometer value to motor speed (0-255)
// Read limit potentiometer value (0-1023)
volume += ((myFlow * 1023.0) / 5.0);
Serial.println(volume);
delay(100);
while (true) {
analogWrite(motorPin, motorSpeed);
double limitPotValue = analogRead(limitPotPin);
Serial.println(limitPotValue);
//delay(100);
if (limitPotValue >= volume ) {
digitalWrite(motorPin, LOW);
//volume += volume;
break;
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
delay(500); // Delay for the sound duration (adjust as needed)
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
}
}
void flowType(char key) {
if (key == 'A') {
lcd.clear();
lcd.print("Pot Value :");
bool a_state = true;
while (a_state) {
char key = keypad.getKey();
while (a_state) {
char key = keypad.getKey();
flowRate = (analogRead(potPin) / 1023.00) * 5.00;
double myFlow = flowRate.toDouble();
lcd.setCursor(0, 1);
lcd.print(myFlow);
if (key == '#') {
flowRate = "";
while (true) {
//Serial.println(myFlow);
syringeValue -= myFlow;
program (myFlow);
myFlow = 0.0;
a_state = false;
break;
}
}
}
}
}
if (key == 'B') {
lcd.clear();
lcd.print("KeyPad Value :");
bool b_state = true;
while (b_state) {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == 'D') {
// Delete the last entered digit
if (flowRate.length() > 0) {
flowRate = flowRate.substring(0, flowRate.length() - 1);
lcd.setCursor(0, 1);
double myFlow = flowRate.toDouble();
lcd.print(myFlow);
}
} else {
flowRate += key;
lcd.setCursor(0, 1);
double myFlow = flowRate.toDouble();
lcd.print(myFlow);
if (key == '#') {
flowRate = "";
while (true) {
//Serial.println(myFlow);
syringeValue -= myFlow;
program (myFlow);
myFlow = 0.0;
b_state = false;
break;
}
}
}
}
}
}
}