vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Create a Menu in which options can be interacted with buttons and displaying them in LCD

Thu Oct 18, 2018 6:10 am

As I said i want to create a menu which can be interacted by buttons. I am currently programming in R-Pi 3 Model 3. The Program which I come up with looks like the one below. I made the first layer of the menu but can you help me in selecting one of them and creating menu inside every single one of those. What i'm asking is that if i have four main menu like: Fruits, Vegetables, Clothes and Shoes. But when i'm on Vegetables and I press another button then it takes me into that sub-menu like showing different vegetables like pea, cabbage, spinach etc. and another Button to go back. I Have Maximum four buttons to interact with the menu.

Code: Select all

from RPLCD import CharLCD, cleared, cursor
import RPi.GPIO as GPIO
import time
from gpiozero import Button
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

lcd = CharLCD(numbering_mode=GPIO.BCM, cols=16, rows=2, pin_rs=13, pin_e=6, pins_data=[5,22,17,27,12,25,24,23])

Nbutton = Button(19, pull_up=False, bounce_time=0.001)
Ubutton = Button(26, pull_up=False, bounce_time=0.001)

def Fruits():
    lcd.cursor_pos = (0, 0)
    lcd.write_string("Fruits-1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("Fruits-2")
                
def Vegetables():
    lcd.cursor_pos = (0, 0)
    lcd.write_string("Vegetables-1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("Vegetables-2")
        
def Clothes():
    lcd.cursor_pos = (0, 0)
    lcd.write_string("Clothes-1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("Clothes-2")
        
def Shoes():
    lcd.cursor_pos = (0, 0)
    lcd.write_string("Shoes-1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("Shoes-2")

count=0
try:
    while 1:    
        for count in range(0,4):
            Nbutton.wait_for_press()
            count = count +1
            print (count, "presses so far")
            if count == 1:
                Fruits()
            elif count == 2:
                Vegetables()
            elif count == 3:
                Clothes()
            elif count == 4:
                Shoes()
            else:
                pass
            time.sleep(0.3)
        
except:
    pass

finally:
    lcd.clear()
    GPIO.cleanup()
Any Help Is Greatly Appreciated!

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Thu Oct 18, 2018 4:43 pm

So this is my take on how you might do it.

Code: Select all



def level0():
    #main menu
    lcd.cursor_pos = (0, 2)
    lcd.write_string('main menu option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('main menu option 2')
    lcd.cursor_pos = (2, 2)
    lcd.write_string('main menu option 3')
    lcd.cursor_pos = (3, 2)
    lcd.write_string('main menu option 4')
    
def level1():
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 1 option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 1 option 2')
    lcd.cursor_pos = (2, 2)
    lcd.write_string('sub menu 1 option 3')
    lcd.cursor_pos = (3, 2)
    lcd.write_string('sub menu 1 option 4')
    
def level2():
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 2 option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 2 option 2')
    lcd.cursor_pos = (2, 2)
    lcd.write_string('sub menu 2 option 3')
    lcd.cursor_pos = (3, 2)
    lcd.write_string('sub menu 2 option 4')
    
def level3():
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 3 option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 3 option 2')
    lcd.cursor_pos = (2, 2)
    lcd.write_string('sub menu 3 option 3')
    lcd.cursor_pos = (3, 2)
    lcd.write_string('sub menu 3 option 4')
    
def level4():
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 4 option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 4 option 2')
    lcd.cursor_pos = (2, 2)
    lcd.write_string('sub menu 4 option 3')
    lcd.cursor_pos = (3, 2)
    lcd.write_string('sub menu 4 option 4')
    
def clearpointer():
    lcd.cursor_pos = (0, 0)
    lcd.write_string(' ')
    lcd.cursor_pos = (1, 0)
    lcd.write_string(' ')
    lcd.cursor_pos = (2, 0)
    lcd.write_string(' ')
    lcd.cursor_pos = (3, 0)
    lcd.write_string(' ')    
    
level = 0
position = 0


level0()
lcd.cursor_pos = (position, 0)
lcd.write_string('>')
while True:

    button = input('1 = down, 2 = up, 3 = select, 4 = level up ')
    if button = 1:
        position = position + 1

    if button = 2:
    position = position - 1
    
    clearpointer()
    lcd.cursor_pos = (position, 0)
    lcd.write_string('>')
    
    if button = 3:
        if position = 0:
            level = 1
            lcd.clear()
            level1()
            position = 0
            
        if position = 1:
            level = 2
            lcd.clear()
            level2()
            position = 0
            
        if position = 2:
            level = 3
            lcd.clear()
            level3()
            position = 0  

        if position = 3:
            level = 4
            lcd.clear()
            level4()
            position = 0       
            
    if button = 4:   
            level = 0
            lcd.clear()
            level0()
            position = 0
            
            
This is of course not a working program just an example of some code that might do what you want.

So we have 4 buttons represented in the code by the input from the keyboard
button 1 moves ">" up
button 2 moves ">" Down
button 3 selects the option were the pointer ">" is
button 4 goes back to the main menu

So we start at the main menu and load the LCD with its options as defined in function level0
then we place the pointer at the first line by setting the cursor at 0,0 and writing the string ">".

now you can move the ">" down by inputting 1 and the corresponding if statement adds 1 to the position variable and clears the ">" by calling the function clearpointer and then re-draws the ">" at the new position.

the same occurs if you move the ">" up by entering 2 but we subtract 1 from the position variable.

there is no error checking in this example to prevent the ">" from moving off the screen and crashing the program.

now if you input a 3 then the program takes the current position and from that decides which sub menu to update the screen with, first clearing the screen then calling the relevant function, options level 1 to level 4,
it then sets the ">" back to position 0 and writes the string.

lastly in this example inputting 4 causes the menu to be rest to the main menu and the ">" to be rest to position 0.

now I have only done this for one menu up from the main menu but you could add as many levels as you wish, the program does not know what's on the screen only at what level its at and the position of the pointer ">", using the variables level & position.

hope this helps and gives you some ideas on how you can create your menu .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Fri Oct 19, 2018 10:54 am

Thank You Soo much for helping me out.I'm really sorry i forgot to mention that i'm using 16x2 LCD. The code you wrote was for some different LCd. I really liked your approach and finally came with the program below. But i got stuck when I tried to enter a menu. I tried my best to get in a menu where cursor is currently set but couldn't find a way in which it should actually work. The code you wrote above doesn't actually gave me idea on how to detect that a button is pressed. What i'm doing is that i use

Code: Select all

button.wait_for_press()
to detect where i think a button might be pressed but if you have any other approach to detect that a button is pressed and when pressed specific block of code should be executed. Thank You again for helping me out, I appreciate it.

Code: Select all

from RPLCD import CharLCD, cleared, cursor
import RPi.GPIO as GPIO
import time
from gpiozero import Button
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

lcd = CharLCD(numbering_mode=GPIO.BCM, cols=16, rows=2, pin_rs=13, pin_e=6, pins_data=[5,22,17,27,12,25,24,23])

def level0():
    #main menu
    lcd.cursor_pos = (0, 2)
    lcd.write_string('main option 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('main option 2')
def level1():
    #sub menu for cursor(0,0)
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 1')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 1')
def level2():
    #sub menu for cursor(1,0)
    lcd.cursor_pos = (0, 2)
    lcd.write_string('sub menu 2')
    lcd.cursor_pos = (1, 2)
    lcd.write_string('sub menu 2')
        
button = Button(21, pull_up=False, bounce_time=0.001)#down
button2 = Button(26, pull_up=False, bounce_time=0.001)#up
button3 = Button(19, pull_up=False, bounce_time=0.001)#next
button4 = Button(20, pull_up=False, bounce_time=0.001)#back

try:
    while 1:
        for position in range(0,1):
            level0()
                
            b = button2.wait_for_press()
            if b == 1:
                lcd.clear()
                level0()
                print(position)
                lcd.cursor_pos = (position, 0)
                lcd.write_string('>')

            a = button.wait_for_press()
            if a == 1:
                position = position + 1
                print(position)
                lcd.clear()
                level0()
                lcd.cursor_pos = (position, 0)
                lcd.write_string('>')
                time.sleep(0.3)
except:
    pass

finally:
    lcd.clear()
    GPIO.cleanup()
With this code i'm able to navigate between two main menu options with curosr(>) by pressing different keys for both like for first option up button and for down option down button. But how can I get into sub menu while cursor is on (0,0) or (1,0).

I tried to work something out with position and if statement but couldn't do it. Help me out!

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Fri Oct 19, 2018 6:14 pm

OK no problem I have a 16x2 LCD and 4 buttons I can setup and workout some code for you but I am busy over the weekend so it might be monday before I get back to you.

In the mean time which version of RPLCD just so I set up with the correct version.

also if you are only using a 2 line display can we just use 2 buttons one to select each line and a third to go back, that way we don't need to code to move the pointer, if you prefer to stick with the moving pointer no problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Sun Oct 21, 2018 11:56 am

I'm Currently using latest version of RPLCD i.e. 1.1.0 and it's fine if we use only 2 buttons. I said that because i have maximum of 4 buttons to control with. And if you prefer not to use pointer I don't mind it either. I tried my best to get this done but as i'm still learning few things here and there and will get better eventually.
No problem with Monday, it sounds fine with me.
I'm really glad you helping me. I appreciate it

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 11:33 am

Have you actually used RPLCD 1.1.0 to display the menu on an LCD ?

because I have tiered twice now second time on a fresh build of stretch line and it always returns the error

"TypeError: this constructor takes no arguments"

for the lcd setup line ,

I have even tried the example LCD test included with it and it returns the same error.

EDIT..

should have studied the docs better..

Code: Select all

from RPLCD.gpio import CharLCD
this is the correct import line under version 1.1

and not

Code: Select all

from RPLCD import CharLCD, cleared, cursor
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 11:43 am

pcmanbob wrote:
Mon Oct 22, 2018 11:33 am
Have you actually used RPLCD 1.1.0 to display the menu on an LCD ?

because I have tiered twice now second time on a fresh build of stretch line and it always returns the error

"TypeError: this constructor takes no arguments"

for the lcd setup line ,

I have even tried the example LCD test included with it and it returns the same error.
Actually I Started programming my LCD from about 25-30 days and installed RPLCD back then only so i don't think there was any update in a past month. Even though i would check it for you if you just tell me how can i check the current version of my RPLCD library?

Why don't you try my program(both) given above as its actually well tested on my device and in 8 bit mode. With RPLCD let me know if there is anything else I can do to help you out. And one more thing I use Python 3, if it helps solve that issue.


EDIT..
Ok. Thank you for telling me that. Will keep it in mind.

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 1:35 pm

vickey97 wrote:
Mon Oct 22, 2018 11:43 am
pcmanbob wrote:
Mon Oct 22, 2018 11:33 am
Have you actually used RPLCD 1.1.0 to display the menu on an LCD ?

because I have tiered twice now second time on a fresh build of stretch line and it always returns the error

"TypeError: this constructor takes no arguments"

for the lcd setup line ,

I have even tried the example LCD test included with it and it returns the same error.
Actually I Started programming my LCD from about 25-30 days and installed RPLCD back then only so i don't think there was any update in a past month. Even though i would check it for you if you just tell me how can i check the current version of my RPLCD library?

Why don't you try my program(both) given above as its actually well tested on my device and in 8 bit mode. With RPLCD let me know if there is anything else I can do to help you out. And one more thing I use Python 3, if it helps solve that issue.


EDIT..
Ok. Thank you for telling me that. Will keep it in mind.
There are no updates for RPLCD it only updates when a new version comes out. I think my problem was cause by the incorrect import line you had in the code you posted , I happen to be using python 2 so that may also have affected it.

you should only use the import line I posted if you are connecting your LED directly to the pi using the gpio pins which I assume you are because of your lcd setup line.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 2:03 pm

So we have a working program .

Code: Select all

from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)# button 1
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)# button 2
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)# button 3
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # not used 

lcd = CharLCD(numbering_mode=GPIO.BCM, cols=16, rows=2, pin_rw=None, pin_rs=13, pin_e=6, pins_data=[5,22,17,27,12,25,24,23], 
charmap='A02', auto_linebreaks=False)

# set global variables used
update = 1 # causes LCD to be updated while set to 1
mlevel = 1 # current menu level
blevel = 1 # last menu level

def level1():
    #main menu
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Footware")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Transport")
    
def level2():
    #sub menu 
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Shoes")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Boots")
    
