Skip to content
Advertisement

MicroPython count button clicks after 5 presses

I’m trying to count button click but only after 5 clicks and after the fifth click it will print the clicks BUT starting at 1.

Example: 
Press button  = 1 click
Press button  = 2 click
Press button  = 3 click
Press button  = 4 click
Press button  = 5 click
Press button  = "ROUND: 1"
Press button  = "ROUND: 2"........etc.

I have the function below and when button_clicks() is called it displays and counts a 6,7,8 etc. instead 1,2,3 etc.

clicks = 0

def button_clicks():
global clicks
clicks += 1
if (clicks) > 5:
    lcd.move_to(0,3) #Moves text 0 characters from left on row 4
    lcd.putstr("ROUND: " + str(clicks))

I also needed to add another two buttons B3 and B4 that would decrease or increase the number by 1. How would I do that?

Advertisement

Answer

I figured it out…. But still need a button to increase and a button to decrease the value.

def button_clicks():
global clicks
clicks += 1
if (clicks) > 5:
    lcd.move_to(0,3) #Moves text 0 characters from left on row 4
    lcd.putstr('Rnds: {}'.format(clicks-5))
else:
    lcd.move_to(0,3) #Moves text 0 characters from left on row 4
    lcd.putstr('Rnds: -{}'.format(clicks))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement