#include <Servo.h>
#include <LiquidCrystal_I2C.h>
//lcd-----
LiquidCrystal_I2C lcd(0x27,20,4);
#define stepPin1 7 //umpan
#define dirPin1 6 //umpan
#define stepPin2 9 //cut
#define dirPin2 8 //cut
//servo----
Servo lokasi;
#define servo 10 //pin Servo
#define cut 45 //Derajat servo cut
#define strip 90 //Derajat servo strip
//delay----
#define srvdelay 200 //delay servo
#define stepdelay1 350 //delayms stepper 1
#define stepdelay2 400 //delayms stepper 2
#define jeda 100
//input tombol----
#define pbkiri A0
#define pbkanan A3
#define pbatas A1
#define pbbawah A2
//pengaturan pribadi----
unsigned int panjang = 14; //panjang cm
unsigned int panjangkupas = 4; //panjang mm
unsigned int jumlah = 50; // jumlah pcs
//pengaturan sistem----
int changeValue(int value);
int state = 0;
int incrementSpeed = 1;
int previousWireLength = 0;
int previousStripLength = 0;
int previousWireQuantity = 0;
float mmPerStep = 0.16650;
void feedFwd();
/*void feedRwd();*/
void cutting();
void changeValue();
void displayNavigation();
void homeScreen();
void chooseWireLength();
void chooseStripLength();
void chooseWireQuantity();
void confirm();
void currentlyCutting();
void finishedCutting();
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.init();
lcd.backlight();
pinMode(pbatas, INPUT_PULLUP);
pinMode(pbbawah, INPUT_PULLUP);
pinMode(pbkiri, INPUT_PULLUP);
pinMode(pbkanan, INPUT_PULLUP);
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
lokasi.attach(servo);
delay(500);
lokasi.write(cut);
delay(500);
lokasi.write(strip);
delay(500);
}
void loop() {
if (!digitalRead(pbkanan)) {
if (state == 6) {
state = 0;
}
else {
state += 1;
}
delay(100);
lcd.clear();
}
if (!digitalRead(pbkiri) && state > 0 && state < 5) {
state -= 1;
delay(100);
lcd.clear();
}
if (!digitalRead(pbatas) && state == 0) {
feedFwd();
delay(100);
lcd.clear();
}
if (!digitalRead(pbbawah) && state == 0) {
cutting();
delay(100);
lcd.clear();
}
switch (state) {
case 0:
homeScreen();
break;
case 1:
chooseWireLength();
break;
case 2:
chooseStripLength();
break;
case 3:
chooseWireQuantity();
break;
case 4:
confirm();
break;
case 5:
currentlyCutting();
break;
case 6:
finishedCutting();
break;
}
}
void homeScreen() {
lcd.setCursor(0, 0);
lcd.print("*POTONG KUPAS KABEL*");
lcd.setCursor(0, 1);
lcd.print("> DEPAN KUPAS");
lcd.setCursor(0, 2);
lcd.print("> BELAKANG STRIP");
lcd.setCursor(12, 3);
lcd.print("LANJUT >");
delay(100);
}
void chooseWireLength() {
panjang = changeValue(panjang);
//clear lcd----
if (previousWireLength != panjang) {
lcd.clear();
previousWireLength = panjang;
}
//tampilan lcd----
lcd.setCursor(0, 0);
lcd.print("UBAH PJG TOTAL ?");
lcd.setCursor(0, 1);
lcd.print((String)panjang + " cm");
displayNavigation();
}
void chooseStripLength() {
panjangkupas = changeValue(panjangkupas);
//clear lcd----
if (previousStripLength != panjangkupas) {
lcd.clear();
previousStripLength = panjangkupas;
}
//tampilan lcd----
lcd.setCursor(0, 0);
lcd.print("UBAH PJG STRIP ?");
lcd.setCursor(0, 1);
lcd.print((String)panjangkupas + " mm");
displayNavigation();
}
void chooseWireQuantity() {
jumlah = changeValue(jumlah);
//clear lcd----
if (previousWireQuantity != jumlah) {
lcd.clear();
previousWireQuantity = jumlah;
}
//tampilan lcd----
lcd.setCursor(0, 0);
lcd.print("UBAH JUMLAH ?");
lcd.setCursor(0, 1);
lcd.print((String)jumlah + " pcs");
displayNavigation();
}
void confirm() {
lcd.setCursor(0, 0);
lcd.print("YAKIN ?? klik MULAI");
lcd.setCursor(0, 1);
lcd.print((String)panjang + " cm x " + (String)jumlah + " pcs");
lcd.setCursor(0, 3);
lcd.print("< Kembali");
lcd.setCursor(13, 3);
lcd.print("MULAI >");
delay(100);
}
void currentlyCutting() {
if ( panjangkupas == 0) //panjang kupas = 0, tanpa kupas
{
//tanpa pengupasan----
lcd.setCursor(0, 0);
lcd.print("SEDANG MEMOTONG.....");
lcd.setCursor(0, 1);
lcd.print((String)0 + " dari " + (String)jumlah + " pcs");
lcd.setCursor(0, 3);
lcd.print("??? detik selesai..");
int stepsToTake = (int)panjang * 10 / mmPerStep;
for (int i = 0; i < jumlah; i++) {
unsigned long timeForOneCycle = millis();
lokasi.write(cut);
delay(srvdelay);
digitalWrite(dirPin1, HIGH);
for (int x = 0; x < stepsToTake; x++) { //umpan maju----
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
for(int i=0; i<200; i++) { //potong----
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
lcd.setCursor(0, 1);
lcd.print((String)(i + 1) + " dari " + (String)jumlah + " pcs");
lcd.setCursor(0, 3);
unsigned long timeRemaining = ((millis() - timeForOneCycle) * (jumlah - (i + 1))) / 1000;
lcd.print((String)timeRemaining + " detik selesai..");
}
state = 6;
}
else
{
//dengan pengupasan----
lcd.setCursor(0, 0);
lcd.print("SEDANG MEMOTONG.....");
lcd.setCursor(0, 1);
lcd.print((String)0 + " dari " + (String)jumlah + " pcs");
lcd.setCursor(0, 3);
lcd.print("??? detik selesai..");
//Hitung Langkah yang harus diambil
int stripStepsToTake = (int)panjangkupas / mmPerStep;
int totalStepsToTake = (int)panjang * 10 / mmPerStep;
/*int twoStripsSteps = stripStepsToTake * 2;*/
/*int stepsToTake = totalStepsToTake - twoStripsSteps;*/
int stripsSteps = stripStepsToTake * 1;
int stepsToTake2 = totalStepsToTake - stripsSteps;
for (int i = 0; i < jumlah; i++) {
unsigned long timeForOneCycle = millis();
digitalWrite(dirPin1, HIGH);
//umpan strip depan maju----
for (int x = 0; x < stripStepsToTake; x++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
//keep strip 180 derajat---
for(int i=0; i<100; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay2);
}
delay(100);
digitalWrite(dirPin1, LOW);
//umpan strip depan mundur----
for (int x = 0; x < stripStepsToTake; x++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
//lepas strip 180 derajat----
for(int i=0; i<100; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay2);
}
delay(100);
//Umpan kabel tengah----
for (int x = 0; x < stepsToTake2; x++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
//strip belakang----
for(int i=0; i<200; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay2);
}
delay(100);
//umpan strip belakang----
for (int x = 0; x < stripStepsToTake; x++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
//pemotongan kabel----
lokasi.write(cut);
delay(srvdelay);
for(int i=0; i<200; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay2);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay2);
}
delay(100);
lokasi.write(strip);
delay(srvdelay);
lcd.setCursor(0, 1);
lcd.print((String)(i + 1) + " dari " + (String)jumlah + " pcs");
lcd.setCursor(0, 3);
unsigned long timeRemaining = ((millis() - timeForOneCycle) * (jumlah - (i + 1))) / 1000;
lcd.print((String)timeRemaining + " detik selesai..");
}
state = 6;
}
}
void finishedCutting() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("---POTONG SELESAI---");
lcd.setCursor(12, 3);
lcd.print("LANJUT >");
delay(100);
}
int changeValue(int currentValue) {
if (!digitalRead(pbatas)) {
delay(100);
currentValue += incrementSpeed;
}
if (!digitalRead(pbbawah)) {
if (currentValue - incrementSpeed >= 0) {
delay(100);
currentValue -= incrementSpeed;
}
else {
currentValue = 0;
}
}
if (!digitalRead(pbatas) && !digitalRead(pbbawah)) {
incrementSpeed = 1;
}
return currentValue;
}
void displayNavigation() {
lcd.setCursor(0, 3);
lcd.print("< Kembali");
lcd.setCursor(12, 3);
lcd.print("LANJUT >");
delay(100);
}
void feedFwd() { //operasi umpan maju----
for (int x = 0; x < 60; x++) {
digitalWrite(dirPin1, LOW);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
}
/*void feedRwd() { //operasi umpan mundur----
for (int x = 0; x < 50; x++) {
digitalWrite(dirPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepdelay1);
}
}*/
void cutting() { //Operasi pemotongan
lokasi.write(cut);
delay(srvdelay);
for(int i=0; i<200; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay1);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay1);
}
delay(100);
lokasi.write(strip);
delay(srvdelay);
}