我正在尝试基于一组返回某些值的
set_car_id
在时间间隔内开关LED灯。如果设置返回某个值,则希望LED灯变亮8秒钟。在下面的代码中,一旦设置返回值,LED灯将开启8秒钟。但是,如果设置在5秒(8秒内)返回一个值,则LED灯直到下一个13秒才会打开,它将再开3秒钟,然后突然关闭。我只展示了代码的一小部分。有什么解决建议吗?last_bright_time = None
last_dim_time = None
new_action = -1
def LED_control(set_car_id):
global last_bright_time
global last_dim_time
curr_time = time.time()
should_remain_bright = False
should_remain_dim = False
if (new_action == 0): #new_action ==0 corresponds to set_car_id returning some value
if last_bright_time == None:
last_bright_time = time.time()
if (curr_time - last_bright_time) < 8:
should_remain_bright = True
if ((len(set_car_id) > 0) or should_remain_bright = True):
car_light(1) # function to bright the LED
last_dim_time = None
else:
car_light(0) # function to dim the LED
last_bright_time = None
last_*_time
变量怎么样?这样你的状态机就可以记住一些东西了。 - M. Prokhorovlast_*_time
变量,因为在调用car_light
函数后,我需要将它们清除,因为亮度从明到暗或从暗到明的状态发生了变化。 - Venkat kamal