[Python] Python program stops after specific number of passes [closed]

Discussão em 'Python' iniciado por Stack, Julho 8, 2025.

  1. Stack

    Stack Membro Participativo

    The program measures two analog voltages by means of a MCP3008 ad converter. It imports MCP3008 from GPIOzero. In its present form it runs thru a "while:true" loop collecting data from two MCP channels. after going thru the loop 23 times it then stops. When i start it again it first gives some lines of alerts. When i hash out one os the channels from the loop, the programs runs 43 time thru the loop and stops. I use a Raspi 5 with Version 3.11.2 software

    I changed the the number of passes thru the loop from 2 to 1 which increases the number of passes to 43 before stopping. Also varied the Sleep value which has no effect Below is the coding of the program

    #Proefje NOK werkt ok


    from gpiozero import MCP3008

    from time import sleep
    count = 0

    def meetmcp(channel): #Meet de waarde van een MCP3008 channel
    tmp = MCP3008(channel)
    # print (tmp.value)

    waarde = (tmp.value * 3.3-0.5)*100 #Zet de output van de MCP om in bruikbare waarde
    if channel == 0:
    result = (str(round(waarde,1))),('BB current =',channel, 'Amp') #Rond waarde af op 1 dec en zet deze in result
    # print (result)
    BBCurrent =(str(round(waarde,1)))
    print ('BBCurrent = ',BBCurrent)

    if channel == 1:
    result = (str(round(waarde,1))),('SB current =',channel, 'Amp')
    # print (result)
    SBCurrent = (str(round(waarde,1)))#Print result
    print('SBCurrent = ',SBCurrent)

    while True:
    channel=0
    meetmcp(channel)
    channel=1
    meetmcp(channel)
    sleep(0.5)
    count +=1
    print (count)



    Following is the alerts that i get back when i rerun the program after it has stopped.

    Traceback (most recent call last):
    File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 139, in __del__
    super().__del__()
    File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 153, in __del__
    self.close()
    File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 94, in close
    old_close()
    File "/usr/lib/python3/dist-packages/gpiozero/pins/lgpio.py", line 303, in close
    self.pin_factory.release_all(self)
    File "/usr/lib/python3/dist-packages/gpiozero/pins/__init__.py", line 123, in release_all
    self.release_pins(reserver, *(
    File "/usr/lib/python3/dist-packages/gpiozero/pins/__init__.py", line 105, in release_pins
    with self._res_lock:
    KeyboardInterrupt:
    Could not interrupt current process. Forcing the program to stop.

    Continue reading...

Compartilhe esta Página