#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <AccelStepper.h>
#define STOP_BUTTON_PIN 12 // Pin untuk tombol stop
#define MOTOR_STEP_PIN 3 // Pin STEP motor stepper
#define MOTOR_DIR_PIN 2 // Pin DIRECTION motor stepper
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
// Define keypad
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] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define variables
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution for your motor
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
AccelStepper stepper(AccelStepper::FULL4WIRE, MOTOR_STEP_PIN, MOTOR_DIR_PIN);
int drugCount = 0; // Jumlah obat yang akan diinjeksikan
int injectedVolume = 0; // Volume obat yang sudah diinjeksikan
void setup() {
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP); // Set pin tombol stop sebagai input dengan pull-up resistor
stepper.setMaxSpeed(1000.0); // Set kecepatan maksimum motor stepper (langkah/detik)
stepper.setAcceleration(500.0); // Set percepatan motor stepper (langkah/detik^2)
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Nyalakan backlight LCD
lcd.setCursor(0, 0);
lcd.print("Enter drug count:");
}
void loop() {
char key = keypad.getKey(); // Baca tombol yang ditekan
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
drugCount = drugCount * 10 + (key - '0'); // Tambahkan digit ke jumlah obat
lcd.setCursor(0, 1);
lcd.print("Drug count: "); // Bersihkan baris ke-2
lcd.setCursor(12, 1);
lcd.print(drugCount);
} else if (key == '#') {
lcd.setCursor(0, 2);
lcd.print("Injecting..."); // Tampilkan pesan saat proses penyuntikan
for (int i = 0; i < drugCount; i++) {
injectDrug(); // Lakukan penyuntikan sebanyak jumlah obat yang dimasukkan
}
lcd.setCursor(0, 2);
lcd.print("Injection done. ");
lcd.setCursor(0, 3);
lcd.print("Injected: ");
lcd.print(injectedVolume);
lcd.print(" ml");
drugCount = 0; // Reset jumlah obat
}
}
if (digitalRead(STOP_BUTTON_PIN) == LOW) {
stopInjection(); // Memanggil fungsi untuk menghentikan penyuntikan jika tombol stop ditekan
}
}
void injectDrug() {
// Hitung jumlah langkah berdasarkan volume yang akan diinjeksikan
long targetSteps = map(10, 0, 100, 0, stepsPerRevolution * 5); // Misalnya, 10 ml akan diinjeksikan (disesuaikan sesuai dengan kebutuhan)
stepper.moveTo(targetSteps); // Atur posisi motor stepper
while (stepper.distanceToGo() != 0) {
stepper.run(); // Jalankan motor stepper
}
injectedVolume += 10; // Tambahkan volume yang sudah diinjeksikan
delay(500); // Tunggu sebentar sebelum mengambil obat selanjutnya
}
void stopInjection() {
stepper.stop(); // Hentikan motor stepper
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Injection stopped");
delay(1000); // Tunggu sebentar sebelum melanjutkan
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter drug count:");
}