#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 7
#define feeddirPin 6
#define cutstepPin 5
#define cutdirPin 4
float feedsteppermm = 105;
float cutstep = 3200;
const int RotaryCLK = 2; //CLK pin on the rotary encoder
const int RotaryDT = 12; //DT pin on the rotary encoder
const int PushButton = 3; //Button to enter/exit menu
//Statuses for the rotary encoder
int CLKNow;
int CLKPrevious;
int DTNow;
int DTPrevious;
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(RotaryCLK,INPUT_PULLUP);
pinMode(RotaryDT,INPUT_PULLUP);
pinMode(PushButton, INPUT_PULLUP);
CLKPrevious = digitalRead(RotaryCLK);
DTPrevious = digitalRead(RotaryDT);
attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE); //CLK pin is an interrupt pin
//attachInterrupt(digitalPinToInterrupt(PushButton), ushButton, FALLING); //PushButton pin is an interrupt pin
Serial.begin (9600);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop(){
if (!digitalRead(PushButton)){
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(RotaryCLK)&& stateA ==2){
rotate();
delay(PBdelay);
len = countA;
}
if (!digitalRead(RotaryCLK)&& stateA ==3){
countB++;
delay(PBdelay);
qty = countB;
}
if (!digitalRead(PushButton)&& 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(PushButton)&& stateA==5){
lcd.clear();
push = 0;
len = 0;
qty = 0;
countA = 0;
countB = 0;
delay(PBdelay);
}
}
void rotate(){
//-----LENGTH--------------------------------------------------------------
if(stateA ==2){
CLKNow = digitalRead(RotaryCLK);
if (CLKNow != CLKPrevious && CLKNow == 1){
if (digitalRead(RotaryDT) != CLKNow) {
if(countA < 6000){ //we do not go above 6000
countA++;
} else {
countA = 0;
}
} else {
if(countA < 1) {//we do not go below 0
countA = 6000;
} else {
countA--;
}
}
}
CLKPrevious = CLKNow; // Store last CLK state
}
//---QUANTITY---------------------------------------------------------------
else if(stateA ==3) {
CLKNow = digitalRead(RotaryCLK);
if (CLKNow != CLKPrevious && CLKNow == 1){
if (digitalRead(RotaryDT) != CLKNow) {
if(countB < 500){ //we do not go above 500
countB++;
} else {
countB = 0;
}
} else {
if(countB < 1){ //we do not go below 0
countB = 500;
} else {
countB--;
}
}
}
CLKPrevious = CLKNow; // Store last CLK state
}
}
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;
}