-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeExamples.py
More file actions
104 lines (74 loc) · 2.51 KB
/
codeExamples.py
File metadata and controls
104 lines (74 loc) · 2.51 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import time
import board
from robot import Sonar, Motors, Leds, Linesensor, Button, Buzzer
#-------------------------------------------------------------------
# Color LEDs
#-------------------------------------------------------------------
# Make Leds Object
leds=Leds()
# Turn both LEDs red
leds.color(0xff0000)
# Cycle through the rainbow (hue is the angle on the colorwheel)
for i in range(255):
leds.color_hue(i)
time.sleep(0.005)
# Flashing lights
for i in range(10):
leds.left_color(0xff0000)
leds.right_color(0x0000ff)
time.sleep(0.5)
leds.left_color(0x0000ff)
leds.right_color(0xff0000)
time.sleep(0.5)
# Turn off
leds.off()
#-------------------------------------------------------------------
# Ultrasonic Sensor
#-------------------------------------------------------------------
TRIGGER_PIN = board.GP16
ECHO_PIN = board.GP17
# Make the object
sonar=Sonar(TRIGGER_PIN, ECHO_PIN)
# Measure distance
distance = sonar.distance
print(distance)
# Check until distance is less than 5cm, thet turn leds red and stop
leds.color(0x00ff00)
while distance > 5.0:
distance = sonar.distance
print(distance)
time.sleep(0.1)
leds.color(0xff0000)
#-------------------------------------------------------------------
# Line tracking sensor
#-------------------------------------------------------------------
LINE_SENSOR_LEFT_PIN = board.GP27
LINE_SENSOR_RIGHT_PIN = board.GP3
# Making object - for single sensor, make only one object :)
linesensor_right = Linesensor(LINE_SENSOR_RIGHT_PIN)
linesensor_left = Linesensor(LINE_SENSOR_LEFT_PIN)
# Waiting for both sensors to see a line
leds.color(0x00ff00)
while not (linesensor_left.line_detected and linesensor_right.line_detected):
print(f"Sensor left: {linesensor_left.line_detected} - sensor right {linesensor_right.line_detected}")
leds.color(0xff00)
#-------------------------------------------------------------------
# Buttons
#-------------------------------------------------------------------
BUTTON_A_PIN = board.GP20
BUTTON_B_PIN = board.GP21
buttonA = Button(BUTTON_A_PIN)
# Do nothing while the button is not pressed
while not buttonA.clicked():
pass
# Not that the button was pressed print something
print("The button has been pressed")
#-------------------------------------------------------------------
# Motors
#-------------------------------------------------------------------
# Make the object
motors = Motors()
# Drive forward one second and then back
motors.forward(duration=1.0)
motors.backward(duration=1.0)
# Done