#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <avr/wdt.h> // Include the library for resetting
#define BUTTON_PIN A2 // Define the pin for the push button
#define BUTTON_PIN1 13
LiquidCrystal_I2C lcd(0x27, 16, 2);
int i = 0;
bool askingShopOrder = false; // Flag to indicate if we are asking about SHOP ORDER
bool askingItemNumber = false; // Flag to indicate if we are asking about ITEM NUMBER
bool askingEmployeeNumber = false; // Flag to indicate if we are asking about the amount of people
bool buttonPressed = false; // Flag to indicate if the button is pressed
bool setupComplete = false; // Flag to indicate if the setup is completed
String lcdContent = ""; // Variable to store LCD content
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.init();
lcd.init();
lcd.backlight();
lcd.setBacklight(HIGH);
lcd.setCursor(5, 0);
lcd.print("PUSH A");
const char number_of_rows = 4;
const char number_of_columns = 4;
char row_pins[number_of_rows] = {9, 8, 7, 6};
char column_pins[number_of_columns] = {5, 4, 3, 2};
char key_array[number_of_rows][number_of_columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad k = Keypad(makeKeymap(key_array), row_pins, column_pins, number_of_rows, number_of_columns);
char key_pressed;
while (!setupComplete) {
key_pressed = k.getKey();
if (key_pressed == 'A') { // Check if 'A' key is pressed and was not previously pressed and setup is not completed
clearLCD(); // Clear LCD when 'A' key is pressed
lcd.setCursor(0,0);
lcd.print("ENTERING SETUP");
delay(2000);
Serial.println("SETUP STARTED");
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("SHOP ORDER");
askingShopOrder = true; // Set flag to indicate we are asking SHOP ORDER
buttonPressed = true; // Set buttonPressed to true
delay(1000); // Delay for stability
}
if (key_pressed != 'A') { // Check if 'A' key is released
buttonPressed = false; // Set buttonPressed to false
}
if (askingShopOrder && key_pressed == '#' ) { // Check if '#' button is pressed for SHOP ORDER after setup completion
Serial.println("SHOP ORDER: " + lcdContent); // Print SHOP ORDER to serial monitor
clearLCD();
lcd.setCursor(0, 0);
lcd.print("ENTER ITEM"); // Prompt for ITEM NUMBER
askingShopOrder = false;
askingItemNumber = true; // Set flag to indicate we are asking ITEM NUMBER
} else if (askingItemNumber && key_pressed == '#' ) { // Check if '#' button is pressed for ITEM NUMBER after setup completion
Serial.println("ITEM NUMBER: " + lcdContent); // Print ITEM NUMBER to serial monitor
clearLCD();
lcd.setCursor(0, 0);
lcd.print("Employee Number"); // Prompt for Amount of People
askingItemNumber = false;
askingEmployeeNumber = true; // Set flag to indicate we are asking Amount of People
} else if (askingEmployeeNumber && key_pressed == '#' ) { // Check if '#' button is pressed for Amount of People after setup completion
Serial.println("Employee Number:" + lcdContent); // Print Amount of People to serial monitor
clearLCD();
lcd.setCursor(2, 1);
lcd.print("SETUP STARTED");
delay(10000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Press B To");
lcd.setCursor(3,1);
lcd.print("Clock In");
// Optional: print a message to indicate all information has been received
} else if (key_pressed == '*') { // Check if '*' button is pressed for backspace after setup completion
backspace(); // Call backspace function
} else if (key_pressed) { // If any other key is pressed after setup completion
updateLCDContent(key_pressed); // Update LCD content with key pressed
}
if (key_pressed == 'B') {
clearLCD(); // Clear the LCD
Serial.print("JOB START");
lcd.setCursor(0,0);
lcd.print("CLOCKED IN");
delay(3000);
//wdt_enable(WDTO_15MS); // Enable watchdog timer (resets the Arduino)
// while (1);
setupComplete = true;
//return; // Exit loop to restart setup
}
}
}
void updateLCDContent(char key) {
if (i < 16) {
lcd.setCursor(i, 1);
lcd.print(key);
lcdContent += key; // Add the pressed key to lcdContent
i++;
}
}
void clearLCD() {
lcd.clear();
lcd.setCursor(0, 1); // Set cursor to the beginning of the second row
i = 0;
lcdContent = ""; // Clear lcdContent variable
}
void backspace() {
if (i > 0) {
i--; // Move cursor back
lcd.setCursor(i, 1);
lcd.print(" "); // Clear character on LCD
lcd.setCursor(i, 1);
lcdContent.remove(i, 1); // Remove last character from lcdContent
}
}
void loop() {
const char number_of_rows = 3;
const char number_of_columns = 3;
char row_pins[number_of_rows] = {9, 8, 7};
char column_pins[number_of_columns] = {5, 4, 3};
char key_array[number_of_rows][number_of_columns] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
Keypad k = Keypad(makeKeymap(key_array), row_pins, column_pins, number_of_rows, number_of_columns);
char key_pressed = k.getKey();
// Check if 'D' key is pressed to reset setup
if (key_pressed == '1') {
digitalWrite(BUTTON_PIN1, HIGH);
Serial.println(" ");
delay(500);
}
else if(key_pressed == '2') {
digitalWrite(BUTTON_PIN1, LOW);
Serial.println(" ");
delay(500);
}
}