nikademous
Technical User
Hello, first I want to start off by saying I'm super new to coding and learning and my project is in Micropython which I guess is a version of python. My question is I have a beginner project I been wanting to do and its almost done except I needed to add a counter for a button. This counter is for each button press, I need the counter to start off with each press 4,3,2,1,0 then 1,2,3,4,5,6 and so on adding 1 with each press. What I have now is with each press it counts like;
-1, -2, -3, -4, -5, 6, 7, 8, 9........
Please help I been at this for days.. Thanks!
-1, -2, -3, -4, -5, 6, 7, 8, 9........
Please help I been at this for days.. Thanks!
Code:
import utime
import machine
B1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) #C to 3.3v, NO to GPIO 14
B1_clicks = 0
def round_counter():
global B1_clicks
if B1.value(): #Was the B1 button pushed?
B1_clicks += 1
if B1_clicks > 4:
print('Rnds: {}'.format(B1_clicks))
utime.sleep(0.25) #Pause
else:
print('Rnds: -{}'.format(B1_clicks))
utime.sleep(0.25) #Pause