-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminibit.py
89 lines (80 loc) · 2.59 KB
/
minibit.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
from microbit import *
import neopixel
from utime import ticks_us, sleep_us
class miniBit:
def __init__(self):
pass
leds = neopixel.NeoPixel(pin13, 4)
def forward(self, speed):
if (speed > 1023) or (speed < 1):
print("Error, speed must be a number 1-1023")
display.scroll("Speed must be a number 1-1023")
else:
pin12.write_analog(speed)
pin8.write_digital(0)
pin16.write_analog(speed)
pin14.write_digital(0)
def backward(self, speed):
if (speed > 1023) or (speed < 1):
print("Error, speed must be a number 1-1023")
display.scroll("Speed must be a number 1-1023")
else:
pin12.write_digital(0)
pin8.write_analog(speed)
pin16.write_digital(0)
pin14.write_analog(speed)
def left(self, speed):
if (speed > 1023) or (speed < 1):
print("Error, speed must be a number 1-1023")
display.scroll("Speed must be a number 1-1023")
else:
pin12.write_analog(speed)
pin8.write_digital(0)
pin16.write_analog(1023)
pin14.write_analog(1023)
def right(self, speed):
if (speed > 1023) or (speed < 1):
print("Error, speed must be a number 1-1023")
display.scroll("Speed must be a number 1-1023")
else:
pin16.write_analog(speed)
pin14.write_digital(0)
pin12.write_analog(1023)
pin8.write_analog(1023)
def stop(self, brake=True):
if brake==True:
pin12.write_analog(1023)
pin8.write_analog(1023)
pin16.write_analog(1023)
pin14.write_analog(1023)
else:
pin12.write_digital(0)
pin8.write_digital(0)
pin16.write_digital(0)
pin14.write_digital(0)
def sonar_cm(self):
pin15.write_digital(1)
sleep_us(10)
pin15.write_digital(0)
while pin15.read_digital() == 0:
pass
start = ticks_us()
while pin15.read_digital() == 1:
pass
end = ticks_us()
echo = end-start
distance = int(0.01715 * echo)
return distance
def sonar_mm(self):
pin15.write_digital(1)
sleep_us(10)
pin15.write_digital(0)
while pin15.read_digital() == 0:
pass
start = ticks_us()
while pin15.read_digital() == 1:
pass
end = ticks_us()
echo = end-start
distance = int(0.01715 * echo*10)
return distance