Page 1 of 1

How do I do nothing?

Posted: Fri Oct 22, 2021 12:44 pm
by soggycashew
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)

Re: How do I do nothing?

Posted: Fri Oct 22, 2021 2:16 pm
by dhylands
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.

Re: How do I do nothing?

Posted: Fri Oct 22, 2021 2:32 pm
by soggycashew
@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)

Re: How do I do nothing?

Posted: Fri Oct 22, 2021 2:53 pm
by pythoncoder
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

Re: How do I do nothing?

Posted: Fri Oct 22, 2021 3:12 pm
by soggycashew
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)