[Python] Program stops running when executed as a function [closed]

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I run a piece of coding to read a MCP3008. When running normally"" it runs forever. The same program running as a function locks up after a few seconds. There is no error message of any sort. When i halt the program and run it again the same happens.

    I used Thonny to write and run the program but Geany give sthe same result. I have tried just about every form of elimination to determine the cause but after a day i give up and ask your help. The only difference that yield a result is running it strait forward (OK) or as a Function (NOK).

    This is the coding without use of a Function.


    from gpiozero import MCP3008
    from time import sleep

    while True:
    channel = 0
    device = 0


    #def meetmcp(channel,device):
    tmp = MCP3008(channel,device) #Maakt object tmp, setup voor MCP3008
    waarde = (tmp.value * 3.3-0.5)*100 #geeft spanning uit MCP maal 10
    print (str(round(waarde,1)))
    sleep(0.2)
    # return waarde

    while True:
    meetmcp(0,0)

    #print (waarde)




    And this is using it as a Function. It goes throuhg the second While True loop for about 30 times and then halts.


    from gpiozero import MCP3008
    from time import sleep

    #while True:
    # channel = 0
    # device = 0


    def meetmcp(channel,device):
    tmp = MCP3008(channel,device) #Maakt object tmp, setup voor MCP3008
    waarde = (tmp.value * 3.3-0.5)*100 #geeft spanning uit MCP maal 10
    print (str(round(waarde,1)))
    sleep(0.2)
    # return waarde

    while True:
    meetmcp(0,0)

    #print (waarde)



    If i have made incorrect use here pls. forgive and correct me. This is a first for me.

    Continue reading...

Compartilhe esta Página