forked from michaelliao/learn-python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (43 loc) · 1.28 KB
/
main.py
File metadata and controls
51 lines (43 loc) · 1.28 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
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
# Write your program here
motor = Motor(Port.B)
ultrasonic = UltrasonicSensor(Port.S4)
brick.sound.beep()
brick.light(None)
speed = 0
colors = [None, Color.GREEN, Color.YELLOW, Color.RED]
def setSpeed(acc):
global speed
if acc < 0:
speed = max(0, speed - 1)
elif acc > 0:
speed = min(3, speed + 1)
else:
speed = 0
if speed > 0:
motor.run(speed * 90)
else:
motor.stop()
brick.light(colors[speed])
while True:
if not any(brick.buttons()):
wait(10)
else:
if Button.LEFT in brick.buttons():
setSpeed(-1)
elif Button.RIGHT in brick.buttons():
setSpeed(1)
elif Button.CENTER in brick.buttons():
setSpeed(0)
elif Button.UP in brick.buttons():
setSpeed(0)
break
wait(500)
if ultrasonic.distance() < 200:
setSpeed(0)
brick.sound.beep()