//------------------------------- librarys ----------------------------------
#include <Wire.h>
//#include <rgb_lcd.h>
//#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
//------------------------------- lcd ----------------------------------
//rgb_lcd lcd;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//------------------------------- stepper ----------------------------------
//#define stepPin 49
//#define dirPin 50
#define stepPin 3
#define dirPin 2
#define enablePin 4
//------------------------------- servo ----------------------------------
Servo snippers;
#define servo 46
#define openAngle 90
#define closedAngle 0
//------------------------------- input ----------------------------------
// Keypad setup
//const byte ROWS = 4;
//const byte COLS = 3;
//char keys[ROWS][COLS] = {
// {'1', '2', '3' },
// {'4', '5', '6' },
// {'7', '8', '9'},
// {'*', '0', '#'}
//};
//byte rowPins[ROWS] = {18,19,20,21};
//byte colPins[COLS] = {15,16,17};
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B' },
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13,12,11,10};
byte colPins[COLS] = {9,8,7,6};
//byte rowPins[ROWS] = {18,19,22,23};
//byte colPins[COLS] = {14,15,16,17};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char ButtonState = 0;
//#define leftButton 3
//#define rightButton 2
//#define upButton 4
//#define downButton 5
#define START '*' //Maps Character inputs to logical Button Names
#define STOP '#'
#define F1 'A'
#define F2 'B'
#define F3 'C'
#define F4 'D'
#define NOKEY 0
#define LEFT '4'
#define RIGHT '6'
#define UP '2'
#define DOWN '5'
//-------------------------------output ----------------------------------
#define Mpos 37
#define Mcut 38
//------------------------------- user settings ----------------------------------
float wireLength = 0.0;
unsigned int wireQuantity = 0;
int cuttime = 1000;
int positiontime = 1000;
int Maintimer =2500;
int Homesteps =20000;
//------------------------------- system settings ----------------------------------
int state = 0;
int incrementSpeed = 0;
int previousWireLength = 0.0;
int previousWireQuantity = 0;
float mmPerStep = 0.003125;
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
//lcd.begin(16, 2); //LCD columns and rows
// lcd.setRGB(255,255,0); //backlight colour
//
pinMode(5, INPUT_PULLUP);
// pinMode(downButton, INPUT_PULLUP);
// pinMode(leftButton, INPUT_PULLUP);
// pinMode(rightButton, INPUT_PULLUP);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(Mpos,OUTPUT);
pinMode(Mcut,OUTPUT);
snippers.attach(servo);
snippers.write(openAngle);
}
void loop() {
if (ButtonState == STOP){
if(state == 5){
state = 0;
}
else{
state += 1;
}
delay(100);
lcd.clear();
}
if (ButtonState == START && state > 0 && state < 4){
state -=1;
delay(100);
lcd.clear();
}
ButtonState = keypad.getKey();
switch (state){
case 0:
homeScreen();
break;
// case 1:
// chooseWireLength();
// break;
case 1:
handleLengthInput();
break;
case 2:
chooseWireQuantity();
break;
case 3:
confirm();
break;
case 4:
currentlyCutting();
break;
case 5:
finishedCutting();
break;
}
}
void homeScreen(){
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("ALUMINIUM CUTTER");
lcd.setCursor(15, 3);
lcd.print("NEXT>");
delay(100);
if (Homesteps==20000 ) {
for(int x = 0; x < Homesteps; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(400);
digitalWrite(stepPin,HIGH);
delayMicroseconds(200);
if (digitalRead(5) != HIGH)
{
Homesteps=0;
}
}
}
}
//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("MHKOS:" + (String)wireLength + "mm");
// 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("POSOTHTA:" + (String)wireQuantity);
displayNavigation();
}
void handleLengthInput() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Length: " + String(wireLength) + " mm");
displayNavigation();
char key = keypad.getKey();
if (key == '#') {
// Handle '#' key press (confirmation)
state = 2; // Go to the next state for wire quantity input
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Length: " + String(wireLength) + " mm");
lcd.setCursor(0, 1);
lcd.print("Press # to confirm");
displayNavigation();
} else if (key == '*') {
// Handle '*' key press (back)
state = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Length: " + String(wireLength) + " mm");
} else if (key == 'D') {
// Handle 'D' key press (clear input)
wireLength = 0.0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Length: " + String(wireLength) + " mm");
displayNavigation();
} else if (isdigit(key)) {
// Accumulate length input
wireLength = wireLength * 10 + (key - '0');
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Length: " + String(wireLength) + " mm");
displayNavigation();
}
}
void chooseWireLength() {
static String inputBuffer = ""; // Buffer to store user input
// Handle keypad input
char key = keypad.getKey();
if (key == NO_KEY) {
if (key != '#') {
// Use the '*' key to confirm the input and set the wire length
if (inputBuffer.length() > 0) {
// Convert the inputBuffer to a float
wireLength = inputBuffer.toInt();
inputBuffer = ""; // Clear the input buffer
lcd.clear(); // Clear the LCD
displayNavigation();
}
} else if (key != '*') {
// Use the '#' key to clear the input
inputBuffer = "";
lcd.clear();
displayNavigation();
} else if (isdigit(key) && inputBuffer.length() < 6) {
// Allow only digits and limit input to 6 characters
inputBuffer += key;
lcd.setCursor(0, 0);
lcd.print("MHKOS: " + inputBuffer + "mm"); // Display the input
lcd.setCursor(0, 1);
lcd.print("<BACK *CONFIRM>");
}
}
else {
lcd.setCursor(0, 0);
lcd.print("MHKOS: " + inputBuffer + "mm"); // Display the input
lcd.setCursor(0, 1);
lcd.print("<BACK *CONFIRM>");
}
}
void confirm(){
lcd.setCursor(0, 0);
lcd.print((String)wireLength + "mm x " + (String)wireQuantity + "pcs");
lcd.setCursor(0, 3);
lcd.print("<PISW");
lcd.setCursor(11, 3);
lcd.print("EKKINHSH>");
delay(100);
}
void currentlyCutting(){
lcd.setCursor(0, 0);
lcd.print((String)0 + "/" + (String)wireQuantity);
lcd.setCursor(0, 1);
lcd.print("Ypologismos xronou");
//Anoigma meggenis kopis kleisimo meggenis topothetisis
digitalWrite(Mcut,LOW);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("MCUT OFF");
delay(1000); // Edw mporei na mpei ena interlock asfaleias ean anoiksa oi meggenes kopis
digitalWrite(Mpos,HIGH);
lcd.setCursor(7,1);
lcd.print("MPOS ON");
delay(positiontime);
//Metakinisi stin epithimiti thesi
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("METAKINHSH");
int stepsToTake = (float)wireLength/mmPerStep;
for(int i = 0; i < wireQuantity; i++){
unsigned long timeForOneCycle = millis();
digitalWrite(dirPin,HIGH);
for(int x = 0; x < stepsToTake; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(200);
digitalWrite(stepPin,LOW);
delayMicroseconds(200);
}
// Oloklirwsi metakinhshs
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Move is done!");
delay(Maintimer);
lcd.clear();
lcd.setCursor(7,0);
lcd.print("MCUT ON");
digitalWrite(Mcut,HIGH);
delayMicroseconds(1000);
lcd.setCursor(7,1);
lcd.print("MPOS OFF");
digitalWrite(Mpos,LOW);
delay(Maintimer);
int stepsToTake = (float)wireLength/mmPerStep;
digitalWrite(dirPin,LOW);
for(int x = 0; x < stepsToTake; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(200);
digitalWrite(stepPin,LOW);
delayMicroseconds(200);
}
lcd.clear();
lcd.setCursor(3,0);
lcd.print("CUTTING");
snippers.write(openAngle);
delay(cuttime);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("CUT IS DONE");
delay(Maintimer);
lcd.setCursor(7, 0);
lcd.print("MCUT OFF");
digitalWrite(Mcut,LOW);
delayMicroseconds(1000);
lcd.setCursor(7,1);
lcd.print("MPOS ON");
digitalWrite(Mpos,HIGH);
delay(positiontime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print((String)(i+1) + "/" + (String)wireQuantity);
lcd.setCursor(0, 1);
unsigned long timeRemaining = ((millis() - timeForOneCycle)*(wireQuantity - (i+1)))/1000;
lcd.print((String)timeRemaining + "s ");
snippers.write(closedAngle);
lcd.setCursor(0, 1);
lcd.print((String)timeRemaining + "s ");
}
wireLength = 0;
wireQuantity = 0;
state = 5;
}
void finishedCutting(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CUT IS COMPLETED");
lcd.setCursor(15, 3);
lcd.print("NEXT>");
delay(100);
}
float changeValue(float currentValue){
if (ButtonState == UP) {
currentValue ++;
incrementSpeed =1;
}
if (ButtonState == START) {
currentValue = currentValue+0.1;
incrementSpeed =10;
}
if (ButtonState == DOWN) {
if(currentValue - incrementSpeed >= 0){
currentValue -= incrementSpeed;
incrementSpeed =1;
}
else{
currentValue = 0;
}
}
return currentValue;
}
void displayNavigation(){
lcd.setCursor(0, 3);
lcd.print("<BACK");
lcd.setCursor(15, 3);
lcd.print("NEXT>");
delay(100);
}