// https://www.baldengineer.com/arduino-f-macro.html
// wrap lcd & serial in F() for
//#include <arduino.h> for platformIO
#include <Wire.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
#include <ezButton.h> // Include the ezButton library
// Create an instance of the LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define button pins
ezButton incSpeedButton(32); // C spindle increase speed
ezButton decSpeedButton(35); // C spindle decrease speed
// define limit switch pin
const int limitSwitchButton = 5;
bool limitSwitchButtonState = false; // toggling limit switch true / false
int limitSwitchButtonCount = 0;
unsigned long limitSwitchButton_prev_time = 0; // debounce reference value for last press on limitswitch
// limit switch to be placed in IRAM area instead of flash for fast execution
void IRAM_ATTR ISRlimitSwitchButton() {
if( millis() - limitSwitchButton_prev_time > 75 ) { // debounce function
limitSwitchButtonCount++; // count each limit switch press
//limitSwitchButtonState = true; // toggel true false
Serial.printf("Button has been pressed %u times\n", limitSwitchButtonCount);
limitSwitchButton_prev_time = millis(); // reset value for last button press
}
}
// Define LCD timeout values
const unsigned long MENU_TIMEOUT= 1500; // Menu time out value WAS 3 seconds
unsigned long MENU_lastButtonPressMillis; // Menu reference value
const unsigned long LCD_refreshrate = 500; // LCD refresh value WAS 100 mS
unsigned long LCD_lastButtonPressMillis; // LCD reference value
// Define menu variables start at main menu = 0 , Submenu1 = 1
int menuIndex = 0;
// pre declarations
void MenuLogic(); // button action sets menuIndex to 1 or 0 , with timeout and refresh rate
void showMenu(); // writes to LCD what is set in menuLogic
void setup() {
Serial.begin(115200);
Serial.println(F("\nVINCES Lathe\n"));
incSpeedButton.setDebounceTime(50); //was 50
decSpeedButton.setDebounceTime(50);
// limit switch button
pinMode(limitSwitchButton, INPUT_PULLUP);
attachInterrupt(limitSwitchButton, ISRlimitSwitchButton, FALLING);
// LCD initialization
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.println(F(" VINCES Lathe"));
lcd.setCursor(0, 1);
lcd.println(F("Ready to Rumble"));
delay(2000);
lcd.clear();
}
void loop() {
// must call loop to check if button is pressed
incSpeedButton.loop();
decSpeedButton.loop();
// Logic to enter menu after button press and time out back to main menu,
// calls showMenu with what to update
MenuLogic();
// // limit swtich check , moving this out of void
// if (limitSwitchButtonState) {
// Serial.printf("Button has been pressed %u times\n", limitSwitchButtonCount);
// limitSwitchButtonState = false;
// }
}
void MenuLogic(){
// when button is pressed enter submenu while menu time out is bigger
// than millis else return to main menu
while (millis() - MENU_lastButtonPressMillis < MENU_TIMEOUT) {
showMenu(); // show one of two menus choosen by menuIndex 0 or 1
// Serial.println(F("\n Inside submenu 1\n"));
// Serial.println((millis()));
// Serial.println((MENU_lastButtonPressMillis));
}
if (incSpeedButton.isPressed() || decSpeedButton.isPressed() ) {
menuIndex = 1;
MENU_lastButtonPressMillis = millis(); // start time out function
// Serial.println(F("\n Enter submenu 1\n"));
}
else {
menuIndex = 0;
showMenu();
//Serial.println(F("\n Return to Main menu 0\n"));
}
}
// Function to display the menus based on the menuIndex variable also refresh rate
// to make LCD smooth & conserv CPU
void showMenu() {
// enter in here if millis is larger than LCD_refreshrate
while (millis() - LCD_lastButtonPressMillis > LCD_refreshrate ){
if (menuIndex == 0) {
//lcd.clear();
lcd.setCursor(0, 0); // only update whats needed to conserve CPU
lcd.print(F("Main Menu"));
lcd.setCursor(0, 1); // only update whats needed to conserve CPU
lcd.print((LCD_lastButtonPressMillis));
}
else if (menuIndex == 1) {
//lcd.clear();
lcd.setCursor(0, 0); // only update whats needed to conserve CPU
lcd.print(F("Submenu 1"));
lcd.setCursor(0, 1); // only update whats needed to conserve CPU
lcd.print((LCD_lastButtonPressMillis));
}
LCD_lastButtonPressMillis = millis(); // reset value when moving out of while loop
}
}