-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_control_switch.py
136 lines (106 loc) · 3.67 KB
/
arduino_control_switch.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 25 23:06:06 2016
@author: Kay Schink
"""
import time
import serial
import pyfirmata
try:
board.exit()
except NameError:
""
finally:
""
board = pyfirmata.Arduino('COM3')
counter = 0
# Setup the digital pin on the arduino
digital_0 = board.get_pin('d:12:i')
# this sets up the board
it = pyfirmata.util.Iterator(board)
it.start()
digital_0.enable_reporting()
print "Board opened"
board_closed=False
time.sleep(2)
print "Ready to start perfusion, activate switch to run"
print ""
do_recovery = False
pause_at = [5,10,15,20]
time.sleep(2)
counter = 0
number_of_loops = 25
was_active = False
try:
while (True): # continuous loop, runs forever
if str(digital_0.read()) == 'False': # read out the manual switch
if was_active == True: # this deals with neutralizing after the switch was flipped to "off"
board.digital[2].write(1) # open neutral buffer
print "Neutralizing"
time.sleep(15)
board.digital[2].write(0) # close neutral buffer
print "Run finished"
was_active = False
board.digital[3].write(0) # turn all ports off
board.digital[2].write(0) # turn all ports off
counter = 0 # resets the number of loops run so far
if str(digital_0.read()) == 'True': # manual switch flipped to on
if counter < number_of_loops:
counter = counter+1
was_active = True
# for i in range(0,100):
print "Perfusion running"
print "Loop " + str(counter)
board.digital[3].write(1)
print "Port 2 active"
time.sleep(5)
board.digital[3].write(0)
board.digital[2].write(1)
print "Port 1 active"
time.sleep(5)
board.digital[2].write(0)
if do_recovery: # allows cells to recover after a number of cycles
if counter in pause_at:
print "____________"
print "Recovering"
print "____________"
time.sleep(30)
else:
print "Maximum number of loops reached"
board.digital[3].write(0)
board.digital[2].write(0)
time.sleep(2)
#time.sleep(0.5)
except KeyboardInterrupt:
print "Execution halted, closing all ports"
board.digital[3].write(0)
board.digital[2].write(0)
board.exit()
board_closed=True
print "Board unloaded"
try:
""
board.exit()
except serial.SerialException:
print "Port already closed"
#
#while counter < 1 :
# #pin13 = board.get_pin('d:13:i')
# print board.digital[13].mode()
## if board.digital[13].read()== 1:
## try:
## # for i in range(0,100):
## for j in range(2,10)
## board.digital[j].write(1)
## time.sleep(0.5)
## board.digital[j].write(0)
## #time.sleep(0.5)
## except KeyboardInterrupt:
## print "Execution halted, closing all ports"
## for j in range(2,10):
##
## board.digital[j].write(0)
## board.exit()
#
#
#board.exit()