/*REV_7 WireStrp_add stepper
8.25.2025
-try finish up the rotary phase. after all input. say "proceed" yes or no.
if no, will go over previous input from the begining
-Try add the stepper communication
*/
//lcd
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//rotary encoder
const unsigned int ROTARY_ENC_PIN_A = 2;
const unsigned int ROTARY_ENC_PIN_B = 3;
const unsigned int ROTARY_ENC_SWITCH = 4;
#define NO_CHANGE 0
#define TURN_CW 1
#define TURN_CCW 2
//Direction ┌─ ccw ─┐ N ┌─ cw ─┐
//Index 0 1 2 3 4 5 6 7 8
byte aState[] = { 3, 2, 0, 1, 3, 2, 0, 1, 3 };
byte lastState = 3;
volatile int count = 0;
unsigned int index = 4;
volatile byte encoderStatus = NO_CHANGE;
//CUSTOM CHARACTOR FOR "ADD WIRE LENGTH"================
byte bracketLeft[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00001,
B11111,
B00001,
};
byte mid1[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte mid2[] = {
B01010,
B00100,
B01010,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte mid3[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte bracketRight[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B10000,
B11111,
B10000,
};
//CUSTOM CHARACTOR FOR "ADD STRIP LENGTH"================
byte bracketLeftS[] = {
B01010,
B00100,
B01010,
B00000,
B00000,
B00001,
B11111,
B00001,
};
byte mid1S[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte mid2S[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte mid3S[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte bracketRightS[] = {
B01010,
B00100,
B01010,
B00000,
B00000,
B10000,
B11111,
B10000,
};
//===========user define functions below============================
void readEncoderStatus() {
// reads encoder pins
// updates encoderStatus
// updates count
byte A_Output = digitalRead(ROTARY_ENC_PIN_A);
byte B_Output = digitalRead(ROTARY_ENC_PIN_B);
byte currState = (A_Output * 2) + B_Output;
if (currState != lastState) {
if (currState == aState[index + 1]) {
index++;
if (index == 8) {
count++;
index = 4;
encoderStatus = TURN_CW;
}
} else if (currState == aState[index - 1]) {
index--;
if (index == 0) {
count--;
index = 4;
encoderStatus = TURN_CCW;
}
}
lastState = currState;
}
}
//=========user define functions=====================
void displayGetWireLength(int len) {
//CUSTOM CHARACTER
lcd.clear();
lcd.createChar(0, bracketLeft);
lcd.createChar(1, mid1);
lcd.createChar(2, mid2);
lcd.createChar(3, mid3);
lcd.createChar(4, bracketRight);
//WIRE LENGTH
lcd.setCursor(0, 0);
lcd.print("ADD WIRE LENGTH"); //CHANGED WORDING
lcd.setCursor(0, 1);
lcd.print("MM="); //CHANGE TO MM
lcd.print(len);
//CUSTOM CHARACTER
lcd.setCursor(7, 1);
lcd.write(byte(0)); // bracketLeft
lcd.write(byte(1)); // mid1
lcd.write(byte(2)); // mid2
lcd.write(byte(3)); // mid3
lcd.write(byte(4)); // bracketRight
}
void displayGetStripLength(int len) {
//CUSTOM CHARACTER
lcd.clear();
lcd.createChar(0, bracketLeftS);
lcd.createChar(1, mid1S);
lcd.createChar(2, mid2S);
lcd.createChar(3, mid3S);
lcd.createChar(4, bracketRightS);
//STRIP LENGTH
lcd.setCursor(0, 0);
lcd.print("ADD STRIP ENDS"); //CHANGED WORDING
lcd.setCursor(0, 1);
lcd.print("MM="); //CHANGED TO MM
lcd.print(len);
//CUSTOM CHARACTER
lcd.setCursor(7, 1);
lcd.write(byte(0)); // bracketLeftS (NOTE "S" FOR STRIP)
lcd.write(byte(1)); // mid1S
lcd.write(byte(2)); // mid2S
lcd.write(byte(3)); // mid3S
lcd.write(byte(4)); // bracketRightS
}
void displayGetQty(int qty) {
//QUANTIY
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("QUANTITY");
lcd.setCursor(0, 1);
lcd.print("QTY=");
lcd.print(qty);
}
//WHERE IM STUCK, HOW TO SET UP A "YES OR NO" CONFIRMATION CHOICE? IF YES THEN PROCEED, IF NO THEN GO BACK AND ADJUST BEFORE CONFIRMING
void displayGetConfrimation(int){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("L:");
lcd.print("S");
lcd.print("Q");
lcd.setCursor(0,1);
lcd.print("OK?");
lcd.print("[YES] NO ");
}
// Enumeration
enum STATES {
WELCOME_MSG, // this is automatically assigned 0
GET_WIRE_LENGTH, // this is automatically assigned 1
GET_STRIP_LENGTH, // this is automatically assigned 2
GET_QTY, //ADDED QTY
GET_CONFIRMATION, //DONT KNOW HOW TO STATE OPTIONS YES OR NO
STRIP_WIRE,
CUT_WIRE,
};
// Global state variables
byte current_state = WELCOME_MSG; // What value do you think this holds right now? "0"
int wire_length = 0;
int strip_length = 0;
int qty = 0; //ADDED THIS
bool confirmed = false; //0
void setup() {
//setup lcd
lcd.init(); //MUST ADD TO SHOW UP ON LCD*
lcd.backlight();
lcd.begin(16, 2);
//for debugging
Serial.begin(9600);
Serial.println("Starting, press enter or rotate");
//set up encoder interrups
attachInterrupt(digitalPinToInterrupt(ROTARY_ENC_PIN_A), readEncoderStatus, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_ENC_PIN_B), readEncoderStatus, CHANGE);
pinMode(ROTARY_ENC_SWITCH, INPUT_PULLUP);
//welcome message
lcd.setCursor(0, 0);
lcd.print("Wire Stripper");
lcd.setCursor(0, 1);
lcd.print("Program");
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Turn Knob to");
lcd.setCursor(0, 1);
lcd.print("choose values");
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Knob");
lcd.setCursor(0, 1);
lcd.print("to select");
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Knob");
lcd.setCursor(0, 1);
lcd.print("now to start!");
// Wait until user presses button
while (current_state == WELCOME_MSG) {
// Check if button pressed
if (!digitalRead(ROTARY_ENC_SWITCH)) {
current_state = GET_WIRE_LENGTH;
;
//debounce switch press
delay(20);
while (!digitalRead(ROTARY_ENC_SWITCH))
;
delay(20);
displayGetWireLength(wire_length);
}
}
}
void loop() {
//get wire length
if (current_state == GET_WIRE_LENGTH) {
// Did they enter a new value?
if (encoderStatus != NO_CHANGE) {
int Count;
noInterrupts();
Count = count;
interrupts();
encoderStatus = NO_CHANGE;
wire_length = Count;
//debugging
//Serial.print("wire length: ");
//Serial.println(wire_length);
displayGetWireLength(wire_length);
}
// Check if button pressed
if (!digitalRead(ROTARY_ENC_SWITCH)) {
count = 0;
current_state = GET_STRIP_LENGTH;
displayGetStripLength(strip_length);
//debounce switch press
delay(10);
while (!digitalRead(ROTARY_ENC_SWITCH))
;
delay(10);
}
}
//get strip length
if (current_state == GET_STRIP_LENGTH) {
// Did they enter a new value?
if (encoderStatus != NO_CHANGE) {
int Count;
noInterrupts();
Count = count;
interrupts();
encoderStatus = NO_CHANGE;
strip_length = count;
//debugging
//Serial.print("strip length: ");
//Serial.println(strip_length);
displayGetStripLength(strip_length);
}
// Check if button pressed
if (!digitalRead(ROTARY_ENC_SWITCH)) {
count = 0; // resest count for next state
current_state = GET_QTY; // This moves us to the next state
displayGetQty(qty);
//debounce switch press
delay(10);
while (!digitalRead(ROTARY_ENC_SWITCH))
;
delay(10);
}
}
//get QTY
if (current_state == GET_QTY) {
if (encoderStatus != NO_CHANGE) {
int Count;
noInterrupts();
Count = count;
interrupts();
encoderStatus = NO_CHANGE;
qty = count;
//debugging
//Serial.print("quantity: ");
// Serial.println(qty);
displayGetQty(qty);
}
// Check if button pressed
if (!digitalRead(ROTARY_ENC_SWITCH)) {
count = 0;
current_state = GET_CONFIRMATION;
displayGetWireLength(wire_length);
//debounce switch press
delay(10);
while (!digitalRead(ROTARY_ENC_SWITCH))
;
delay(10);
}
}
// Get confirmation
if (current_state == GET_CONFIRMATION) {
if (encoderStatus != NO_CHANGE) {
int Count;
noInterrupts();
// set to 0 if greater or less than 1
if(count > 1 || count < 1) {
count == 0;
}
Count = count;
interrupts();
encoderStatus = NO_CHANGE;
confirmed = Count;
//debugging
//Serial.print("quantity: ");
// Serial.println(qty);
displayConfirmation(confirm);
}
// Check if button pressed
if (!digitalRead(ROTARY_ENC_SWITCH)) {
//count = 0;
// if count == 0 else --> startover
//else proceed to
current_state = GET_WIRE_LENGTH;
displayGetWireLength(wire_length);
//debounce switch press
delay(10);
while (!digitalRead(ROTARY_ENC_SWITCH))
;
delay(10);
}
}
}