I have a working menu using push buttons but now want to use a Rotary encoder instead.
what do I have to do to use the encoder instead of the push buttons to select menu options.
here is the reduced MENU code sketch due to message limitation.
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> //this library is included in the Arduino IDE
const int buttonPinLeft = 44; // pin for the Up button
const int buttonPinRight = 46; // pin for the Down button
const int buttonPinEnter = 45; // pin for the Enter button
const int buttonPinEsc = 42; // pin for the Esc button
int lastButtonPushed = 0;
int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
int lastButtonEscState = LOW; // the previous reading from the Esc input pin
int lastButtonLeftState = LOW; // the previous reading from the Left input pin
int lastButtonRightState = LOW; // the previous reading from the Right input pin
long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long lastEscDebounceTime = 0; // the last time the output pin was toggled
long lastLeftDebounceTime = 0; // the last time the output pin was toggled
long lastRightDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 100; // the debounce time
LiquidCrystal lcd(7,8,9,10,11,12);
void setup()
{
pinMode(buttonPinLeft, INPUT);
pinMode(buttonPinRight, INPUT);
pinMode(buttonPinEnter, INPUT);
pinMode(buttonPinEsc, INPUT);
lcd.begin(16, 2);
}
void loop()
{
readButtons(); //I splitted button reading and navigation in two procedures because
navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings)
} //loop()...
void menuChanged(MenuChangeEvent changed){
MenuItem newMenuItem=changed.to; //get the destination menu
lcd.setCursor(0,0); //set the start position for lcd printing to the second row
if(newMenuItem.getName()==menu.getRoot())
{ //this code will change to have time and date displayed
lcd.print("Main Menu ");
}
//--------------------------------------------------------------menu lcd-----------------------------------------
else if(newMenuItem.getName()=="menuLcd"){
lcd.clear();
lcd.print("Lcd");
}
else if(newMenuItem.getName()=="menuItemOn"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("On");
}
else if(newMenuItem.getName()=="menuItemOff"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Off");
}
//--------------------------------------------------------------------menu set clock-----------------------------------
else if(newMenuItem.getName()=="menuSetClock"){
lcd.clear();
lcd.print("Set Clock");
}
else if(newMenuItem.getName()=="menuItemHr"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Hr");
}
else if(newMenuItem.getName()=="menuItemMin"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Min");
}
else if(newMenuItem.getName()=="menuItemSec"){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Sec");
}
else if(newMenuItem.getName()=="menuItemConfirm"){
lcd.clear();
lcd.print("Confirmed");
}
//--------------------------------------------------menu set date-------------------------------------------------------------------------
else if(newMenuItem.getName()=="menuSetDate"){
lcd.clear();
lcd.print("Set Date");
}
else if(newMenuItem.getName()=="menuItemDay"){
lcd.clear();
lcd.print("Set Day");
}
else if(newMenuItem.getName()=="menuItemMonth"){
lcd.clear();
lcd.print("Set Month");
}
else if(newMenuItem.getName()=="menuItemYear"){
lcd.clear();
lcd.print("Set Year");
}
//------------------------------------------------------------menu set phone number--------------------------------------------------------
else if(newMenuItem.getName()=="menuSetPhone"){
lcd.clear();
lcd.print("Set Phone");
}
//---------------------------------------------------------------menu set pin number---------------------------------------------------------
else if(newMenuItem.getName()=="menuSetPin"){
lcd.clear();
lcd.print("Set Pin");
}
//---------------------------------------------------------menu set sms---------------------------------------------------------------------
else if(newMenuItem.getName()=="menuSms"){
lcd.clear();
lcd.print("Sms ");
}
else if(newMenuItem.getName()=="menuItemSigStr"){
lcd.clear();
lcd.print("Signal Stre");
}
else if(newMenuItem.getName()=="menuItemBalance"){
lcd.clear();
lcd.print("Balance");
}
else if(newMenuItem.getName()=="menuItemTest"){
lcd.clear();
lcd.print("Test Sms");
}
//---------------------------------------------------------------------menu exit to time----------------------------------------------------------------
else if(newMenuItem.getName()=="menuExit"){
lcd.clear();
lcd.print("Exit");
}
}
void menuUsed(MenuUseEvent used){ //**************if you have a function call it here***********************
if( used.item == menuItemOn ){
lcd.display();
//digitalWrite(10, HIGH);
//delay(3000);
//digitalWrite(10, LOW);
lcd.clear();
menu.toRoot();
}
if ( used.item == menuItemOff ){
lcd.noDisplay();//your code here
menu.toRoot();
}
//etc.
}
void navigateMenus() {
MenuItem currentMenu=menu.getCurrent();
switch (lastButtonPushed){
case buttonPinEnter: // enter pin
if(!(currentMenu.moveDown())){ //if the current menu has a child and enter has been pressed then navigate menu to item below
menu.use();
}
else{ //otherwise, if menu has no child and enter has been pressed---- the current menu is used
menu.moveDown();
}
break;
case buttonPinEsc:
menu.toRoot(); //back to main
break;
case buttonPinRight:
menu.moveRight();
break;
case buttonPinLeft:
menu.moveLeft();
break;
}
lastButtonPushed=0; //reset the lastButtonPushed variable
}
I have a separate encoder sketch that works in serial monitor that shows each indent of the encoder 1,2,3,4 "you get the idea".
Here is the encoder sketch.
#define EncoderPinA 2
#define EncoderPinB 3
#include "pins_arduino.h"
byte portA;
byte portB;
byte bit_maskA;
byte bit_maskB;
volatile byte *registerA;
volatile byte *registerB;
volatile static int numberA;
volatile int* attached_val;
void setup()
{
Serial.begin(9600);
pinMode(EncoderPinA, INPUT);
pinMode(EncoderPinB, INPUT);
digitalWrite(EncoderPinA, HIGH);
digitalWrite(EncoderPinB, HIGH);
portA=digitalPinToPort(EncoderPinA);
portB=digitalPinToPort(EncoderPinB);
bit_maskA = digitalPinToBitMask(EncoderPinA);
bit_maskB = digitalPinToBitMask(EncoderPinB);
registerA = portInputRegister(portA);
registerB = portInputRegister(portB);
attached_val = &numberA;
attachInterrupt(0, doEncoderA, FALLING); // for some reason the new mouser encoders only work on A falling and b rising The other ones don't read fast enough
//and always count the same way
}
void loop()
{
Serial.print("numberA = ");
Serial.println(numberA);
delay(500);
}
void doEncoderA()
{
((((*registerA) & bit_maskA) && ((*registerB) & bit_maskB)) || ((!((*registerA) & bit_maskA)) && (!((*registerB) & bit_maskB))))? (*attached_val)++ :
(*attached_val)--;
}
hope I have included enough code for you to help.