#include <LiquidCrystal_I2C.h>
#include <Button.h>
//Setting up the button pins
bool btnUpState, btnOkState, btnDnState;
Button btnUp(A1);
Button btnOk(A2);
Button btnDn(A3);
//Setting up the i2C LCD Screen
LiquidCrystal_I2C lcd(0x27, 16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
String menu[4] = {
"Auto Home",
"Automatic Cut",
"Manual Cut",
"Settings"
};
String autoCut[5] = {
"Long to Short",
"Long to A4",
"Long to 1/2",
"Long to 1/4"
};
int selectVal = 0;
void setup() {
// put your setup code here, to run once:
//Initialize Serial
Serial.begin(115200);
//Initialize Initialize
lcd.init();
lcd.backlight();
//Initialize all buttons
btnUp.begin();
btnOk.begin();
btnDn.begin();
}
void loop() {
// put your main code here, to run repeatedly:
buttonRead();
menuNav(selectVal);
Serial.println(btnUp.pressed());
}
void menuNav(int selectVal) {
static int x, y, z;
static int menuNoItems = (sizeof(menu) / sizeof(menu[0]) - 1);
static int autoCutNoItems = (sizeof(autoCut) / sizeof(autoCut[0]) - 1);
//Serial.println(selectVal);
//Main Menu
if(selectVal == 0) {
if(x <= menuNoItems && x >= 0) {
lcdPrint(menu[x], 0);
lcdPrint(menu[x+1], 1);
}
}
//Auto Cut
else if (selectVal == 20) {
if(y <= autoCutNoItems && y >= 0) {
lcdPrint("Please Select:", 0);
lcdPrint(autoCut[y-1], 1);
}
}
//Button selection
if(btnUpState) {
Serial.println(x);
//If @ main menu
if(selectVal == 0) {
//checks if value exceeds total menu
if(x > menuNoItems) {
x = 0;
}
else {
x+=1;
}
}
else if (selectVal == 20) {
//checks if value exceeds total menu
if(y > autoCutNoItems) {
y = 0;
}
else {
y+=1;
}
}
}
}
void buttonRead() {
if(btnUp.pressed()) {
btnUpState = true;
}
else if (btnUp.released()) {
btnUpState = false;
}
if(btnOk.pressed()) {
btnOkState = true;
}
else if (btnOk.released()) {
btnOkState = false;
}
if(btnDn.pressed()) {
btnDnState = true;
}
else if (btnDn.released()) {
btnDnState = false;
}
}
void lcdPrint(String text, int row) {
int textLength = text.length();
int startingCursor;
static String lastTextRow1, lastTextRow2;
//checks if value is an odd or even
if (textLength % 2 == 0) {
startingCursor = (16/2) - (textLength/2);
}
else {
startingCursor = (16/2) - ((textLength + 1)/2);
}
//checks if text is below 16 characters
if(textLength > 16) {
Serial.println("Text Exceeded 15 characters, chopping other texts...");
}
else {
//checks which row is it gonna be pasted
if (row == 0) {
if (text != lastTextRow1) {
//lcd.clear();
//clears the first the row,
lcd.setCursor(0, row);
lcd.print(" ");
//and then, starts to print
lcd.setCursor(startingCursor, row);
lcd.print(text);
//stores the last text to the string
lastTextRow1 = text;
}
}
else {
if (text != lastTextRow2) {
//lcd.clear();
//clears the first the row,
lcd.setCursor(0, row);
lcd.print(" ");
//and then, starts to print
lcd.setCursor(startingCursor, row);
lcd.print(text);
//stores the last text to the string
lastTextRow2 = text;
}
}
}
}