#include <Button.h>
#include "LiquidCrystal_I2C.h"
#include <Keypad.h>
#define dirPin 2
#define stepPin 3
#define ledPin 13
const int stepDelay = 20000;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROW_NUM = 4; // jumlah baris keypad
const byte COLUMN_NUM = 4; // jumlah kolom keypad
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {12, 11, 10, 9}; // hubungkan baris keypad ke pin digital ini
byte pin_column[COLUMN_NUM] = {8, 7, 6, 5}; // hubungkan kolom keypad ke pin digital ini
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
String inputString = "";
int currentPosition = -2; // Inisialisasi posisi awal
int A = 50;
int B = 100;
int C = 150;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(4, 0); lcd.print("Tugas Akhir");
lcd.setCursor(3, 1); lcd.print("Kel 4 Kelas A");
lcd.setCursor(2, 2); lcd.print("Stechoq Robotika");
delay(2000);
// Pindahkan stepper ke posisi awal (0)
moveStepper(-currentPosition);
//currentPosition = 0; // Reset posisi saat ini ke 0
lcd.clear();
}
void moveStepper(int steps) {
currentPosition += steps; // Perbarui posisi saat ini
digitalWrite(dirPin, HIGH); // Arahkan motor stepper searah jarum jam
for (int i = 0; i < steps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
// Beri sinyal visual dengan LED
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
void handleButton(char button) {
lcd.clear();
//lcd.setCursor(0, 0); lcd.print("Input: " + String(button));
if (button == 'A') {
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml): 50");
moveStepper(50);
} else if (button == 'B') {
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml):100");
moveStepper(100);
} else if (button == 'C') {
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml):150");
moveStepper(150);
} else if (button == 'D') {
// Hapus input dan kembalikan stepper ke posisi awal (0)
inputString = "";
lcd.clear();
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml):");
} else if (button == '*') {
// Periksa apakah currentPosition sudah 0 sebelum mengubah arah stepper
if (currentPosition > 0) {
digitalWrite(dirPin, LOW);
for (int i = 0; i < currentPosition; i++) { // Perbaikan pada perulangan
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
lcd.clear();
lcd.setCursor(7, 1);
lcd.print("Reset");
}
delay(1000);
lcd.clear();
currentPosition = 0; // Setelah reset, posisi saat ini menjadi 0
}
}
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Jika tombol '#' ditekan, proses input
int steps =inputString.toInt();
moveStepper(steps);
// Reset inputString setelah diproses
inputString = "";
lcd.clear();
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml): ");
} else if (key >= '0' && key <= '9') {
// Jika tombol 0-9 ditekan pada keypad, tambahkan ke inputString
inputString += key;
lcd.clear();
lcd.setCursor(5, 0); lcd.print("Kelompok 4");
lcd.setCursor(2, 1); lcd.print("Stechoq Robotika");
lcd.setCursor(0, 2); lcd.print("Cairan Obat (ml): ");
lcd.setCursor(17,2); lcd.print(inputString);
} else {
// Handle tombol A, B, C, D, dan *
handleButton(key);
}
}
}