Code to display the menu and its interactions.

 

#include <Wire.h>
#include <LiquidCrystal.h>
#include "LCDCommands.h"

/*
 * Pins used:
 * ==========
 *  0,1 : USB or BT Serial
 *  3 :   LCDBacklight
 *  8, 9, 4, 5, 6, 7: LCD Display         
 *  A0 :  LCD Buttons Volatge
 */

LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );


void ButtonsInit();
void GetKeyboard();
void BL_On();
void BL_Off();

bool commandMode;

void setup()
{
  commandMode = false;
  ButtonsInit();
  lcd.begin(16, 2);
  lcd.print("hello, world!");
  Serial.begin(9600);
}



void loop()
{
  byte byt = Serial.read();
  char ch;
  while (byt == 255) //-1 = FF
  {
    GetKeyboard();
    byt = Serial.read();
  }
  ch = byt;
  if (commandMode)
  {
    switch (ch)
    {
      case CMD_RESET_CHAR:
        //Reset the display to 16 chars x 2 rows
        lcd.begin(16, 2);
        break;
      case CMD_CLEAR_CHAR:
        //Clear the LCD screen
        //and positions the cursor in the upper-left corner
        lcd.clear();
        break;
      case CMD_HOME_CHAR:
        //Position the cursor in the upper-left of the LCD.
        //i.e. Set cursor at start of first line
        lcd.home();
        break;
      case CMD_SECONDLINE_CHAR:
        //Set cursor at start of second line
        lcd.setCursor(0, 1);
        break;
      case CMD_BACLIGHT_ON_CHAR:
        //Turns on the LCD display, after it's been turned off
        BL_On();
        break;
      case CMD_BACKLIGHT_OFF_CHAR:
        //Turns off the LCD display
        BL_Off();
        break;
      case CMD_DISPLAY_ON_CHAR:
        //Turns on the LCD display, after it's been turned off
        lcd.display();
        break;
      case CMD_DISPLAY_OFF_CHAR:
        //Turns off the LCD display
        lcd.noDisplay();
        break;
      case CMD_CURSOR_CHAR:
        //Display the LCD cursor: an underscore (line) at the position
        // to which the next character will be written
        lcd.cursor();
        break;
      case CMD_NOCURSOR_CHAR:
        //Hides the LCD cursor
        lcd.noCursor();
        break;
      case CMD_BLINK_CHAR:
        //Display the blinking LCD cursor.
        lcd.blink();
        break;
      case CMD_NOBLINK_CHAR:
        //Turns off the blinking LCD cursor.
        lcd.noBlink();
        break;
      case CMD_SCROLLRIGHT_CHAR:
        //Scrolls the contents of the display
        // (text and cursor) one space to the right.
        lcd.scrollDisplayRight();
        break;
      case CMD_SCROLLLEFT_CHAR:
        //Scrolls the contents of the display
        // (text and cursor) one space to the left.
        lcd.scrollDisplayLeft();
        break;
      case CMD_AUTOSCROLL_ON_CHAR:
        //Turns on automatic scrolling of the LCD.
        lcd.autoscroll();
        break;
      case CMD_AUTOSCROLL_OFF_CHAR:
        //Turns off automatic scrolling of the LCD
        lcd.noAutoscroll();
        break;
      case CMD_HELLO_WORLD_CHAR:
        lcd.clear();
        lcd.print("Hello, World!");
        break;
      case CMD_TOGGLE_CMD_CHAR:
        commandMode = false;
        break;
      default:
        commandMode = false;
        break;
    }
  }
  else
  {
    if (ch == '~')
      commandMode = true;
    else
    {
      lcd.print(ch);
      Serial.print(ch);
    }

  }
}