Thanks all for contributions.
I started over and I think I have a reasonable template to use for menu selecting with an encoder / LCD.
The only thing that's not quite working right yet is when you switch back over to run mode (pin 11), it doesn't seem to be running the clearScreen(); inside loop(); but that's relatively minor at this point.
Below is the code, incase anyone can benefit from it:
/*
* Arduino pins
* 0 - (FREE)
* 1 - Rotary Encoder CLK
* 2 - Rotary Encoder DT
* 3 - Rotary Encoder Switch
* 4 - (FREE)
* 5 - (FREE)
* 6 - (FREE)
* 7 - (FREE)
* 8 - (FREE)
* 9 - (FREE)
* 10- (FREE)
* 11- Two Position Switch to select Run Mode or Setup
* 12- (FREE)
* 13- (FREE)
* A0- (FREE)
* A1- (FREE)
* A2- (FREE)
* A3- (FREE)
* A4- (FREE)
* A5- (FREE)
* SCL - I2C / LCD SCL line
* SDA - I2C / LCD SDA line
*
*/
#include <Wire.h> //library for LCD / I2C
#include <LiquidCrystal_I2C.h> //library for LCD / I2c
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2); //I2C ADDRESS
#define CLK 1 //define pin 1 as CLK of rotary encoder
#define DT 2 //define pin 2 as DT of rotary encoder
#define SW 3 //define pin 3 as the select on rotary encoder
#define setupPin 11 //switch to select between setup and run mode
int counter = 0; //variable for counting the rotary encoder movement
int menu = 0; //vairable for redistributing the counter clicks (2 per detent) into menu items
int currentStateCLK; // records CLK current position on rotary encoder
int lastStateCLK; // records CLK last position on rotary encoder
unsigned long lastButtonPress = 0; //records button press on rotary encoder
bool clearscrn = false; // switch to clear screen only once in loop()
int numScrn = 4; // number of setup screens to use
void setup() {
lcd.begin(); //initalize LCD display
lcd.backlight(); //turn on LCD backlight
lcd.clear(); //clear the LCD for a fresh start
lcd.setCursor(1,0); // set the cursor to position (2,0)
lcd.print("menu two V0.1"); // print out start up message to LCD
delay(3000);
lcd.clear();
pinMode(CLK,INPUT); // setup rotary encoder CLK as an input
pinMode(DT,INPUT); // setup rotary encoder DT as an input
pinMode(SW,INPUT_PULLUP); // setup rotary encoder SW as an input
pinMode(setupPin, INPUT_PULLUP); // setup selector switch
lastStateCLK = digitalRead(CLK); // read inital state of CLK, assign to previousStateCLK
}
void menuSelect(){
currentStateCLK = digitalRead(CLK); // read current state of CLK on rotary encoder
// the following compares last and current state of CLK to see if pulse occured
if (currentStateCLK != lastStateCLK){
// if the DT state is different than CLK state then CCW rotation, so decrement
if (digitalRead(DT) != currentStateCLK) {
counter ++;
menu = counter/2;
if(menu > numScrn){ //this rolls back over to zero if counter gets over number of screens
counter = 0;
}
} else {
// encoder is rotating CW so incriment
counter --;
menu = counter/2;
if (menu <0){ //this rolls back over to the max number of screens if counter gets below zero
counter = numScrn;
}
}
// the following is to display the encoder state on the LCD screen
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0); // set cursor to beginning
lcd.print("Select Operation"); // print out rotation on line 1
lcd.setCursor(1,1); // set cursor to second row
switch(menu){
case 0:
lcd.print("operation 1");
break;
case 1:
lcd.print("operation 2");
break;
case 2:
lcd.print("operation 3");
break;
case 3:
lcd.print("operation 4");
break;
case 4:
lcd.print("operation 5");
break;
}
}
lastStateCLK = currentStateCLK; //remember last CLK state
int btnState = digitalRead(SW); //read button state
//if btnState LOW, then button is pressed
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
switch(menu){
case 0:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("running op 1...");
//run menu 1 program here
break;
case 1:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("running op 2...");
//run menu 2 program here
break;
case 2:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("running op 3...");
//run menu 3 program here
break;
case 3:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("running op 4...");
//run menu 4 program here
break;
case 4:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("running op 5...");
//run menu 5 program here
break;
}
}
lastButtonPress = millis(); // remember last button press
}
clearscrn = false;
}
void clearScreen(){
lcd.clear();
clearscrn = true;
}
void loop() {
if (digitalRead(setupPin) == HIGH){
menuSelect();
}
if (digitalRead(setupPin) == LOW){
if (clearscrn = false){
clearScreen();
}
//run mode
lcd.setCursor(2,0);
lcd.print("run mode"); // trouble shooting remove this and insert the run mode program call
}
}