getState() function returns different values

Supporting 2.7 and 3.2+
Post Reply
DaeviaAlex
Phidgetsian
Posts: 5
Joined: Thu Dec 15, 2022 12:06 am
Contact:

getState() function returns different values

Post by DaeviaAlex »

Hi,

I noticed something that I think is a little strange.

Let's say that you wanted to check the Digital Input state of one port of the VINT hub in your main loop, but inside a WHILE loop that would do so for a certain amount of time.

Here is a code to illustrate this:

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
import time

def main():
    #Create your Phidget channels
    DigitalInput0 = DigitalInput()
    DigitalInput0.setIsHubPortDevice(True)
    DigitalInput0.setHubPort(0)
    DigitalInput0.openWaitForAttachment(1000)
    
    #Timeout time to break the WHILE loop
    timeout = 1
    
    #Count the amount of time you go through that loop
    count = 0
    
    #Print initial state
    state = DigitalInput0.getState()
    print("Initial state: " + str(state))
    
    #Record initial time
    timeout_start = time.time()
    #WHILE timeout has not passed, check digital input signal and record and print count
    while time.time() < (timeout_start + timeout):
        state = DigitalInput0.getState()
        count = count+1
        print("Count: " + str(count)) 
        
    #Print final Digital Input state recorded
    print("Final state: " + str(state))                       
        
    #Close channels
    DigitalInput0.close()

main()


When I run this code with Thonny, I get a TRUE response for the initial state, but then a FALSE one after the WHILE loop. The sensor (an encoder) remained in the same position the whole time and did not move. I get the same result even with a smaller interval like 0.1sec instead of 1sec.

Why do I get such contradictory responses?

Thanks for your help.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests