Repository navigation
Expand file tree
/
Copy pathmicrobit_main.py
More file actions
81 lines (58 loc) · 2.01 KB
/
Copy pathmicrobit_main.py
File metadata and controls
81 lines (58 loc) · 2.01 KB
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
from microbit import *
import distance
import neopixel
# MICROBIT PROJECT
# Pin 1: Ultrasonic Distance Sensor
# Pin 2: LED Bulb
# Pin 16: Fan
distance_sensor = distance.DISTANCE(pin1) # Distance sensor on pin 1
np = neopixel.NeoPixel(pin2, 1) # Neopixel LED on pin 2
uart.init(baudrate=9600)
buffer = ""
display.scroll("READY")
def serialconnection():
global buffer # use global buffer to prevent that error of "buffer" being undefined
data = uart.read()
if data:
buffer += data.decode()
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
line = line.strip()
if "|" not in line:
uart.write("ERROR|Invalid packet\n")
continue
command, value = line.split("|", 1)
if command == "CROWD": # command to display the LED
if value == "LOW":
np.clear()
display.show(Image.HAPPY)
np.fill((0, 255, 0)) # green
np.show()
elif value == "CROWDED":
np.clear()
display.show(Image('00000:'
'09090:'
'00000:'
'99999:'
'00000')) # no expression face
np.fill((255, 165, 0)) # orange
np.show()
elif value == "FULL":
np.clear()
display.show(Image.SAD)
np.fill((255, 0, 0)) # red
np.show()
uart.write("OK|CROWD\n")
elif command == "END": # end program
np.clear()
uart.write("OK|END\n")
else:
uart.write("ERROR|Unknown command\n")
def runfan():
if distance_sensor.get_distance() < 25:
pin16.write_digital(1)
else:
pin16.write_digital(0)
while True:
serialconnection()
runfan()