Menu

Menu with LCD and keypad, Please help!

Help
Judah Ben
2020-09-26
2020-09-28
  • Judah Ben

    Judah Ben - 2020-09-26

    Please I'm needing help.
    I'm trying to set up a menu with LCD as output and matrix keypad as input. It just isn't working right,

    I'm not so sure how this is supposed to be done. I'm using subroutines for each keypress.
    I need the LCD to keep on displaying the different key functions and immediately jump to the sub-routine when a key is pressed. However, if after it starts the subroutine no further action is taken or it has done what it should, it should wait for say 3 seconds and go back to the main loop.

    I have attached the code I've gotten up.
    This is just the basic stuff, I'll still add more layers.

    Here is part of the code for quick viewing. The full gcb file is attached.

    Main:
      Do forever
    
        Select case KeypadData
          case 10 'Value 10 is Key A
            Lock
    
          case 11 'Value 11 is Key B
            Unlock
    
          case 13 'Value 13 is Key D
            Reset
    
        End select
      loop
    
         sub Lock
          cls
          print "Set Password:"
          locate 1,0
          LCDCursor FlashOn
          wait 3 s
          LCDCursor FlashOff
          Goto Main
        end sub
    
        sub Unlock
          cls
          Print "Enter Password:"
          wait 3 s
          Goto Main
        end sub
    
        sub Reset
          cls
          Print "Enter Master pin"
          wait 3 s
          Goto Main
        end sub
    
     

    Last edit: Judah Ben 2020-09-27
  • stan cartwright

    stan cartwright - 2020-09-26

    will do forever do forever? there's do while and do until but do forever would just be
    do
    loop

    Main:
      Do forever
    
        Select case KeypadData
          case 10 'Value 10 is Key A
            Print "Lock"
    
          case 11 'Value 11 is Key B
            Print "Unlock"
    
          case 13 'Value 13 is Key D
            Print "Reset"""
    
        End select
      loop
    
     
    • Judah Ben

      Judah Ben - 2020-09-27

      Thank you so much for responding.
      It seems I overlooked some errors before posting the code above (just the calling in of the subroutines). I have corrected it.
      I just use use the 'Do forever' in place of do because I think it's meant to catch errors. I think I saw that somewhere, not sure.

      Okay, Do while and Do until?
      How would I structure it??

       
      • Chris Roper

        Chris Roper - 2020-09-27

        This may help: http://gcbasic.sourceforge.net/help/_do.html

        Do
        Syntax:
        
            Do [{While | Until} condition]
            ...
            program code
            ...
            <condition> Exit Do
            ...
            Loop [{While | Until} condition]
        Command Availability:
        
        Available on all microcontrollers.
        

        Explanation:

        The Do command will cause the code between the Do and the Loop to run repeatedly while condition is true or until condition is true, depending on whether While or Until has been specified.

        Note that the While or Until and the condition can only be specified once, or not at all. If they are not specified, then the code will repeat endlessly.

        Optionally, you can specify a condition to EXIT the Do-Loop immediately.

        Example 1:

            'This code will flash a light until the button is pressed
            #chip 12F629, 4
        
            #define BUTTON GPIO.3
            #define LIGHT GPIO.5
        
            Dir BUTTON In
            Dir LIGHT Out
        
            Do Until BUTTON = 1
              PulseOut LIGHT, 1 s
              Wait 1 s
            Loop
        Example 2:
        

        This code will also flash a light until the button is pressed. This example uses EXIT DO within a continuous loop.

            #chip 12F629, 4
        
            #define BUTTON GPIO.3
            #define LIGHT GPIO.5
        
            Dir BUTTON In
            Dir LIGHT Out
        
            Do
              PulseOut LIGHT, 1 s
              Wait 1 s
              if BUTTON = 1 then EXIT DO
            Loop
        For more help, see Conditions
        
         
        • Judah Ben

          Judah Ben - 2020-09-27

          Thank you very much, I'll put something together and get back ASAP.

           
  • mmotte

    mmotte - 2020-09-27

    Your first code was very close to working . Calling the subs with select is exactly how you could build the menu.

    You do not want " Goto Main" in your subs. When the program hits "End Sub" it automatically goes back to where it came from.

    Your options menu should be put into a sub also. I don't know how big your lcd is? 1x16, 2x16,2x20. it would be nice to get the whole menu options on the display with one write, use abbreviations. "A-LK B-unlk D-rst" there it is in 16 chars. Or you could make scrolling but that is harder.

    Sub MenuOptions
    cls
     Print "A-LK B-unlk D-rst"
    End Sub
    

    Once the options are in a sub then write the MenuOptions Sub 2 different places in your main program.
    first place is in the intro right after the

    print "Welcome"
        wait 2 s   
    MenuOptions    
    

    the other place is at the end of every sub (lock,unlock,reset) before the "end sub" statement. this will clean your screen and put up the MenuOptions.

    sub Unlock
          cls
          Print "Enter Password:"
          wait 3 s
         MenuOptions
        end sub
    

    GL
    Mike

     
    • Judah Ben

      Judah Ben - 2020-09-28

      Thank you so much@mmotte.
      I appreciate the details. Let me try this and get back.
      I also would love to implement the scrolling menu immediately I get this working.
      I intend using a 4 by 16 LCD but for now, I'm using a 2 by 16 LCD.

       

      Last edit: Judah Ben 2020-09-28
  • mkstevo

    mkstevo - 2020-09-27

    I'm not sure if it is any help, but both the clocks I have posted in the finished projects section have a menu for setting the time, date and parameters. The menu is displayed on a standard 16 x 2 LCD and all options are selected and adjusted with three buttons (Up, Down, OK). The menu has a timeout where if no buttons are pressed the menu exits out. It may inspire you in how to implement something similar?

    This code is a modified form of one originally written by someone else. I thought I'd borrowed it from one of the clock examples in the demo folder, but I can't spot which one it was.

     
    • Judah Ben

      Judah Ben - 2020-09-28

      Okay, thank you. I will look them up and go through.

       
  • stan cartwright

    stan cartwright - 2020-09-27

    I would try

    do
        do until key=pressed
        loop
        If key=value1 then key1 
        else if key=value2 then key2
        else if key=value3 then key3
        endif
    loop
    ;
    sub key1
        what to do
    end sub
    ;
    sub key2
        what to do
    end sub
    ;
    sub key3
        what to do
    end sub
    

    goto is elegant sometimes but not used much now. There are alternative methods.

     
  • Anobium

    Anobium - 2020-09-27

    Stan... Case - Select. Faster and a lot more smarter.

    do until key=pressed
      select case key
    
                  key = ?
                          do one thing
                  key = ?
                          do another thing
                  key = ?
                          do a third thing
                  key = ?
                          do something else
    
            end select
    

    loop

     
    • stan cartwright

      stan cartwright - 2020-09-27

      Indeed.Wow interesting that case is more efficient / faster than multiple if then.
      Thanks for the heads up. Little things like this are not explained in the gcb help.
      I thought case would be less efficient cos it looked too polished.
      cheers

      ps lcd displays are so boring

      I hope this has not confused Judah Ben.

      I don't suppose code speed matters with lcds as they are slow as snails anyway :)

       

      Last edit: stan cartwright 2020-09-27
      • Judah Ben

        Judah Ben - 2020-09-28

        Not at all. My case of use isn't speed-sensitive too. Thank you.

         
    • Judah Ben

      Judah Ben - 2020-09-28

      Alright, thank you @evanvennn

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.