getState() function returns different values
Posted: Thu Dec 22, 2022 1:38 pm
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:
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.
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.