#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//pin kipas heater
const int fan = 18;
const int heater = 19;
const int 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] = { 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;
int adc1 = 0;
int adc2 = 0;
double suhuAvg = 0.0;
float suhuA = 0.0;
float suhuB = 0.0;
void setup() {
pinMode(ntcA, INPUT);
pinMode(ntcB, INPUT);
pinMode(motorDir, OUTPUT);
digitalWrite(motorDir, HIGH); // set motor berputar clockwise
pinMode(motorENA, OUTPUT);
pinMode(motorPul, OUTPUT);
digitalWrite(motorPul, 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.getKey();
if (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 == '*') { // Tombol *: Tampilkan Menu
tampilkanMenu();
} 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(2000);
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 Status");
}
// Fungsi untuk membaca nilai Feedrate
int bacaNilai() {
String input = "";
lcd.setCursor(0, 1);
lcd.print("> ");
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Tombol '#' sebagai enter
break;
} else if (key >= '0' && key <= '9') {
input += key;
lcd.print(key);
} else if (key == 'D') { // Tombol '*' 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();
while (true) {
if (isRunning) {
generatePulses(pulseCount); // Jalankan generatePulses terus-menerus
digitalWrite(motorENA, LOW);
} else {
digitalWrite(motorENA, HIGH);
}
char key = keypad.getKey();
if (key == 'D') {
// If 'D' is pressed, stop the fan, heater, and motor stepper
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() {
double stepspermm = 200 / 23;
pulseCount = (feedrate / 60) * stepspermm; // feedrate harus diubah dulu dengan persamaan agar
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
}
}