How do I do nothing?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

How do I do nothing?

Post by soggycashew » Fri Oct 22, 2021 12:44 pm

Hello, I have the code below I don't want my buttons number to go below 0 and give me -1, -2 etc. and I want to do an else "Do Nothing". What in would I use for MicroPython for that? I tried Pass and Continue but get an invalid syntax. I also want to know is there a "Cheat Sheet" for symbols and their meaning like Python has that I can reference? Thanks!

Code: Select all

def count_down(pin):
    global rnds_clicks
    if btn_rnds_down.value() == 1:
#           Print 14 blank spaces to clear round counter
        lcd.move_to(6,3) 
        lcd.putstr("             ")
        rnds_clicks -= 1  #Decreases 1 to the rounds counter
        lcd.move_to(6,3)
        lcd.putstr('{}'.format(rnds_clicks))
        utime.sleep(0.25)
        else
        'Do nothing'

#Attach interrupt to btn_rnds_down              
btn_rnds_down.irq(trigger=machine.Pin.IRQ_RISING, handler=count_down)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How do I do nothing?

Post by dhylands » Fri Oct 22, 2021 2:16 pm

You need a colon on the else:

Code: Select all

else:
    pass
You can also just drop the else entirely and you’ll get the same result.

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: How do I do nothing?

Post by soggycashew » Fri Oct 22, 2021 2:32 pm

@dhylands Do I need to add another If statement for if my rnds_clicks < 0 then do nothing? I tried the below but get a error

TypeError: unsupported types for __lt__: 'Pin', 'int'

Code: Select all

def count_down(pin):
    global rnds_clicks
    if btn_rnds_down.value() == 1:
        if rnds_clicks < 0:
            pass
        else:
#       Print 14 blank spaces to clear round counter
            lcd.move_to(6,3) 
            lcd.putstr("             ")
            rnds_clicks -= 1  #Decreases 1 to the rounds counter
            lcd.move_to(6,3)
            lcd.putstr('{}'.format(rnds_clicks))
            utime.sleep(0.25)
Last edited by soggycashew on Fri Oct 22, 2021 3:15 pm, edited 1 time in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How do I do nothing?

Post by pythoncoder » Fri Oct 22, 2021 2:53 pm

The error will go away if your code reads:

Code: Select all

        if btn_rnds_down.value() < 0:
However it still doesn't make much sense as the line will only be executed if the value is 1. What range of values do you want to accept? You can write:

Code: Select all

if 0 <= btn_rnds_down.value() <= 10:
    # do stuff if value is between 0 and 10
Peter Hinch
Index to my micropython libraries.

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: How do I do nothing?

Post by soggycashew » Fri Oct 22, 2021 3:12 pm

Im trying to decrease the rnds_clicks value by 1 with each button press BUT not allow the rnds_clicks number to go below 0 like -1, -2 etc.

I got it with..... Thanks!

Code: Select all

def count_down(pin):
    global rnds_clicks
    if btn_rnds_down.value() == 1:
        if rnds_clicks < 1:
            pass
        else:
#       Print 14 blank spaces to clear round counter
            lcd.move_to(6,3) 
            lcd.putstr("             ")
            rnds_clicks -= 1  #Decreases 1 to the rounds counter
            lcd.move_to(6,3)
            lcd.putstr('{}'.format(rnds_clicks))
            utime.sleep(0.25)
    
#Attach interrupt to btn_rnds_down              
btn_rnds_down.irq(trigger=machine.Pin.IRQ_RISING, handler=count_down)

Post Reply