-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttonTester.py
48 lines (37 loc) · 1.13 KB
/
buttonTester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
print("start...")
import numpy
import serial # you may need to install the pySerial :pyserial.sourceforge.net
import struct
import time
import random
import pandas
import re
import threading
from psychopy import core, visual, event, gui, data, iohub, monitors
# your Serial port could be different!
try:
arduino = serial.Serial('COM3', 9600, timeout = 1)
except serial.SerialException:
arduino = serial.Serial('COM5', 9600, timeout = 1)
except serial.SerialException:
arduino = serial.Serial('COM4', 9600, timeout = 1)
except serial.SerialException:
arduino = serial.Serial('COM1', 9600, timeout = 1)
except serial.SerialException:
arduino = serial.Serial('COM2', 9600, timeout = 1)
time.sleep(3)
def waitForButton():
while arduino.in_waiting:
print arduino.read()
arduino.write(struct.pack('>B',2)) #grant permission
while True:
if(arduino.inWaiting()>0):
tmp = arduino.read()
if tmp!= '':
if int(tmp)==2:
print "Button pressed"
break
t1 = threading.Thread(target = waitForButton)
t1.start()
print "Press button"
t1.join()