Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Micropython Counter

    Scheduled Pinned Locked Moved LoPy
    lopy4datacountingsd card
    2 Posts 2 Posters 578 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      Justin_Dylan
      last edited by

      I am trying to increment counter by 1 whenever a button is pressed. to this end I have come up with this code:

      import time
      import machine
      from machine import Pin

      tip_num = 0
      tip = (Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP))
      tip([1])

      while tip !="":
      tip_num += 1
      tip_value = (tip)
      total += tip_value
      tip = (Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP))

      print("Counter", tip_num)
      print("Done")

      I have clearly missed something but I have no idea what it is?
      Any help would be GREATLY welcomed.

      1 Reply Last reply Reply Quote 0
      • S Offline
        Sympatron
        last edited by

        Python code without proper indentation is not very useful. You should try to put it inside a code block like this:

        import time
        import machine
        from machine import Pin
        
        tip_num = 0
        tip = (Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP))
        tip([1])
        
        while tip !="":
          tip_num += 1
          tip_value = (tip)
          total += tip_value
          tip = (Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP))
        
        print("Counter", tip_num)
        print("Done")
        

        Apart from that there are a lot of mistakes (e.g. tip([1])) and redundancies (e.g. wrapping everything in parenthesis) in your code. You should probably read the the MicroPython and Pin documentation first.

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom