Go Down

Topic: <MenuBackend.h> Menu control with I2C pushbuttons (Read 1 time) previous topic - next topic



I need some help to remove the ARDUINO PINS and intergrade my I2C pushbuttons in the Menu.

this the the working Menu with ARDUINO PINS. get it from http://playground.arduino.cc/Code/Menu
Code: [Select]
#include <MenuBackend.h>
#include <LiquidCrystal.h>

const int buttonPinLeft = 8;
const int buttonPinRight = 9;
const int buttonPinEsc = 10;
const int buttonPinEnter = 11;

int lastButtonPushed = 0;

int lastButtonEnterState = LOW;   // the previous reading from the "Enter" input pin
int lastButtonEscState = LOW;
int lastButtonLeftState = LOW;
int lastButtonRightState = LOW;

long lastEnterDebounceTime = 0;  // the last time the output pin was toggled
long lastEscDebounceTime = 0;
long lastLeftDebounceTime = 0;
long lastRightDebounceTime = 0;
long debounceDelay = 500;    // the debounce time

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
   MenuItem menu1Item1 = MenuItem("Item1");
     MenuItem menuItem1SubItem1 = MenuItem("Item1SubItem1");
     MenuItem menuItem1SubItem2 = MenuItem("Item1SubItem2");
   MenuItem menu1Item2 = MenuItem("Item2");
     MenuItem menuItem2SubItem1 = MenuItem("Item2SubItem1");
     MenuItem menuItem2SubItem2 = MenuItem("Item2SubItem2");
     MenuItem menuItem2SubItem3 = MenuItem("Item2SubItem3");
   MenuItem menu1Item3 = MenuItem("Item3");

void setup()
 pinMode(buttonPinLeft, INPUT);
 pinMode(buttonPinRight, INPUT);
 pinMode(buttonPinEnter, INPUT);
 pinMode(buttonPinEsc, INPUT);
 lcd.begin(16, 2);

 //configure menu

}  // setup()...

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,1); //set the start position for lcd printing to the second row
     lcd.print("Main Menu       ");
 }else if(newMenuItem.getName()=="Item1"){
     lcd.print("Scannen         ");
 }else if(newMenuItem.getName()=="Item1SubItem1"){
 }else if(newMenuItem.getName()=="Item1SubItem2"){
     lcd.print("Item1SubItem2   ");
 }else if(newMenuItem.getName()=="Item2"){
     lcd.print("Freq band       ");
 }else if(newMenuItem.getName()=="Item2SubItem1"){
     lcd.print("Item2SubItem1   ");
 }else if(newMenuItem.getName()=="Item2SubItem2"){
     lcd.print("Item2SubItem2   ");
 }else if(newMenuItem.getName()=="Item2SubItem3"){
     lcd.print("Item2SubItem3   ");
 }else if(newMenuItem.getName()=="Item3"){
     lcd.print("Freq invoer      ");

void menuUsed(MenuUseEvent used){
 delay(3000);  //delay to allow message reading
 menu.toRoot();  //back to Main

void  readButtons(){  //read buttons status
 int reading;
 int buttonEnterState=LOW;             // the current reading from the Enter input pin
 int buttonEscState=LOW;             // the current reading from the input pin
 int buttonLeftState=LOW;             // the current reading from the input pin
 int buttonRightState=LOW;             // the current reading from the input pin

 //Enter button
                 reading = digitalRead(buttonPinEnter);

                 if (reading != lastButtonEnterState) {

                   lastEnterDebounceTime = millis();
                 if ((millis() - lastEnterDebounceTime) > debounceDelay) {


                 lastButtonEnterState = reading;
                 reading = digitalRead(buttonPinEsc);

                 if (reading != lastButtonEscState) {

                   lastEscDebounceTime = millis();
                 if ((millis() - lastEscDebounceTime) > debounceDelay) {

                   buttonEscState = reading;
                 lastButtonEscState = reading;                   

                 reading = digitalRead(buttonPinRight);

                 if (reading != lastButtonRightState) {
                   // reset the debouncing timer
                   lastRightDebounceTime = millis();
                 if ((millis() - lastRightDebounceTime) > debounceDelay) {

                   buttonRightState = reading;
                  lastRightDebounceTime =millis();
                 lastButtonRightState = reading;                  
                 if (reading != lastButtonLeftState) {

                   lastLeftDebounceTime = millis();
                 if ((millis() - lastLeftDebounceTime) > debounceDelay) {

                   buttonLeftState = reading;
                 lastButtonLeftState = reading;  

                 if (buttonEnterState==HIGH){

                 }else if(buttonEscState==HIGH){

                 }else if(buttonRightState==HIGH){

                 }else if(buttonLeftState==HIGH){


void navigateMenus() {
 MenuItem currentMenu=menu.getCurrent();
 switch (lastButtonPushed){
   case buttonPinEnter:
     if(!(currentMenu.moveDown())){  //if the current menu has a child and has been pressed enter then menu navigate to item below
     }else{  //otherwise, if menu has no child and has been pressed enter the current menu is used
   case buttonPinEsc:
     menu.toRoot();  //back to main
   case buttonPinRight:
   case buttonPinLeft:
 lastButtonPushed=0; //reset the lastButtonPushed variable

This is the I2C pushbutton control i want to use in the menu, instead of the ARDUINO PINS.
Code: [Select]
 Wire.requestFrom(PCF1_BUTTON, 1);
 if(Wire.available())                              // If the request is available from the PCF1_BUTTON
   PCF_1=Wire.read();                              // Receive the data from the PCF1_BUTTON
   if(PCF_1<255)                                   // If the button data is less than 255
   if (PCF_1==254) { buttonLeft; };
   if (PCF_1==253) { buttonRight; };
   if (PCF_1==251) { buttonEsc; };
   if (PCF_1==247) { buttonEnter; };


I wil appreciatie any help

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131