Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function to count down then up on button press

Status
Not open for further replies.

nikademous

Technical User
Sep 9, 2018
41
0
0
US
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!

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
 
Your written requirements and your code are substantially divergent.
 
@mintjulep please talk hillbilly cuz I have no idea what your talking about....Im to the point Im done with trying to figure this out...
 
Not sure I am fully understanding your question

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)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top