#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Key.h>
#include <Servo.h> //library servo
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
Servo myservo;
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] = {11, 10, 9, 8};
byte colPins[COLS] = {7, 6, 5, 4};
Keypad kpd = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte Simbol_derajat = B11011111;
int state = 0;
int push =0;
int qty =0;
int len = 0;
int temp =0;
int i = 0;
int status= 0;
int address = 0; //eeprom
#define feedstepPin 3
#define feeddirPin 2
#define cutstepPin 8
#define cutdirPin 7
const int buzzerPin = 16;
float feedsteppermm = 105;
float cutstep = 3200;
bool isRunning = false;
char input_arr[3];
// Inisialisasi pin data untuk sensor DS18B20
const int oneWireBusPin = 10; // Sesuaikan dengan pin yang digunakan pada Arduino Anda
// Inisialisasi objek OneWire dan DallasTemperature
OneWire oneWire(oneWireBusPin);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode (feedstepPin, OUTPUT);
pinMode (feeddirPin, OUTPUT);
pinMode (cutstepPin, OUTPUT);
pinMode (cutdirPin, OUTPUT);
pinMode(14, OUTPUT);
digitalWrite(14, LOW);
pinMode(buzzerPin, OUTPUT);
myservo.attach(15);
myservo.write(0);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
char keypressed;
int steps, i = 0;
switch(state) {
case 0: //tampilan awal State---------------------------------------------------------------------------------------------
homescreen();
do {
// menunggu input
keypressed = kpd.getKey();
if(keypressed == 'A') {
state++;
digitalWrite(14, HIGH);
}
if(keypressed == 'B') {
status = digitalRead(14);
if (status == 0) {
digitalWrite(14, HIGH);
} else {
digitalWrite(14, LOW);
}
}
if(keypressed == 'C') {
myservo.write(180);
}
if(keypressed == 'D') {
state = 6;
}
} while(keypressed == NO_KEY);
break;
case 1: //masukan panjang------------------------------------------------------------------------------------------
lcd.clear();
setlength();
len = input_handler();
break;
case 2: //masukan jumlah ----------------------------------------------------------------------------------------------
setqty();
qty = input_handler();
break;
case 3:
confirm();
do {
keypressed = kpd.getKey();
if(keypressed == '*') {
state--;
} else if(keypressed == '#') {
state++;
}
} while(keypressed != '#' && keypressed != '*');
break;
case 5:
lcd.clear();
lcd.print("FINISHED CUTTING");
lcd.setCursor(0, 1);
lcd.print(qty);
lcd.setCursor(4, 1);
lcd.print("L x ");
lcd.setCursor(10, 1);
lcd.print(len);
lcd.setCursor(14, 1);
lcd.print("CM");
tone(buzzerPin, 1500, 500);
delay(500);
tone(buzzerPin, 1500, 50);
delay(500);
tone(buzzerPin, 1500, 500);
do {
keypressed = kpd.getKey();
} while(keypressed != '#');
state = 0;
break;
default: //error state
lcd.clear();
lcd.print("ERROR");
delay(5000);
break;
}
if(state == 4) {
lcd.clear();
int steptotake = len * feedsteppermm * 10;
for (i = 0; i < qty; i++) {
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("CUTTING THE WIRES");
lcd.setCursor(0, 1);
int k = i + 1;
lcd.print(k);
lcd.setCursor(4, 1);
lcd.print("< " +(String)qty);
lcd.setCursor(13, 1);
lcd.print("L");
digitalWrite(feeddirPin, HIGH);
for (int x = 0; x < steptotake; x++) {
digitalWrite(feedstepPin, HIGH);
delayMicroseconds(50);
digitalWrite(feedstepPin, LOW);
delayMicroseconds(50);
}
}
state++;
}
}
void homescreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WC MACHINE");
lcd.setCursor(14, 1);
lcd.print("A>");
}
void setlength() {
lcd.clear();
lcd.print("SET LENGTH :");
lcd.setCursor(4,1);
lcd.print("CM");
lcd.setCursor(0,1);
}
void setqty() {
lcd.clear();
lcd.print("SET QUANTITY :");
lcd.setCursor(4,1);
lcd.print("L");
lcd.setCursor(0,1);
}
void confirm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LEGTH = ");
lcd.setCursor(10, 0);
lcd.print(len);
lcd.setCursor(14, 0);
lcd.print("CM");
lcd.setCursor(0, 1);
lcd.print("QUANTITY = ");
lcd.setCursor(12, 1);
lcd.print(qty);
lcd.setCursor(15, 1);
lcd.print("L");
}
int input_handler() {
char keypressed;
int input_counter = 0;
do {//wait for input
keypressed = kpd.getKey();
if(keypressed != NO_KEY) {
if(input_counter != 3) {
input_arr[input_counter] = keypressed;
input_counter++;
lcd.print(keypressed);
tone(buzzerPin, 2000, 50);
}
input_arr[3] = '\0';
}
} while(keypressed != '#' && keypressed != '*' && keypressed != 'A' && keypressed != 'B' && keypressed != 'C' && keypressed != 'D');
if(keypressed == '#') {
sscanf(input_arr,"%d", &input_counter);
state++;
return input_counter;
}
if(keypressed == '*') {
if(state == 7) {
state = 0;
} else {
state--;
}
return input_counter;
} else {
lcd.print(keypressed);
state + 1 - 1;
}
}