def level3():
    #sub menu 
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Ground")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Air")
    
def level4():
    #sub menu
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Red")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Black")
    
def level5():
    #sub menu 
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Lace up")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Zip up")
    
def level6():
    #sub menu 
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Car")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Bus") 
    
def level7():
    #sub menu 
    lcd.cursor_pos = (0, 2)
    lcd.write_string("1.Plane")
    lcd.cursor_pos = (1, 2)
    lcd.write_string("2.Helicopter")   
    
def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    update = 1     
    
GPIO.add_event_detect(26, GPIO.RISING, callback=option1, bouncetime=200)    
GPIO.add_event_detect(19, GPIO.RISING, callback=option2, bouncetime=200)   
GPIO.add_event_detect(20, GPIO.RISING, callback=goback, bouncetime=200)    
        

#loop to update menu on LCD
while True:
    while update ==0:
        time.sleep (0.1)
        
     
    lcd.clear()
    
    if mlevel == 1: level1()
    if mlevel == 2: level2()
    if mlevel == 3: level3()
    if mlevel == 4: level4()
    if mlevel == 5: level5()
    if mlevel == 6: level6()
    if mlevel == 7: level7()
            
    update = 0

 
First thing to say is there is no error checking in the program so if you try to select an option beyond the 3rd level the program will fail, likewise if you try going back beyond the main menu.

