Help with LCD menu

  • 5 Replies
  • 383 Views
*

Offline Bonez34

  • Member
  • **
  • 22
    • 300ZX
Help with LCD menu
« on: September 13, 2015, 12:07:44 PM »
So I am playing with an Arduino Uno and LCD shield,  I have this menu up and running but I can't figure out how to make it do anything,  turn pins high/low,  etc.  I have tried a few basic things but I can't get it to work.  It's probably really simple but I'm new so...theres that...

Arduino LCD Keypad Shield – Basic Menu System | The hack shed

#include <LiquidCrystal.h>
 
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
 
void setup() {
   //Set the characters and column numbers.
   lcd.begin(16, 2);
   //Print default title.
   clearPrintTitle();
}
 
void loop() {
  //Call the main menu.
  mainMenu();
}
 
void mainMenu() {
  //State = 0 every loop cycle.
  int state = 0;
  //Refresh the button pressed.
  int x = analogRead (0);
  //Set the Row 0, Col 0 position.
  lcd.setCursor(0,0);
 
  //Check analog values from LCD Keypad Shield
  if (x < 100) {
    //Right
  } else if (x < 200) {
   //Up
    state = 1;
  } else if (x < 400){
   //Down
    state = 2;
  } else if (x < 600){
    //Left
  } else if (x < 800){
    //Select
    state = 3;
  }
 
  //If we are out of bounds on th menu then reset it.
  if (currentMenuItem < 0 || currentMenuItem > 4) {
   currentMenuItem = 0;
  }
 
   //If we have changed Index, saves re-draws.
   if (state != lastState) {
      if (state == 1) {
         //If Up
          currentMenuItem = currentMenuItem - 1;
          displayMenu(currentMenuItem);
      } else if (state == 2) {
         //If Down
          currentMenuItem = currentMenuItem + 1; 
          displayMenu(currentMenuItem);
      } else if (state == 3) {
         //If Selected
         selectMenu(currentMenuItem);
      }
      //Save the last State to compare.
      lastState = state;
   }
   //Small delay
  delay(5);
}
 
//Display Menu Option based on Index.
void displayMenu(int x) {
     switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("-> Menu Option 1");
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("-> Menu Option 2");
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("-> Menu Option 3");
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("-> Menu Option 4");
        break;
    }
}
 
//Print a basic header on Row 1.
void clearPrintTitle() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" HACKSHED.CO.UK ");
  lcd.setCursor(0,1);
}
 
//Show the selection on Screen.
void selectMenu(int x) {
   switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("Selected Opt 1");
        //Call the function that belongs to Option 1
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("Selected Opt 2");
        //Call the function that belongs to Option 2
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("Selected Opt 3");
        //Call the function that belongs to Option 3
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("Selected Opt 4");
        //Call the function that belongs to Option 4
        break;
    }
}

*

Offline Bonez34

  • Member
  • **
  • 22
    • 300ZX
Re: Help with LCD menu
« Reply #1 on: September 13, 2015, 02:02:43 PM »
I've got it working...sort of.  I can activate LEDs via the menu but I can't figure out how to use an external switch input.  So basically,  select a menu 1,  and if a switch is pressed light up led 1.  If menu 2 is selected,  and the switch is pressed,  light up led 2.  Seems simple,  but it just ignores the switch.

*

Offline Rx7man

  • Crazy guy
  • Hero Member
  • *****
  • 988
  • Jack of all trades
    • '94 Dodge dually
Re: Help with LCD menu
« Reply #2 on: September 13, 2015, 05:54:46 PM »
Something along the lines of

Code: [Select]
boolean Switch1 = digitalRead(Switch1Pin);

if (Menu1Selected){
//Menu 1 selected, LED reflects the state of the switch
digitalWrite(Led1Pin, Switch1);
}
if (Menu2Selected){
//Menu 2 selected, LED reflects the state of the switch
digitalWrite(Led2Pin, Switch1);
}

Of course you'll have to define "Menu1Selected" as a boolean and set it wherever you do the logic for that.. may need to have it as a global variable.



'94 dodge, Colt Cam, head worked, NV5600, 3.55's, SB clutch, Ford manual hubs w. hawk brakes, custom flatdeck, custom aluminum bumper, cab off restoration, totally new engine, Mahle pistons @19:1..HE351VE with 67mm turbine and 67x95mm compressor
440hp/1000 ft lb @ 2400 RPM

*

Offline Rx7man

  • Crazy guy
  • Hero Member
  • *****
  • 988
  • Jack of all trades
    • '94 Dodge dually
Re: Help with LCD menu
« Reply #3 on: September 13, 2015, 05:59:16 PM »
I don't think you will be able to have that "small delay" in there as it will hang up the rest of the code for too long.. just something to keep in mind if it doesn't work as planned
'94 dodge, Colt Cam, head worked, NV5600, 3.55's, SB clutch, Ford manual hubs w. hawk brakes, custom flatdeck, custom aluminum bumper, cab off restoration, totally new engine, Mahle pistons @19:1..HE351VE with 67mm turbine and 67x95mm compressor
440hp/1000 ft lb @ 2400 RPM

*

Offline Bonez34

  • Member
  • **
  • 22
    • 300ZX
Re: Help with LCD menu
« Reply #4 on: September 14, 2015, 03:12:33 PM »
Thanks,  I suppose I need to read up on booleans and whatnot.

*

Offline hakcenter

  • developer
  • Hero Member
  • *****
  • 679
    • 1998 12v Ram
Re: Help with LCD menu
« Reply #5 on: September 14, 2015, 03:29:05 PM »
Yuuup
AMP 2 Corinthians 8:21
For we take thought beforehand and aim to be honest and absolutely above suspicion, not only in the sight of the Lord but also in the sight of men.