-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotorizedFaderEsp.py
260 lines (198 loc) · 6.4 KB
/
MotorizedFaderEsp.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from machine import Pin, ADC, PWM
from time import sleep
from dcmotor import DCMotor
# -- Raspi_Output=Esp_Input 2-bit Communication -- #
pi_out_bit0 = Pin(23, Pin.IN)
pi_out_bit1 = Pin(22, Pin.IN)
# -- Raspi_Input=Esp_Output 2-bit Communication -- #
pi_in_bit0 = Pin(4, Pin.OUT)
pi_in_bit1 = Pin(0, Pin.OUT)
# -- Poti -- #
wiper = ADC(Pin(34))
wiper.atten(ADC.ATTN_11DB)
wiper.width(wiper.WIDTH_12BIT)
# -- LED's -- #
led_off = Pin(15, Pin.OUT) # red
led_ap = Pin(2, Pin.OUT) # yellow
led_ln = Pin(16, Pin.OUT) # blue
led_fi = Pin(17, Pin.OUT) # green
# -- DC-Motor -- #
# forward = down to off // backwards = up to full_internet
dc_frequency = 75
dc_speed = 100
dc_pin1 = Pin(32, Pin.OUT)
dc_pin2 = Pin(33, Pin.OUT)
enable = PWM(Pin(25), dc_frequency)
dc_motor = DCMotor(dc_pin1, dc_pin2, enable)
# -- Modes Poti Variables -- #
off = 4095
ap = 3000
ln = 1400
fi = 0
def set_led(mode):
if (mode == "off"):
led_off.value(1)
led_ap.value(0)
led_ln.value(0)
led_fi.value(0)
if (mode == "ap"):
led_off.value(0)
led_ap.value(1)
led_ln.value(0)
led_fi.value(0)
if (mode == "ln"):
led_off.value(0)
led_ap.value(0)
led_ln.value(1)
led_fi.value(0)
if (mode == "fi"):
led_off.value(0)
led_ap.value(0)
led_ln.value(0)
led_fi.value(1)
# 00 = offline
wiper_value = wiper.read()
if ((wiper_value < off + off*10/100) and (wiper_value > off - off*10/100)):
pi_in_bit0.value(0)
pi_in_bit1.value(0)
return "off"
# 01 = access point
if ((wiper_value < ap + ap*10/100) and (wiper_value > ap - ap*10/100)):
pi_in_bit0.value(0)
pi_in_bit1.value(1)
return "ap"
# 10 = local network
if ((wiper_value < ln + ln*10/100) and (wiper_value > ln - ln*10/100)):
pi_in_bit0.value(1)
pi_in_bit1.value(0)
return "ln"
# 11 = full internet
if ((wiper_value < fi + fi*10/100) and (wiper_value > fi - fi*10/100)):
pi_in_bit0.value(1)
pi_in_bit1.value(1)
return "fi"
def send_to_raspi(mode):
if (mode == "off"):
pi_in_bit0.value(0)
pi_in_bit1.value(0)
if (mode == "ap"):
pi_in_bit0.value(0)
pi_in_bit1.value(1)
if (mode == "ln"):
pi_in_bit0.value(1)
pi_in_bit1.value(0)
if (mode == "fi"):
pi_in_bit0.value(1)
pi_in_bit1.value(1)
def get_mode_from_slider():
# 00 = offline
wiper_value = wiper.read()
if ((wiper_value < off + off*10/100) and (wiper_value > off - off*10/100)):
return "off"
# 01 = access point
if ((wiper_value < ap + ap*10/100) and (wiper_value > ap - ap*10/100)):
return "ap"
# 10 = local network
if ((wiper_value < ln + ln*10/100) and (wiper_value > ln - ln*10/100)):
return "ln"
# 11 = full internet
if ((wiper_value < fi + fi*10/100) and (wiper_value > fi - fi*10/100)):
return "fi"
def get_mode_from_raspi():
# 00 = offline
if (pi_out_bit0.value() == 0 and pi_out_bit1.value() == 0):
return "off"
# 01 = access point
if (pi_out_bit0.value() == 0 and pi_out_bit1.value() == 1):
return "ap"
# 10 = local network
if (pi_out_bit0.value() == 1 and pi_out_bit1.value() == 0):
return "ln"
# 11 = full internet
if (pi_out_bit0.value() == 1 and pi_out_bit1.value() == 1):
return "fi"
def change_mode(new_mode):
if (new_mode == "off"):
set_offline_slider()
if (new_mode == "ap"):
set_access_point_slider()
if (new_mode == "ln"):
set_local_network_slider()
if (new_mode == "fi"):
set_full_internet_slider()
def set_full_internet_slider():
wiper_value = wiper.read()
print("FULL INTERNET")
if ((wiper_value < fi + fi*10/100) and (wiper_value > fi - fi*10/100)):
return
while(wiper_value < fi):
wiper_value = wiper.read()
dc_motor.forward(dc_speed)
while(wiper_value > fi):
wiper_value = wiper.read()
dc_motor.backwards(dc_speed)
sleep(0.1)
print("-> Wiper Value: " + str(wiper_value) + "\n")
dc_motor.stop()
def set_local_network_slider():
wiper_value = wiper.read()
print("LOCAL NETWORK")
if ((wiper_value < ln + ln*10/100) and (wiper_value > ln - ln*10/100)):
return
while(wiper_value < ln):
wiper_value = wiper.read()
dc_motor.forward(dc_speed)
while(wiper_value > ln):
wiper_value = wiper.read()
dc_motor.backwards(dc_speed)
print("-> Wiper Value: " + str(wiper_value) + "\n")
dc_motor.stop()
def set_access_point_slider():
wiper_value = wiper.read()
print("ACCESS POINT")
if ((wiper_value < ap + ap*10/100) and (wiper_value > ap - ap*10/100)):
return
while(wiper_value < ap):
wiper_value = wiper.read()
dc_motor.forward(dc_speed)
while(wiper_value > ap):
wiper_value = wiper.read()
dc_motor.backwards(dc_speed)
print("-> Wiper Value: " + str(wiper_value) + "\n")
dc_motor.stop()
def set_offline_slider():
wiper_value = wiper.read()
print("OFFLINE")
if (wiper_value == off):
return
if ((wiper_value < off + off*10/100) and (wiper_value > off - off*10/100)):
dc_motor.forward(dc_speed)
while(wiper_value < off):
wiper_value = wiper.read()
dc_motor.forward(dc_speed)
sleep(0.2)
while(wiper_value > off):
wiper_value = wiper.read()
dc_motor.backwards(dc_speed)
print("-> Wiper Value: " + str(wiper_value) + "\n")
dc_motor.stop()
pi_arr = ["timeX", "timeX+1"]
slide_arr = ["timeX", "timeX+1"]
while True:
pi_arr.append(get_mode_from_raspi())
pi_arr.pop(0)
if (pi_arr[0] != pi_arr[1]):
change_mode(pi_arr[1])
set_led(pi_arr[1])
sleep(0.1)
slide_arr.append(get_mode_from_slider())
slide_arr.pop(0)
if (slide_arr[0] != slide_arr[1]):
while (True):
sleep(2)
slide_arr.append(get_mode_from_slider())
slide_arr.pop(0)
if (slide_arr[0] == slide_arr[1]):
set_led(slide_arr[1])
send_to_raspi(slide_arr[1])
break