-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
234 lines (216 loc) · 5.48 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import RPi.GPIO as GPIO
import time
import os
import random
clear = lambda: os.system("clear")
GPIO.setmode(GPIO.BOARD)
GPIO.setup([11,12,13,15,16,18], GPIO.OUT)
GPIO.setup([31,32], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
global mode
global auto
global s
mode = 1
auto = 0
s = 1
pin_left = [11,12,13]
pin_right = [15,16,18]
def mode_1(pins):
d = s
GPIO.output(pins, 1)
time.sleep(d)
GPIO.output(pins, 0)
def mode_2(pins):
d = s * 0.3
GPIO.output(pins[0], 1)
time.sleep(d)
GPIO.output(pins[0], 0)
GPIO.output(pins[1], 1)
time.sleep(d)
GPIO.output(pins[1], 0)
GPIO.output(pins[2], 1)
time.sleep(d)
GPIO.output(pins[2], 0)
def mode_3(pins):
d = s * 0.3
GPIO.output(pins[2], 1)
time.sleep(d)
GPIO.output(pins[2], 0)
GPIO.output(pins[1], 1)
time.sleep(d)
GPIO.output(pins[1], 0)
GPIO.output(pins[0], 1)
time.sleep(d)
GPIO.output(pins[0], 0)
def mode_4(pins):
p_1 = GPIO.PWM(pins[0], 50)
p_2 = GPIO.PWM(pins[1], 50)
p_3 = GPIO.PWM(pins[2], 50)
p_1.start(0)
p_2.start(0)
p_3.start(0)
d = s * 0.017
for dc in range(0,101, 5):
p_1.ChangeDutyCycle(dc)
time.sleep(d)
p_1.ChangeDutyCycle(0)
for dc in range(0,101, 5):
p_2.ChangeDutyCycle(dc)
time.sleep(d)
p_2.ChangeDutyCycle(0)
for dc in range(0,101, 5):
p_3.ChangeDutyCycle(dc)
time.sleep(d)
p_3.ChangeDutyCycle(0)
def mode_5(pins):
f = 50
d = s * 0.017
p_1 = GPIO.PWM(pins[2], f)
p_2 = GPIO.PWM(pins[1], f)
p_3 = GPIO.PWM(pins[0], f)
p_1.start(0)
p_2.start(0)
p_3.start(0)
for dc in range(0,101, 5):
p_1.ChangeDutyCycle(dc)
time.sleep(d)
p_1.ChangeDutyCycle(0)
for dc in range(0,101, 5):
p_2.ChangeDutyCycle(dc)
time.sleep(d)
p_2.ChangeDutyCycle(0)
for dc in range(0,101, 5):
p_3.ChangeDutyCycle(dc)
time.sleep(d)
p_3.ChangeDutyCycle(0)
def mode_6(pins):
count = 20
d = s/count
while count != 0:
D = random.choice([0,1,2])
GPIO.output(pins[D], 1)
time.sleep(d)
GPIO.output(pins[D], 0)
count -= 1
def mode_7(pins):
d = s * 0.05
p_1 = GPIO.PWM(pins[0], 50)
p_2 = GPIO.PWM(pins[1], 50)
p_3 = GPIO.PWM(pins[2], 50)
p_1.start(0)
p_2.start(0)
p_3.start(0)
for dc in range(0,101,10):
p_1.ChangeDutyCycle(dc)
p_2.ChangeDutyCycle(dc)
p_3.ChangeDutyCycle(dc)
time.sleep(d)
for dc in range(100, -1, -10):
p_1.ChangeDutyCycle(dc)
p_2.ChangeDutyCycle(dc)
p_3.ChangeDutyCycle(dc)
time.sleep(d)
def mode_8(pins):
count = 50
d = s * 0.5
while count != 0:
D = random.choice([0,1,2,3,4,5])
GPIO.output(pins[D], 1)
time.sleep(d)
GPIO.output(pins[D], 0)
count -= 1
def mode_9(pins):
p_1 = GPIO.PWM(pins[0], 50)
p_2 = GPIO.PWM(pins[1], 50)
p_3 = GPIO.PWM(pins[2], 50)
p_1.start(100)
p_2.start(100)
p_3.start(100)
d = s * 0.017
for dc in range(100,-1, -5):
p_1.ChangeDutyCycle(dc)
time.sleep(d)
for dc in range(100,-1, -5):
p_2.ChangeDutyCycle(dc)
time.sleep(d)
for dc in range(100,-1, -5):
p_3.ChangeDutyCycle(dc)
time.sleep(d)
def left():
if mode == 1:
mode_1(pin_left)
if mode == 2:
mode_2(pin_left)
if mode == 3:
mode_3(pin_left)
if mode == 4:
mode_4(pin_left)
if mode == 5:
mode_5(pin_left)
if mode == 6:
mode_6(pin_left)
if mode == 7:
mode_7(pin_left)
if mode == 8:
pin_left.extend(pin_right)
mode_8(pin_left)
if mode == 9:
mode_9(pin_left)
def right():
if mode == 1:
mode_1(pin_right)
if mode == 2:
mode_2(pin_right)
if mode == 3:
mode_3(pin_right)
if mode == 4:
mode_4(pin_right)
if mode == 5:
mode_5(pin_right)
if mode == 6:
mode_6(pin_right)
if mode == 7:
mode_7(pin_right)
if mode == 8:
pin_left.extend(pin_right)
mode_8(pin_left)
if mode == 9:
mode_9(pin_right)
def callback_off(self):
global auto
if auto:
auto = 0
def display_choices():
print("""
MODE {} IS IN USE
With a timespan of {} seconds
Auto is: {}""".format(mode, s, auto))
print("""
OPTIONS :
[1] For L/R simultanous activation
[2] For L/R Sequential <logic> activation Back to Front
[3] For L/R Sequential <logic> activation Front to Back
[4] For L/R sequential <PWM> activation Back to Front
[5] For L/R sequential <PWM> activation Front to Back
[6] For L/R random activation
[7] For L/R Simultanous <PWM> activation
[8] For ALL OUTPUT RANDOM
[9] For L/R Sequential <PWM> DESactivation Back to Front
""")
if __name__ == "__main__":
try:
GPIO.add_event_detect(31, GPIO.RISING, callback = callback_off, bouncetime = 1000)
#GPIO.add_event_detect(32, GPIO.RISING, callback = callback_mode, bouncetime = 1000)
while True:
clear()
display_choices()
while auto:
right()
left()
else:
clear()
display_choices()
mode = int(input("Mode desired: "))
s = int(input("Timespan desired: "))
auto = 1
except KeyboardInterrupt:
GPIO.cleanup()