this is my first time asking question in this forum. If there are mistakes in posting this question, please mention it.
I am doing a menu function by using Python. In the code, I used def to classify the functions like keypad and what to display. The following is the code:
#import
import RPi.GPIO as GPIO
import time
def main():
# Initialise display
lcd_init()
definition()
while True:
lcd_string("Press any key to",LCD_LINE_1)
lcd_string("continue",LCD_LINE_2)
keypad_init()
key_pad()
time.sleep(0.5)
menu_page()
key_pad1()
time.sleep(0.5)
def keypad_init():
for j in range(4):
GPIO.setup(col[j],GPIO.OUT)
GPIO.output(col[j],1)
for i in range(4):
GPIO.setup(row[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
def definition():
global row
row=[2,3,4,17]
global col
col=[27,22,10,9]
global MATRIX
MATRIX=[[1,2,3,'A'],
[4,5,6,'B'],
[7,8,9,'C'],
['Yes',0,'No','D']]
def key_pad():
# Keypad Function
while True:
for j in range(4):
GPIO.output(col[j],0)
for i in range(4):
if GPIO.input(row[i])==0:
return
while(GPIO.input(row[i])==0):
pass
GPIO.output(col[j],1)
def key_pad1():
# Keypad Function
global pagecount
while True:
for j in range(4):
GPIO.output(col[j],0)
for i in range(4):
if GPIO.input(row[i])==0:
pagecount = MATRIX[i][j]
return
while(GPIO.input(row[i])==0):
pass
GPIO.output(col[j],1)
def menu_page():
lcd_string("1.Diagnose",LCD_LINE_1)
lcd_string("2.BMI Calculator",LCD_LINE_2)
def option_choose():
if pagecount ==1:
lcd_string("Diagnose Start!",LCD_LINE_1)
lcd_string(" ",LCD_LINE_2)
if pagecount ==2:
lcd_string("BMI Calculate!",LCD_LINE_1)
lcd_string(" ",LCD_LINE_2)
def lcd_init():
# Initialise display
The problem is that the program does not stop at key_pad() and key_pad1(). It just keep on looping and change the things to be displayed. I want to display "Press any key to continue" until a key is pressed. Please help me with the problem. If the question is not clear, please state them. Thank you very much. Ps. I am using Raspberry Pi for the hardware part
Instead of using a while True
loop to wait a key press, you can use GPIO.add_event_detect
method :
def keypad_init():
for j in range(4):
GPIO.setup(col[j],GPIO.OUT)
GPIO.output(col[j],1)
for i in range(4):
GPIO.setup(row[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(row[i], GPIO.BOTH, callback=keyPressed)
def keyPressed:
# Do your action here
You can also use GPIO.event_detected()
method, depending on what you want to do. Take a look at https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.