Thanks all for the suggestions. I tried tinkering and still getting stuck.
Using the if or the while (tried both) seems to jump to the menuselect() portion. But it seems to be getting stuck there. The LCD readout in the code below displays "rotate 0" and the encoder nor the button seem to have any effect.
I've tried putting the rotate() and the buttonPressed() in several places, as it seems that neither is being called no matter where I put them. I had both working before when it was just the two, but when trying to integrate into a larger program, they seem to be getting ignored. That tells me it has something to do with where I put them or how I'm calling them.
If anyone has any insight, I'd greatly appreciate it.
/*
* This is to be the basis for a menu select for a LCD screen and a rotary encoder with button select
*
* Arduino pins
* 0 - (FREE)
* 1 - Rotary Encoder CLK
* 2 - Rotary Encoder DT
* 3 - Rotary Encoder SW
* 4 - (FREE)
* 5 - (FREE)
* 6 - (FREE)
* 7 - (FREE)
* 8 - (FREE)
* 9 - (FREE)
* 10- (FREE)
* 11- 2 Position Selector Switch
* 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(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 switch on rotary encoder
#define setupPin 11 //switch to select between setup mode and run mode
int ButtonCounter = 0; //counts the button clicks
int RotateCounter = 0; //counts the rotation clicks
bool rotated = true; //state of the rotation
bool ButtonPressed = false; //state of the button
//variables to track the encoder:
int CLKNow;
int CLKPrevious;
int DTNow;
int DTPrevious;
// Timers for tracking encoder clicks:
float TimeNow1;
float TimeNow2;
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 test V0.1"); // print out start up message to LCD
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);
//Setup states for the encoder:
CLKPrevious = digitalRead(CLK);
DTPrevious = digitalRead(DT);
attachInterrupt(digitalPinToInterrupt(CLK), rotate, CHANGE);
attachInterrupt(digitalPinToInterrupt(SW), buttonPressed, FALLING);
TimeNow1 = millis(); //Start timer 1
}
void buttonPressed(){
//This timer is an attempt at a software debounce
TimeNow2 = millis();
if((TimeNow2 - TimeNow1 > 500) && (digitalRead(SW) == HIGH)){
ButtonPressed = true;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("button press");
}
TimeNow1 = millis(); //reset timer for the next press
}
void rotate(){
CLKNow = digitalRead(CLK); //Read the state of the CLK pin
// If last and current state of CLK are different, then a pulse occurred
if (CLKNow != CLKPrevious && CLKNow == 1)
{
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so increase
if (digitalRead(CLK) != CLKNow){
RotateCounter++;
if(RotateCounter > 4){
RotateCounter = 0;
}
}
else{
RotateCounter--;
if(RotateCounter < 0){
RotateCounter = 4;
}
}
}
CLKPrevious = CLKNow; // Store last CLK state
rotated = true;
lcd.setCursor(0,1);
lcd.print("rotate ");
lcd.print(RotateCounter);
}
void menuSelect(){
rotate();
buttonPressed();
if(ButtonPressed == true)
{
switch(RotateCounter)
{
case 0:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("menu 0");
break;
case 1:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("menu 1");
break;
case 2:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("menu 2");
break;
case 3:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("menu 3");
break;
case 4:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("menu 4");
break;
}
}
ButtonPressed = false; //reset this variable
}
void loop() {
//menu select programming:
if (digitalRead(setupPin) == HIGH){
menuSelect();
while (digitalRead(setupPin) == HIGH);
}
else {
//run mode program
}
}