function to count down then up on button press
function to count down then up on button press
(OP)
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 --> micropython
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
RE: function to count down then up on button press
RE: function to count down then up on button press
RE: function to count down then up on button press
if you want a circular counter the use modulo arithmatic
a=0
while True:
a = (a+1) % 6
print(a)
will get you a couht that cyles 0-5
if you want +V & negative numbers arround 0 then add & subtract an offest a= ((3+ a + 1) %6) -3
hope this at leasts helps you towards your solution
Do things on the cheap & it will cost you dear
Avaya Remote Support Engineer (A.R.S.E)