LCD Shield game menu

This is my first Arduino project and i want to play 2 games (pacman and hangman)on an 1602 LCD Keypad Shield for Arduino Uno and i want to have a menu with those 2 games as options to play. Right now i have the menu code but im not sure how to put the code for the menu and the 2 games together to make it work.
In the instructions for the LCD menu this was written on how to insert my own menu options : "You will need to write your own code in the body of method processMenuCommand(byte cmdId) to determine what must be done when a menu item is selected. The cmdId parameter is the Id you associate with a menu item in your menu Xml file.
Callback method getNavAction() handles user input, and callback method refreshMenuDisplay(byte refreshMode) renders the menu. "

this is from the tab where i put the menu items

#ifndef _sampleMenu_
#define _sampleMenu_
#include "MenuManager.h"
#include <avr/pgmspace.h>
enum sampleMenuCommandId
{
  mnuCmdBack = 0,
  mnuCmdTopItem1,
  mnuCmdTopItem2,
  mnuCmdSubItem1,
  mnuCmdSubItemA,
  mnuCmdSubItemB,
  mnuCmdResetToDefaults
};

PROGMEM const char sampleMenu_back[] = "Back";
PROGMEM const char sampleMenu_exit[] = "Exit";

PROGMEM const char sampleMenu_2_1_1[] = "1 Sub item A";
PROGMEM const char sampleMenu_2_1_2[] = "2 Sub item B";
PROGMEM const MenuItem sampleMenu_List_2_1[] = {{mnuCmdSubItemA, sampleMenu_2_1_1}, {mnuCmdSubItemB, sampleMenu_2_1_2}, {mnuCmdBack, sampleMenu_back}};

PROGMEM const char sampleMenu_2_1[] = "1 Sub item 1";
PROGMEM const MenuItem sampleMenu_List_2[] = {{mnuCmdSubItem1, sampleMenu_2_1, sampleMenu_List_2_1, menuCount(sampleMenu_List_2_1)}, {mnuCmdBack, sampleMenu_back}};

PROGMEM const char sampleMenu_1[] = "1 Pacman";
PROGMEM const char sampleMenu_2[] = "2 Hangman";
PROGMEM const MenuItem sampleMenu_Root[] = {{mnuCmdTopItem1, sampleMenu_1}, {mnuCmdTopItem2, sampleMenu_2, sampleMenu_List_2, menuCount(sampleMenu_List_2)}, {mnuCmdBack, sampleMenu_exit}};

#endif

this is the Menu code where the instructions say i can put my Pacman and Hangman code to make it work

#include <LiquidCrystal.h>
#include "LcdKeypad.h"
#include "MenuData.h"
#include <EEPROM.h>

enum AppModeValues
{
  APP_NORMAL_MODE,
  APP_MENU_MODE,
  APP_PROCESS_MENU_CMD
};

byte appMode = APP_NORMAL_MODE;

MenuManager Menu1(sampleMenu_Root, menuCount(sampleMenu_Root));

char strbuf[LCD_COLS + 1]; // one line of lcd display
byte btn;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void refreshMenuDisplay (byte refreshMode);
byte getNavAction();


void setup()
{
  //Serial.begin(9600);

  backLightOn();
  // set up the LCD's number of columns and rows:
  lcd.begin(LCD_COLS, LCD_ROWS);

  // fall in to menu mode by default.
  appMode = APP_MENU_MODE;
  refreshMenuDisplay(REFRESH_DESCEND);

  // Use soft PWM for backlight, as hardware PWM must be avoided for some LCD shields.
  // piggy back on to timer0, which is already set to approx 1khz.
  OCR0A = 0xAF;
  TIMSK0 |= _BV(OCIE0A);

  setBacklightBrightness(1);
}

SIGNAL(TIMER0_COMPA_vect)
{
  lcdBacklightISR();
}

void loop()
{
  btn = getButton();

  switch (appMode)
  {
    case APP_NORMAL_MODE :
      if (btn == BUTTON_UP_LONG_PRESSED)
      {
        appMode = APP_MENU_MODE;
        refreshMenuDisplay(REFRESH_DESCEND);
      }
      break;
    case APP_MENU_MODE :
      {
        byte menuMode = Menu1.handleNavigation(getNavAction, refreshMenuDisplay);

        if (menuMode == MENU_EXIT)
        {
          lcd.clear();
          lcd.print("Hold UP for menu");
          appMode = APP_NORMAL_MODE;
        }
        else if (menuMode == MENU_INVOKE_ITEM)
        {
          appMode = APP_PROCESS_MENU_CMD;

          // Indicate selected item.
          if (Menu1.getCurrentItemCmdId())
          {
            lcd.setCursor(0, 1);
            strbuf[0] = 0b01111110; // forward arrow representing input prompt.
            strbuf[1] = 0;
            lcd.print(strbuf);
          }
        }
        break;
      }
    case APP_PROCESS_MENU_CMD :
      {
        byte processingComplete = processMenuCommand(Menu1.getCurrentItemCmdId());

        if (processingComplete)
        {
          appMode = APP_MENU_MODE;
          // clear forward arrow
          lcd.setCursor(0, 1);
          strbuf[0] = ' '; // clear forward arrow
          strbuf[1] = 0;
          lcd.print(strbuf);
        }
        break;
      }
  }
}
//----------------------------------------------------------------------
// Addition or removal of menu items in MenuData.h will require this method
// to be modified accordingly.
byte processMenuCommand(byte cmdId)
{
  byte complete = false;  // set to true when menu command processing complete.
  if (btn == BUTTON_SELECT_PRESSED)
  {
    complete = true;
  }
  switch (mnuCmdTopItem1)
  {
      // TODO Process menu commands here:
      break;

  }
  return complete;
}
//----------------------------------------------------------------------
// Callback to convert button press to navigation action.
byte getNavAction()
{
  byte navAction = 0;
  byte currentItemHasChildren = Menu1.currentItemHasChildren();

  if (btn == BUTTON_UP_PRESSED || btn == BUTTON_UP_LONG_PRESSED) navAction = MENU_ITEM_PREV;
  else if (btn == BUTTON_DOWN_PRESSED || btn == BUTTON_DOWN_LONG_PRESSED) navAction = MENU_ITEM_NEXT;
  else if (btn == BUTTON_SELECT_PRESSED || (btn == BUTTON_RIGHT_PRESSED && currentItemHasChildren)) navAction = MENU_ITEM_SELECT;
  //else if (btn == BUTTON_LEFT_PRESSED) navAction = MENU_BACK;
  return navAction;
}
//----------------------------------------------------------------------
const char EmptyStr[] = "";

// Callback to refresh display during menu navigation, using parameter of type enum DisplayRefreshMode.
void refreshMenuDisplay (byte refreshMode)
{
  char nameBuf[LCD_COLS + 1];

  lcd.setCursor(0, 0);
  if (Menu1.currentItemHasChildren())
  {
    rpad(strbuf, Menu1.getCurrentItemName(nameBuf));
    strbuf[LCD_COLS - 1] = 0b01111110;          // Display forward arrow if this menu item has children.
    lcd.print(strbuf);
    lcd.setCursor(0, 1);
    lcd.print(rpad(strbuf, EmptyStr));          // Clear config value in display
  }
  else
  {
    byte cmdId;
    rpad(strbuf, Menu1.getCurrentItemName(nameBuf));

    if ((cmdId = Menu1.getCurrentItemCmdId()) == 0)
    {
      strbuf[LCD_COLS - 1] = 0b01111111;        // Display back arrow if this menu item ascends to parent.
      lcd.print(strbuf);
      lcd.setCursor(0, 1);
      lcd.print(rpad(strbuf, EmptyStr));        // Clear config value in display.
    }
    else
    {
      lcd.print(strbuf);
      lcd.setCursor(0, 1);
      lcd.print(" ");

      // TODO Display config value.
    }
  }
}

Hangman txt.txt (7.6 KB)

Pacman.txt (8.16 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.