Micropython Counter



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



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



Pycom on Twitter