So I don't use gpiozero so the buttons are just defined in RPi.GPIO using the event detect option, and you don't need to setup the gpio pins for the LCD as outputs RPLCD takes care of that for you, I have added to your lcd setup line with the options they list in the RPLCD docs.

https://rplcd.readthedocs.io/en/stable/index.html

So we use 3 buttons
button 1 selects 1st option on on each menu (gpio 26)
button 2 selects 2nd option on on each menu (gpio 19)
button 3 selects steps you back through the menu one level at a time (gpio 20)

So the updating of the display is handled by the while true loop , which has a wait created by the second while loop that only allows the display to be updated if something changes, like when you press a button.

Now the button presses each call there own function which for button 1 & 2 move you up through the levels using some simple maths so the levels increment correctly, but because we are updating the level , blevel & update variables in functions we need to tell them to use the global variables used in the main loop that updates the display.

button 3 works slightly differently as it first resets the mlevel variable to the blevel and then calculates what the new blevel should be, so that you can keep pressing the button to move back down the menu levels.

As I said I used python 2 and this program as be tested using that, Its should work under python 3 but I have not tested it.

and finally if you dont understand any thing or have problems post your question and I will do my best to help.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 3:31 pm

It's Working! Thank You so much. This is what I've been asking for thanks a lot. I edited your code as per my need but its perfect.

And I just have few questions right now, hope you don't mind.
1. If I want to connect arduino with R-Pi and send and receive data which method would you use or what would you suggest me to use for communicating between two?
2. So if i have arduino connected with R-Pi and i'm reading some values from arduino can I set them to display those values into my LCD?
3. If my memory in arduino is not enough for me can I use memory of R-Pi? Is it even possible?
4. I have a PS4 Controller is it possible to use my controller with some kind of WiFi adapter and connect it to R-Pi's WiFi and control whatever I've programmed?

Once Again Thank you so much for helping me out. I appreciate it.

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 3:55 pm

vickey97 wrote:
Mon Oct 22, 2018 3:31 pm
It's Working! Thank You so much. This is what I've been asking for thanks a lot. I edited your code as per my need but its perfect.

And I just have few questions right now, hope you don't mind.
1. If I want to connect arduino with R-Pi and send and receive data which method would you use or what would you suggest me to use for communicating between two?
2. So if i have arduino connected with R-Pi and i'm reading some values from arduino can I set them to display those values into my LCD?
3. If my memory in arduino is not enough for me can I use memory of R-Pi? Is it even possible?
4. I have a PS4 Controller is it possible to use my controller with some kind of WiFi adapter and connect it to R-Pi's WiFi and control whatever I've programmed?

Once Again Thank you so much for helping me out. I appreciate it.
Glad the program does what you want , don't forget to add some error checking to prevent you from going below menu level 1 or above your top menu level.

To answer your questions

1. I don't have/use arduino's so cant help with connecting them to a pi, but it is mentioned a lot on the forum https://www.google.com/search?q=get+dat ... =firefox-b

2. If you can get the data from the arduino then once its on your pi its just a matter of converting the data to a string then you can display it on your LCD.

3. I don't have a PS4 or a PS4 controller so cant help with that., again search the forum someone will have done it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 4:15 pm

Thanks! Understood

Do you have any Idea for error checking? I'll probably try for error checking tomorrow so just wanted to know in which side should i approach.
I just have some idea like preventing the value of mlevel to not go below 1 or something like that. I'll probably know it tomorrow if that works. Any suggestions might help me.
By the way, Thanks again.

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 4:28 pm

This is un-tested but should work.

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6: mlevel = 6
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7 mlevel = 7
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1
    update = 1     
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 4:37 pm

Thanks. I will try it out tomorrow. and let you know if its ok.
And whats is the use of charmap="A02" in our program? What is it actually used for in LCD other than this program?

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Mon Oct 22, 2018 4:52 pm

vickey97 wrote:
Mon Oct 22, 2018 4:37 pm
And whats is the use of charmap="A02" in our program? What is it actually used for in LCD other than this program?
Read the documents lots of information in there.

https://rplcd.readthedocs.io/en/stable/ ... acter-maps
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Tue Oct 23, 2018 6:48 am

pcmanbob wrote:
Mon Oct 22, 2018 4:28 pm
This is un-tested but should work.

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6: mlevel = 6
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7: mlevel = 7
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1
    update = 1     
    
