#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define ok 8
#define up 9
#define down 10
#define back 11
int count = 0;
int PBdelay = 200;
int stateA = 0;
int push = 0;
int qty = 0;
int len = 0;
int countA = 0;
int countB = 0;
int i = 0;
#define feedstepPin 3
#define feeddirPin 2
#define cutstepPin 5
#define cutdirPin 4
float feedsteppermm = 105;
float cutstep = 3200;
#define CLK 6
#define DT 7
#define SW 12
int counter = 0;
int counterA = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
void setup(){
pinMode (ok,INPUT_PULLUP);
pinMode (up,INPUT_PULLUP);
pinMode (down,INPUT_PULLUP);
pinMode (back,INPUT_PULLUP);
pinMode (feedstepPin,OUTPUT);
pinMode (feeddirPin,OUTPUT);
pinMode (cutstepPin,OUTPUT);
pinMode (cutdirPin,OUTPUT);
pinMode(CLK,INPUT_PULLUP);
pinMode(DT,INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
Serial.begin (9600);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop(){
if (!digitalRead(SW)){
if (push >= 3){
push = 0;
} else {
push++;
}
delay(PBdelay);
Serial.println(push);
lcd.clear();
}
if (!digitalRead(back)){
if (push < 5 && push > 0){
push--;
}
delay(PBdelay);
Serial.println(push);
lcd.clear();
}
switch (push){
case 0:
homescreen();
break;
case 1:
setlength();
break;
case 2:
setqty();
break;
case 3:
confirm();
break;
case 4:
finish();
break;
}
if (!digitalRead(up)&& stateA ==2){
countA++;
delay(PBdelay);
len = countA;
}
if (!digitalRead(down)&& stateA ==2){
if(len > 0){
countA--;
delay(PBdelay);
len = countA;
} else {
len = 0;
}
}
if (!digitalRead(up)&& stateA ==3){
countB++;
delay(PBdelay);
qty = countB;
}
if (!digitalRead(down)&& stateA ==3){
if(qty > 0){
countB--;
delay(PBdelay);
qty = countB;
} else {
qty = 0;
}
}
if (!digitalRead(SW)&& stateA==4){
lcd.clear();
push = 4;
int steptotake = len * feedsteppermm *10;
for (i = 0; i<qty; i++){
Cutting();
digitalWrite(feeddirPin,HIGH);
for (int x = 0; x < steptotake; x++){
digitalWrite(feedstepPin ,HIGH);
delayMicroseconds(100);
digitalWrite(feedstepPin,LOW);
delayMicroseconds(100);
}
digitalWrite(cutdirPin,HIGH);
for (int y = 0; y < cutstep; y++){
digitalWrite(cutstepPin ,HIGH);
delayMicroseconds(100);
digitalWrite(cutstepPin,LOW);
delayMicroseconds(100);
}
digitalWrite(cutdirPin,LOW);
for (int y = 0; y < cutstep; y++){
digitalWrite(cutstepPin ,HIGH);
delayMicroseconds(100);
digitalWrite(cutstepPin,LOW);
delayMicroseconds(100);
}
}
}
if(!digitalRead(SW)&& stateA==5){
lcd.clear();
push = 0;
len = 0;
qty = 0;
countA = 0;
countB = 0;
delay(PBdelay);
}
}
void encoder(){
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
counter ++;
currentDir ="CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
lastButtonPress = millis();
}
delay(1);
}
void homescreen(){
lcd.setCursor(0, 0);
lcd.print("WIRE CUTTER");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
stateA = 1;
}
void setlength(){
lcd.setCursor(0, 0);
lcd.print("SET LENGTH");
lcd.setCursor(0, 1);
lcd.print(len);
lcd.print(" ");
lcd.setCursor(5, 1);
lcd.print("CM");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
stateA = 2;
}
void setqty(){
lcd.setCursor(0, 0);
lcd.print("SET QUANTITY");
lcd.setCursor(0, 1);
lcd.print(qty);
lcd.print(" ");
if (qty<2){
lcd.setCursor(5, 1);
lcd.print("NO.");
lcd.print(" ");
} else {
lcd.setCursor(5, 1);
lcd.print("NOs.");
}
lcd.setCursor(11, 1);
lcd.print("NEXT>");
stateA = 3;
}
void confirm(){
lcd.setCursor(0, 0);
lcd.print("LENGTH = ");
lcd.setCursor(9, 0);
lcd.print(len);
lcd.setCursor(11, 0);
lcd.print("CM");
lcd.setCursor(0, 1);
lcd.print("QTY = ");
lcd.setCursor(6, 1);
lcd.print(qty);
lcd.setCursor(11, 1);
lcd.print("READY");
stateA = 4;
}
void Cutting(){
lcd.setCursor(0, 0);
lcd.print("CUTTING ");
lcd.setCursor(8, 0);
int k = qty-i;
if(k==9){
lcd.clear();
}
lcd.print(k);
lcd.setCursor(11, 0);
lcd.print("OF " +(String)qty);
}
void finish(){
lcd.setCursor(0, 0);
lcd.print("FINISH.... ");
lcd.setCursor(10, 1);
lcd.print("HOME>");
stateA = 5;
}