//Libary for LCD Screen
#include <LiquidCrystal_I2C.h>
//Define Button Pins
const int UP_BUTTON = 13;
const int DOWN_BUTTON = 15;
#define SELECT_BUTTON 35
//Define LED Pins
#define LED_CARD_DETECTED 16
#define LED_CARD_NOT_DETECTED 17
const int WIFI_PIN = 12;
//Define IR Sensor Pins
const int SENSOR_PIN = 27;
const int RFID_PIN = 33; //**this will be removed when the RFID is added
//**can change these variables
String mode = "MultiDisplay"; // Stationary = only run press screen MultiDisplay = multiple pages
//Define Variables
String cardUID = "";
String cardProduct;// = ""; //**This Should be blank
int productCount = 0;
const int numRows = 40;//Max number of operators or work orders for Product array size
//Define Flag Variables
bool lcdUpdated = false; // Flag to track whether the LCD has been updated
bool wasHigh = false; //Flag to track whether the IR sensor was high or not
int previousP=0;
int matchingRow = -1; // finding the row of the variable in the productinfo array that matches with the UID; initialize to -1 to indicate no match found
int upButtonState;// used for the reset function
int downButtonState;// used for the reset function
const unsigned long RESET_DELAY = 1000; // hold up and down for this long to reset the esp32
static unsigned long buttonPressStartTime = 0; // used for the reset function
//Define Variables for Scrolling
int item_selected = 0; //which item in the menu is selected
int item_sel_previous; // previous item - item before the selected one
int item_sel_next; // next item - item after the selected one
int NUM_ITEMS; //defines the number of rows in the array you are using
int UP_BUTTON_CLICKED = 0;
int DOWN_BUTTON_CLICKED = 0;
//Define SElected values saved memory variables
String workOrder;
String Operator;
String product;
//LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
String lcdState="";
//Data Variables from Tulip
String arrayOperators[numRows]={
{"Operator 1"},
{"Operator 2"},
{"Operator 3"},
{"Operator 4"},
{"Operator 5"},
{"Operator 6"}
};
String arrayWorkOrders[numRows]={
{"Work Order 1"},
{"Work Order 2"},
{"Work Order 3"},
{"Work Order 4"}
};
String productInfo[numRows][3] = {
{"Cariboo", "E0040100888CDC03", "110"},
{"Daredevil", "E0040100888CD031", "136"},
{"Blazer", "E00401084431C4F0", "12"}
}; // Each row has 3 columns: product name and UID and count (this is what I will use: String productInfo[numRows][3];)
String arrayLines[numRows][3];
void pageOperators(); // Page-1
void pageWorkOrders(); // Page-2
void pageProducts(); // Page-3
void pageRunPress(); // Page-4
void pageNoDie(); // Page-5
void pageWorkOrderPress(); // Page-5
const int pageCount = 6;
int p;
void (*pages[pageCount])() = { pageOperators, pageWorkOrders, pageProducts, pageRunPress, pageNoDie, pageWorkOrderPress};
//int duration [pageCount] = { 1000, 1000, 1000, 2000 }; I dont' use this because I don't want the time to change
void updateCount(){
lcd.setCursor(8, 1);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(productCount);
}
void setup() {
Serial.begin(115200);
// Setup LCD
lcd.init();
lcd.backlight();
if(mode=="MultiDisplay"){
p = 0; //**This is setting what page to start on
populateArrayLines(arrayLines, numRows, p);
}else if(mode=="Stationary"){
p=3;
}
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(SELECT_BUTTON, INPUT_PULLUP); //**this was just input berfore
pinMode(SENSOR_PIN, INPUT_PULLUP);
pinMode(RFID_PIN, INPUT_PULLUP); //**This will need to be removed for the actual RFID sensor
pinMode(LED_CARD_DETECTED, OUTPUT);
pinMode(LED_CARD_NOT_DETECTED, OUTPUT);
pinMode(WIFI_PIN, OUTPUT);
}
void loop() {
// Inside the loop() function, handle button presses
if (digitalRead(UP_BUTTON) == LOW && UP_BUTTON_CLICKED == 0) {
item_selected = item_selected - 1;
UP_BUTTON_CLICKED = 1;
if (item_selected < 0) {
item_selected = NUM_ITEMS - 1;
}
Serial.print("Item Selected: ");
Serial.println(item_selected);
lcdUpdated = false;
}
else if (digitalRead(DOWN_BUTTON) == LOW && DOWN_BUTTON_CLICKED == 0) {
item_selected = item_selected + 1;
if (item_selected >= NUM_ITEMS) {
item_selected = 0;
}
DOWN_BUTTON_CLICKED = 1;
Serial.print("Item Selected: ");
Serial.println(item_selected);
lcdUpdated = false;
}
// Handle button release conditions
if (digitalRead(UP_BUTTON) == HIGH && UP_BUTTON_CLICKED == 1) {
UP_BUTTON_CLICKED = 0;
}
if (digitalRead(DOWN_BUTTON) == HIGH && DOWN_BUTTON_CLICKED == 1) {
DOWN_BUTTON_CLICKED = 0;
}
item_sel_previous= item_selected-1;
if (item_sel_previous<0) {item_sel_previous = NUM_ITEMS-1;}
item_sel_next = item_selected +1;
if (item_sel_next>=NUM_ITEMS){item_sel_next=0;}
static bool selectButtonPressed = false;
if (digitalRead(SELECT_BUTTON) == LOW && !selectButtonPressed) {
Serial.println("-----Select Button -----------------------------------");
selectButtonPressed = true;
if(p==0){ //Page 1
Operator = arrayLines[item_selected][0];
Serial.print("Operator: ");
Serial.println(Operator);
} else if(p==1){ //Page 2
workOrder = arrayLines[item_selected][0];
Serial.print("Work Order: ");
Serial.println(workOrder);
} else if(p==2){ //Page 3
product = arrayLines[item_selected][0];
Serial.print("Product: ");
Serial.println(product);
}
//Go To Next Page
p = p + 1;
if (p >= pageCount) {
p = 0;
}
populateArrayLines(arrayLines, numRows, p);
item_selected = 0;
Serial.println("------------------------------------------------------");
} else if (digitalRead(SELECT_BUTTON) == HIGH) {
selectButtonPressed = false;
}
//assignLines();
//See if an RFID Tag is Loaded
if(mode == "Stationary" && digitalRead(RFID_PIN)== LOW){
p=3;
if(cardUID==""){ //If the card has just been loaded and found then find the UID and product
findCardUID(); // This will be similar to printUID from other code
findCardProduct(); //This will look up the productInfo array and find the product name
Serial.print("Product: ");
Serial.println(cardProduct);
}
}else if(mode == "Stationary" && digitalRead(RFID_PIN)== HIGH){
p=4;
cardUID="";
}
// Update LCD screen when the p value changes
if (p != previousP) {
lcdUpdated = false;
previousP = p; // Update previousP to match the current value of p
}
if (!lcdUpdated) {
(*pages[p])(); // Call the page function
lcdUpdated = true; // Set the flag to true
}
//If we are in the run press page, read the IR sensor
if(p==3){
readIRSensor();
}
}
void pageOperators(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">");
lcd.setCursor(1, 0);
lcd.print(arrayLines[item_selected][1]);
lcd.setCursor(1, 1);
lcd.print(arrayLines[item_sel_next][1]);
}
void pageWorkOrders(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">");
lcd.setCursor(1, 0);
lcd.print(arrayLines[item_selected][1]);
lcd.setCursor(1, 1);
lcd.print(arrayLines[item_sel_next][1]);
}
void pageProducts(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">");
lcd.setCursor(1, 0);
lcd.print(arrayLines[item_selected][0]);
lcd.setCursor(1, 1);
lcd.print(arrayLines[item_sel_next][0]);
}
void pageRunPress(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(cardProduct);
lcd.setCursor(0, 1);
lcd.print("Count: ");
lcd.setCursor(8, 1);
lcd.print(productCount);
}
void pageNoDie(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Die Loaded");
}
void pageWorkOrderPress(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(workOrder);
lcd.setCursor(0, 1);
lcd.print(product);
lcd.setCursor(10, 1);
lcd.print(productCount);
}
void findCardUID(){
cardUID="E0040100888CDC03";
Serial.print("Card UID: ");
Serial.println(cardUID);
}
void findCardProduct(){
for (int i = 0; i < numRows; i++) {
if (productInfo[i][1] == cardUID) {
matchingRow = i;
break; // exit the loop if a match is found
}
}
// Check if a match was found
if (matchingRow != -1) {
// Retrieve the corresponding value in the first column
cardProduct = productInfo[matchingRow][0];
productCount = productInfo[matchingRow][2].toInt();
// Print the result
Serial.print("Matching row for cardUID ");
Serial.print(cardUID);
Serial.print(" found at index ");
Serial.print(matchingRow);
Serial.print(", Product: ");
Serial.print(cardProduct);
Serial.print(", Count: ");
Serial.println(productCount);
} else {
Serial.println("No matching row found for cardUID: " + cardUID);
lcd.setCursor(0, 0);
lcd.print("No Goggles");
}
}
void readIRSensor(){
if (digitalRead(SENSOR_PIN) == HIGH && !wasHigh) {
Serial.println("---------Read IR Sensor----------------------------------");
Serial.println("Sensor pin == high and was High");
wasHigh = true;
productCount++;
Serial.print("Product: ");
Serial.println(cardProduct);
Serial.print("Product UID: ");
Serial.println(cardUID);
Serial.print("Count: ");
Serial.println(productCount);
//Send data to tulip
updateCount(); //updates the LCD screen count
Serial.println("------------------------------------------------------");
} else if (digitalRead(SENSOR_PIN) == LOW) {
wasHigh = false;
}
}
void populateArrayLines(String arrayLines[][3], int numRows, int p) {
// Clear arrayLines
for (int i = 0; i < numRows; i++) {
arrayLines[i][0] = "";
arrayLines[i][1] = "";
arrayLines[i][2] = "";
}
// Check the value of p to decide which array to populate from
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < 3; j++) {
if (p == 0) {
arrayLines[i][j] = arrayOperators[i];
} else if (p == 1) {
arrayLines[i][j] = arrayWorkOrders[i];
} else if (p == 2) {
arrayLines[i][j] = productInfo[i][j];
}
}
}
// Calculate NUM_ITEMS
NUM_ITEMS = 0;
for (int i = 0; i < numRows; i++) {
if (!arrayLines[i][0].isEmpty()) {
NUM_ITEMS++;
}
}
// Serial print the matrix
Serial.println("Matrix arrayLines:");
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < 3; j++) {
Serial.print(arrayLines[i][j]);
Serial.print("\t");
}
Serial.println();
}
Serial.print("Number of Items: ");
Serial.println(NUM_ITEMS);
}