This doesn't actually work as when button 26 is pressed 2 times then mlevel=4 when pressed once again value of mlevel=8 i.e. >6 so it will open level 6 which is car and bus. It does work as some option but is not perfect method for error checking.

I found a way to this error checking and just wanted to share here if anyone in future needs this error checking to work.
What I did here is same like you just with different mind set.

EDIT... Sorry copied the same code again

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6: mlevel = blevel #error checking
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7: mlevel = blevel #error checking
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1 #error checking
    update = 1
This worked for me! Thanks
Last edited by vickey97 on Tue Oct 23, 2018 10:00 am, edited 1 time in total.

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Tue Oct 23, 2018 9:51 am

vickey97 wrote:
Tue Oct 23, 2018 6:48 am
pcmanbob wrote:
Mon Oct 22, 2018 4:28 pm
This is un-tested but should work.

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6: mlevel = 6
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7: mlevel = 7
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1
    update = 1     
    
This doesn't actually work as when button 26 is pressed 2 times then mlevel=4 when pressed once again value of mlevel=8 i.e. >6 so it will open level 6 which is car and bus. It does work as some option but is not perfect method for error checking.

I found a way to this error checking and just wanted to share here if anyone in future needs this error checking to work.
What I did here is same like you just with different mind set.

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6: mlevel = 6 #error checking
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7: mlevel = 7 #error checking
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1 #error checking
    update = 1
This worked for me! Thanks

I can't see any difference between what you posted and what I posted ?
Last edited by pcmanbob on Tue Oct 23, 2018 10:12 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

vickey97
Posts: 37
Joined: Tue Jul 24, 2018 2:16 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Tue Oct 23, 2018 10:01 am

pcmanbob wrote:
Tue Oct 23, 2018 9:51 am
I can't see any difference between what you post and what I posted ?
Just Edited/updated, Sorry. Copied the same code without changes.

pcmanbob
Posts: 6632
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Tue Oct 23, 2018 10:24 am

Yes that will work as you say, but you have to press the back button twice to start moving back down the menu, but you are making progress on the understanding of how to make your menu.

try this version it fixes the problem.

Code: Select all

def option1(channel):
    global mlevel, update, blevel
    blevel = mlevel
    mlevel = mlevel*2
    if mlevel > 6:
        mlevel = int(mlevel/2)
        blevel = int(mlevel/2)
    update = 1
    
    
def option2(channel):
    global mlevel, update, blevel
    blevel = mlevel  
    mlevel = (mlevel*2) + 1
    if mlevel > 7:
        mlevel = int(mlevel/2)
        blevel = int(mlevel/2)
    update = 1  
    
def goback(channel):
    global mlevel, update, blevel
    mlevel = blevel
    blevel = int(mlevel/2)
    if blevel < 1: blevel = 1
    update = 1  
    
A lot of times its just trial and error when trying to sort out error checking because you have to cover all possibilities, and you may not see them all or you introduce another problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

abdullahsurati
Posts: 5
Joined: Fri Jul 27, 2018 5:26 pm

Re: Create a Menu in which options can be interacted with buttons and displaying them in LCD

Sun Jan 20, 2019 6:38 pm

I am trying to do something similar here, but the component is different. I am using 16x2 LCD with keypad kit. It has 5 buttons, 4 direction ones and 1 select button. Here is how it works:
1. The " lcd.up_button " returns True if the up button is pressed, similarly for all other buttons (up, down, left, right, select)
2. The " lcd.message = 'Hello\nWorld' " will display 'Hello' in the first line and 'World' in the second. This assignment always replaces text on the LCD.

I will have data in terms of lists. I am making this menu for an attendance system.
Consider this "courseList":

Code: Select all

courseList = [ 'bsc' , 'msc' , 'bba' ]
I want these options to display on the screen as scroll-able.The list is not fixed in size. The first two lines will display the first two courses, and then if I press down again, it goes to the third course over a new screen. Pressing down here goes back to first course, while pressing up goes back to second course. I was thinking to display the cursor as ">" to know which one is currently traversing.

Eg:
>bscit
mscit

If I press down:
bsc
>msc

Down again:
>bba

After the select button is pressed, a "semesterList" is generated with options, in which again selection is required. It has the same logic as before. And then, further more lists will be generated as options are given and selected by the user.
One thing I want to incorporate here is that pressing the left button will go back to the previous screen or menu, where the user can reselect the previous option.

I am unable to code this logic with my knowledge, if someone could guide me on how to get started it will surely help.

Thanks


Return to “Python”