#include <LiquidCrystal_I2C.h>
//lcd -----------------
LiquidCrystal_I2C lcd(0x27,16,2);
//stepper -----------
#define stepPin 6
#define dirPin 5
#define stepPin2 8
#define dirPin2 7
#define stepdelay 400
//input -----
#define leftButton 14
#define rightButton 16
#define upButton 15
#define downButton 17
//user setting--------
unsigned int wireLength = 14;
unsigned int wireQuantity = 50;
//system settings ---------------
int state = 0;
int incrementSpeed = 1;
int previousWireLength = 0;
int previousWireQuantity = 0;
float mmPerStep = 0.16650;
int cm = 10;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
delay(300);
}
void loop() {
if (!digitalRead(rightButton)) {
if (state == 5) {
state = 0;
}
else {
state += 1;
}
delay(200);
lcd.clear();
}
if (!digitalRead(leftButton) && state > 0 && state < 4) {
state -= 1;
delay(200);
lcd.clear();
}
switch (state) {
case 0:
homeScreen();
break;
case 1:
chooseWireLength();
break;
case 2:
chooseWireQuantity();
break;
case 3:
confirm();
break;
case 4:
currentlyCutting();
break;
case 5:
finishedCutting();
break;
}
}
void homeScreen() {
lcd.setCursor(0, 0);
lcd.print("WIRE CUTTER");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}
void chooseWireLength() {
wireLength = changeValue(wireLength);
//clear LCD if required
if (previousWireLength != wireLength) {
lcd.clear();
previousWireLength = wireLength;
}
//Display information on LCD
lcd.setCursor(0, 0);
lcd.print("LENGTH: " + (String)wireLength + " cm");
displayNavigation();
}
void chooseWireQuantity() {
wireQuantity = changeValue(wireQuantity);
//clear LCD if required
if (previousWireQuantity != wireQuantity) {
lcd.clear();
previousWireQuantity = wireQuantity;
}
//Display information on LCD
lcd.setCursor(0, 0);
lcd.print("QUANTITY: " + (String)wireQuantity + " pcs");
displayNavigation();
}
void confirm() {
lcd.setCursor(0, 0);
lcd.print((String)wireLength + " cm x " + (String)wireQuantity + " pcs");
lcd.setCursor(0, 1);
lcd.print("<BACK");
lcd.setCursor(10, 1);
lcd.print("START>");
delay(100);
}
void currentlyCutting() {
lcd.setCursor(0, 0);
lcd.print((String)0 + " dari " + (String)wireQuantity + " pcs");
lcd.setCursor(0, 1);
lcd.print("??? detik selesai");
int stepsToTake = (int)wireLength * int(cm) / mmPerStep;
for (int i = 0; i < wireQuantity; i++) {
unsigned long timeForOneCycle = millis();
digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);
delay(300);
for (int x = 0; x < stepsToTake; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepdelay);
}
delay(300);
lcd.setCursor(0, 0);
lcd.print((String)(i + 1) + " dari " + (String)wireQuantity + " pcs");
for(int i=0; i<200; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay);
}
lcd.setCursor(0, 1);
unsigned long timeRemaining = ((millis() - timeForOneCycle) * (wireQuantity - (i + 1))) / 1000;
lcd.print((String)timeRemaining + " detik selesai");
}
//wireLength = 14;
//wireQuantity = 50;
state = 5;
}
void finishedCutting() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CUTTING COMPLETE");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}
int changeValue(int currentValue) {
if (!digitalRead(upButton)) {
delay(100);
currentValue += incrementSpeed;
}
if (!digitalRead(downButton)) {
if (currentValue - incrementSpeed >= 0) {
delay(100);
currentValue -= incrementSpeed;
}
else {
currentValue = 0;
}
}
if (!digitalRead(downButton) && !digitalRead(upButton)) {
incrementSpeed = 1;
}
return currentValue;
}
void displayNavigation() {
lcd.setCursor(0, 1);
lcd.print("<BACK");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}