我的arduino不断运行一系列数字6 10 14 20 25。
每个数字的生成时间为5秒。
第一个数字和第二个数字之间的时间总是相同,但是例如6& 10与例如10不同14& 20。
按此顺序运行序列: 6 10 14 20 25 6 10 14 20 25 6 10 14 20 25等等
示例时间[秒]例如。
6 - 2s - 10 - 8s - 14 - x - 20 --12s - 25 - 3s - 6 - 2s - 10 等。
你可以注意到14和14之间有'x'。 20。
这是因为x是范围(1,100)的随机时间,预期值是5秒。
所以,我想运行一个从5减1到1的计时器,这样它表示每当我从值14移动到值20并且计时器应该计数时,值14完成后应该产生值20的预期时间如果它需要超过5秒。
如果运行时间少于或超过5秒,我想返回计时器值。
使用python解决此问题的最佳方法是什么?
编辑:
下面的是我当前的代码,但它不起作用......
import time
import sys
### 'a' represent value return from the function that return current value generated by arduino.
### 'a' should be returned every 500ms BUT I do not know how to read it constantly...
a = myArduino()
#expected time between no.14 and no.20
countdown = 5
#countup if 'a' > 5
countup = 0
expectedtime = 5
overtime = 8
cnt = 0
mycountup = []
while True:
# how to start the code below IF last value returned by Arduno was 14 and time has just started between 14 and 20?
time.sleep(1)
countdown -= 1
if countdown == 0 and a is not 20:
countup += 1
if a == 20:
cnt += 1
return mycountup.append((countup,cnt))