while循环无限运行。无法使用计时器

时间:2019-03-26 10:19:55

标签: python-2.7 arduino-uno

我正在尝试通过Python打开和关闭电磁阀。可以在实验开始之前使用信息框定义此切换模式。例如,我可以定义切换的时间(切换的次数)或切换的时间(切换的时间,以秒为单位)。我向Arduino发送了两个字节,一个字节用于通道选择(1至8),第二个字节用于状态(0或1)。

切换次数非常有效。在while循环中,我给出了myTime> 0的指令,并且在每次切换过程中都将其减小了1。因此,while循环将一直运行,直到myTime或切换数变为零为止。

但是,当我尝试从信息框中分配时间时,它将变成一个无限循环。我可以从信息框中提供时间,然后将其添加到time.time()中,并尝试在while循环中进行比较,并想在time.time()大于所需时间时终止循环。

如何确保可以同时使用两种条件终止while循环?

Python code: 

from __future__ import absolute_import, division, print_function
import serial
from time import sleep
import struct
from psychopy import core, data, event, gui, visual 
import time


try:
    arduino = serial.Serial('COM8',19200)
    sleep(2)
    print("Connection to " + 'COM8' + " established succesfully!\n")
except Exception as e:
    print(e)

global command

## create a DlgFromDict
info = {'Observer':'jwp', 'Channel':['1','2','3','4','5','6','7','8'],
    'BreathingCycle':4, 'Timer': 0, 'ExpVersion': 1.1, 'Debug Mode': True}
infoDlg = gui.DlgFromDict(dictionary=info, title='TestExperiment',
    order=['ExpVersion', 'Observer'],
    tip={'Observer': 'trained visual observer, initials'},
    fixed=['ExpVersion'])  

myChannel = info['Channel']
myTime = info ['BreathingCycle']
myTimer = info ['Timer']



win = visual.Window(fullscr=True, size=(1536, 864), monitor='laptop') 
#TODO: handle in a different way the screen resolution

instruction1 = visual.TextStim(win, text=u"""Valve on!""")

instruction2 = visual.TextStim(win, text=u"""Valve off!""")

instruction3 = visual.TextStim(win, text=u"""Thank you!""")


if infoDlg.OK:

Mytimer = time.time() + myTimer

while (myTime > 0 or time.time() < Mytimer):     

        if  myChannel == '1': 
            if u'i' in event.waitKeys():
                command = arduino.write(struct.pack(u'>H',513)) 
                myTime = myTime - 1
                instruction1.draw() 
                win.flip()
            if u'e' in event.waitKeys():
                command = arduino.write(struct.pack(u'>H',512))
                myTime = myTime - 1
                instruction2.draw()
                win.flip()
        elif  myChannel == '2': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',257)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',256))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '3': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',2049)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',2048))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '4': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',1025)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',1024))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '5': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',4097)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',4096))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '6': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',8193)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',8192))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '7': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',32769)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',32768))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()
        elif  myChannel == '8': 
                if u'i' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',16385)) 
                    myTime = myTime - 1
                    instruction1.draw() 
                    win.flip()
                if u'e' in event.waitKeys():
                    command = arduino.write(struct.pack(u'>H',16384))
                    myTime = myTime - 1
                    instruction2.draw()
                    win.flip()

win.close ()  
arduino.close()

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,因为有event.waitKeys()函数,它基本上在无限长的时间内等待键,这就是为什么time.time()