I am monitoring an external device that outputs a boolean value of False when the condition is not met, and a boolean value of True when the condition is met. The problem is, the boolean value is output more than once after the condition is met.
This is what the output looks like:
False
False
False
False
True
True
False
False
False
So what I would like to be able to do is monitor this value and execute a simple function, only once for each time the boolean changes from False to True. I have seen other languages that have a simple "on change" function, so I know its possible and probably a lot easier than I'm making it out to be.
Currently I have this. For the sake of explaining this, the variable "ext_data" is used as the external data that the python script is monitoring.
while True:
if ext_data == True:
print("true")
pass
This prints "true" every time the boolean is equal to true, and when it resets itself the boolean is still set to True, so I get multiple readings when I only want one.
Thanks to anyone who helps out!
edit:
Updated code using the EdgeDetector class written by holdenweb.
ext_data = True/False # This returns True or False depending on the state
# of the variable. It is constantly monitored and
# could change at any moment, which is why I'm
# running the function in a while True loop
def printer():
print(u'something%s' % serialport.cts)
post_to_api('cycle_count')
def myfunction():
return ext_data
test_subject = EdgeDetector(myfunction, printer)
while True:
test_subject.test()
This still returns occasional duplicate posts. It was mentioned that this data needs to be in an array for the EdgeDetector Class to work properly, how would you suggest putting this data into an array without creating an infinitely long array?