Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

function to count down then up on button press

function to count down then up on button press

function to count down then up on button press

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 --> 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
        print('Rnds: -{}'.format(B1_clicks))
        utime.sleep(0.25)  #Pause 

RE: function to count down then up on button press

Your written requirements and your code are substantially divergent.

RE: function to count down then up on button press

@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...

RE: function to count down then up on button press

Not sure I am fully understanding your question

if you want a circular counter the use modulo arithmatic

while True:
a = (a+1) % 6

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)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close