Pages: [1]   Go Down
Author Topic: <MenuBackend.h> Menu control with I2C pushbuttons  (Read 553 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
I just want to learn more :)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,

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:
#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
  menu.getRoot().add(menu1Item1);
  menu1Item1.addRight(menu1Item2).addRight(menu1Item3);
  menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2);
  menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2).addRight(menuItem2SubItem3);
  menu.toRoot();
  lcd.setCursor(0,0);  
  lcd.print("message");

}  // 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
  
  if(newMenuItem.getName()==menu.getRoot()){
      lcd.print("Main Menu       ");
  }else if(newMenuItem.getName()=="Item1"){
      lcd.print("Scannen         ");
  }else if(newMenuItem.getName()=="Item1SubItem1"){
      lcd.print("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){
  lcd.setCursor(0,0);  
  lcd.print("message");
  lcd.setCursor(0,1);
  lcd.print(used.item.getName());
  delay(3000);  //delay to allow message reading
  lcd.setCursor(0,0);  
  lcd.print("message");
  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) {

                    buttonEnterState=reading;
                    lastEnterDebounceTime=millis();
                  }

                  lastButtonEnterState = reading;
                  
                  reading = digitalRead(buttonPinEsc);

                  if (reading != lastButtonEscState) {

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

                    buttonEscState = reading;
                    lastEscDebounceTime=millis();
                  }
                  
                  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;
                    lastLeftDebounceTime=millis();;
                  }
                  
                  lastButtonLeftState = reading;  

                  if (buttonEnterState==HIGH){
                    lastButtonPushed=buttonPinEnter;

                  }else if(buttonEscState==HIGH){
                    lastButtonPushed=buttonPinEsc;

                  }else if(buttonRightState==HIGH){
                    lastButtonPushed=buttonPinRight;

                  }else if(buttonLeftState==HIGH){
                    lastButtonPushed=buttonPinLeft;

                  }else{
                    lastButtonPushed=0;
                  }                  
}

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
        menu.use();
      }else{  //otherwise, if menu has no child and has been pressed enter 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
}

This is the I2C pushbutton control i want to use in the menu, instead of the ARDUINO PINS.
Code:
{
  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
Logged

Pages: [1]   Go Up
Jump to: