#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
#define LENGTH 3
char dose[LENGTH];
int data_count = 0;
char customKey;
int num = 0;
const int stepPin = 12;
const int dirPin = 13;
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.setCursor(0, 0);
lcd.print("IU-VNU-HCMC");
lcd.setCursor(0, 1);
lcd.print("School of BME");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MEDICALDESIGN2B");
lcd.setCursor(0, 1);
lcd.print("Group 5");
delay(2000);
lcd.clear();
printStartingWithDots();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dose of (mL)");
lcd.setCursor(0, 1);
}
void loop() {
customKey = customKeypad.getKey();
if (customKey) {
dose[data_count] = customKey;
lcd.print(dose[data_count]);
Serial.println(dose[data_count]);
data_count++;
}
if (data_count == LENGTH - 1) {
data_count = 0;
delay(1000);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Dose is ");
lcd.setCursor(9, 1);
num = (dose[1] - '0') + ((dose[0] - '0') * 10);
lcd.print(num);
lcd.setCursor(13, 1);
lcd.print("mL");
delay(4000);
// Move the stepper motor to dispense the dose
digitalWrite(dirPin, LOW);
for (int x = 0; x < num * 100; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(5000);
// Move the stepper motor back to the initial position
digitalWrite(dirPin, HIGH);
for (int x = 0; x < num * 100; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
// Print "Process finished" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Process finished");
delay(2000);
lcd.clear();
// Reinitialize the LCD display for the next input
printStartingWithDots();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dose of (mL)");
lcd.setCursor(0, 1);
}
}
void printStartingWithDots() {
lcd.setCursor(0, 0);
lcd.print("Starting");
int dotCount = 8; // "Starting" has 8 characters, so we start from the 9th character
while (dotCount < 32) { // 16 columns * 2 rows = 32 characters total
lcd.print(".");
delay(100); // Adjusted to 100 milliseconds for a faster effect
dotCount++;
if (dotCount == 16) {
lcd.setCursor(0, 1); // Move to the beginning of the second line
}
}
}