#include <Keypad_I2C.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Define I2C addresses
#define KEYPAD_I2C_ADDR 0x20 //alamat i2c keypad
#define LCD_I2C_ADDR 0x27 // alamat i2c lcd 20x4
//pin kipas heater
const int fan = 18;
const int heater = 19;
const double lelehFilament = 150;
//pin input ntc
const int ntcA = 34;
const int ntcB = 35;
//pin pin motor
const int motorPul = 2;
const int motorDir = 4;
const int motorENA = 5;
int pulseCount = 0; // Jumlah pulsa yang akan dihasilkan
bool isRunning = false; // Status apakah pulsa sedang dihasilkan
// Inisialisasi keypad
const byte ROWS = 4; //jumlah baris pada keypad
const byte COLS = 4; //jumlah kolom pada keypad
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// byte rowPins[ROWS] = { 4, 5, 6, 7 };
// byte colPins[COLS] = { 0, 1, 2, 3 };
// // Initialize keypad and LCD
// Keypad_I2C keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS, KEYPAD_I2C_ADDR);
// LiquidCrystal_I2C lcd(LCD_I2C_ADDR, 20, 4);
byte rowPins[ROWS] = { 13, 12, 14, 27 }; //sesuaikan pinnya sesuai dengan koneksi Anda
byte colPins[COLS] = { 26, 25, 33, 32 }; //sesuaikan pinnya sesuai dengan koneksi Anda
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Alamat I2C default untuk LCD 20x4 adalah 0x27
int feedrate = 0;
int setpointSuhu = 0;
String input = "";
int adc1 = 0;
int adc2 = 0;
double suhuAvg = 0.0;
float suhuA = 0.0;
float suhuB = 0.0;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
// keypad.begin();
pinMode(ntcA, INPUT);
pinMode(ntcB, INPUT);
pinMode(heater, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(motorDir, OUTPUT);
pinMode(motorENA, OUTPUT);
pinMode(motorPul, OUTPUT);
digitalWrite(motorDir, 0); // set motor berputar counter clockwise
// digitalWrite(motorPul, LOW);
digitalWrite(motorENA,LOW);
digitalWrite(heater,LOW);
lcd.begin();
lcd.backlight();
lcd.clear();
// Tampilkan menu awal
tampilkanMenu();
}
void loop() {
bacaSuhu();
//pulse digenerate terus menerus nek dikek i if koyok ngisor iki
if (suhuAvg >= setpointSuhu) {
digitalWrite(heater, LOW);
} else {
digitalWrite(heater, HIGH);
}
// Baca input dari keypad
char key = keypad.waitForKey();
if (key) {
Serial.print("Key Pressed: ");
Serial.println(key);
if (key == 'A') { // Tombol A: Masukkan Feedrate
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Feedrate:");
feedrate = bacaNilai();
transisi();
} else if (key == 'B') { // Tombol B: Masukkan Setpoint
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Setpoint:");
setpointSuhu = bacaNilai();
transisi();
} else if (key == 'C') { // Tombol C: Tampilkan Feedrate dan Setpoint
nilaiTersimpan();
} else if (key == 'D') {
feedrate = 0;
setpointSuhu = 0;
input = "";
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("Nilai di Reset");
delay(500);
transisi();
} else if (key == '#') { // Tombol #: Nyalakan Sistem
if (setpointSuhu != 0 && feedrate != 0) {
sistemON();
} else {
error(); // Tampilkan pesan error jika setpoint atau feedrate belum diatur
}
} else { // Tombol tidak valid
error();
}
}
}
void bacaSuhu() {
adc1 = analogRead(ntcA);
adc2 = analogRead(ntcB);
suhuA = adc1 * (3.3 / 4015.0);
suhuB = adc2 * (3.3 / 4015.0);
suhuAvg = (suhuA + suhuB) / 2;
delay(500);
}
void transisi() {
lcd.clear();
nilaiTersimpan();
delay(1000);
tampilkanMenu();
}
//tampilan nilai feedrate dan setpoint suhu yang sudah diinputkan
void nilaiTersimpan() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Feedrate:");
lcd.print(feedrate);
lcd.setCursor(0, 2);
lcd.print("Setpoint:");
lcd.print(setpointSuhu);
}
// Fungsi untuk menampilkan menu awal
void tampilkanMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A. Masukkan Feedrate");
lcd.setCursor(0, 1);
lcd.print("B. Masukkan Setpoint");
lcd.setCursor(0, 2);
lcd.print("C. Cek Nilai");
lcd.setCursor(0, 3);
lcd.print("D. Reset Nilai");
}
// Fungsi untuk membaca nilai Feedrate
int bacaNilai() {
input = "";
lcd.setCursor(0, 1);
lcd.print("> ");
while (true) {
char key = keypad.waitForKey();
if (key) {
if (key == '#') { // Tombol '#' sebagai enter
break;
} else if (key == '*') { // Tombol *: Tampilkan Menu
input = "0";
break;
}else if (key >= '0' && key <= '9') {
input += key;
lcd.print(key);
} else if (key == 'D') { // Tombol 'D' sebagai backspace
if (input.length() > 0) {
input.remove(input.length() - 1);
lcd.setCursor(2, 1);
lcd.print(" "); // Clear line
lcd.setCursor(2, 1);
lcd.print(input);
}
}
}
}
return input.toInt();
}
void error() {
lcd.clear();
lcd.setCursor(3, 1);
lcd.print("Sistem Error");
delay(3000);
lcd.clear();
tampilkanMenu();
}
void monitorSuhu(bool statusMuncul) {
if (statusMuncul) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("NTC1 :");
lcd.print(suhuA);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("NTC2 :");
lcd.print(suhuB);
lcd.print(" C");
lcd.setCursor(0, 2);
lcd.print("Rata suhu:");
lcd.print(suhuAvg);
lcd.setCursor(0, 3);
lcd.print("Fan ON");
}
}
void sistemON() {
digitalWrite(fan, HIGH);
digitalWrite(heater, HIGH);
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("Ekstruder Aktif");
delay(2000);
monitorSuhu(true);
stepper();
// if (suhuA >= lelehFilament && suhuB >= lelehFilament) {}
while(isRunning) {
generatePulses(pulseCount);
if (keypad.getKey() == '*') {
sistemOFF(true);
return tampilkanMenu(); // Exit the function
}
}
}
void sistemOFF(bool off) {
if (off) {
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("Ekstruder Berhenti");
delay(1000); // Display the stop message
digitalWrite(fan, LOW); // Turn off the fan
digitalWrite(heater, LOW); // Turn off the heater
monitorSuhu(false);
isRunning = false;
}
}
void stepper() {
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("Stepper");
lcd.setCursor(6, 2);
lcd.print("Aktif");
delay(1000);
lcd.clear();
monitorSuhu(true);
double stepspermm = 200 / 23;
pulseCount = (feedrate / 60) * stepspermm; // feedrate harus diubah dulu dengan persamaan agar pulse sesuia
if (pulseCount > 0) {
isRunning = true;
} else {
isRunning = false;
}
}
void generatePulses(int count) {
unsigned long interval = 1000000 / count; // Interval waktu dalam mikrodetik
for (int i = 0; i < count; i++) {
digitalWrite(motorPul, HIGH); // Set pin HIGH
delayMicroseconds(interval / 2); // Tunggu setengah interval
digitalWrite(motorPul, LOW); // Set pin LOW
delayMicroseconds(interval / 2); // Tunggu setengah interval
}
}