Skip to content

Commit 4b9ad57

Browse files
committed
first commit Turtle BLE rover
1 parent 36220d6 commit 4b9ad57

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Circuit Playground Bluefruit Rover
2+
# Use with the Adafruit BlueFruit LE Connect app
3+
# Works with CircuitPython 4.0.0-beta.1 and later
4+
# running on an nRF52840 CPB board and Crickit
5+
6+
import time
7+
import board
8+
import digitalio
9+
import neopixel
10+
from adafruit_crickit import crickit
11+
from adafruit_ble.uart_server import UARTServer
12+
13+
from adafruit_bluefruit_connect.packet import Packet
14+
# Only the packet classes that are imported will be known to Packet.
15+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
16+
from adafruit_bluefruit_connect.color_packet import ColorPacket
17+
18+
# Prep the status LED on the CPB
19+
red_led = digitalio.DigitalInOut(board.D13)
20+
red_led.direction = digitalio.Direction.OUTPUT
21+
22+
uart_server = UARTServer()
23+
24+
# motor setup
25+
motor_1 = crickit.dc_motor_1
26+
motor_2 = crickit.dc_motor_2
27+
28+
FWD = 0.25
29+
REV = -0.25
30+
31+
neopixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1)
32+
RED = (200, 0, 0)
33+
GREEN = (0, 200, 0)
34+
BLUE = (0, 0, 200)
35+
PURPLE = (120, 0, 160)
36+
YELLOW = (100, 100, 0)
37+
AQUA = (0, 100, 100)
38+
BLACK = (0, 0, 0)
39+
color = PURPLE # current NeoPixel color
40+
neopixels.fill(color)
41+
42+
print("BLE Turtle Rover")
43+
print("Use Adafruit Bluefruit app to connect")
44+
while True:
45+
# blue_led.value = False
46+
neopixels[0] = BLACK
47+
neopixels.show()
48+
uart_server.start_advertising()
49+
while not uart_server.connected:
50+
# Wait for a connection.
51+
pass
52+
# set a pixel blue when connected
53+
neopixels[0] = BLUE
54+
neopixels.show()
55+
while uart_server.connected:
56+
if uart_server.in_waiting:
57+
# Packet is arriving.
58+
red_led.value = False # turn off red LED
59+
packet = Packet.from_stream(uart_server)
60+
if isinstance(packet, ColorPacket):
61+
# Change the color.
62+
color = packet.color
63+
neopixels.fill(color)
64+
65+
# do this when buttons are pressed
66+
if isinstance(packet, ButtonPacket) and packet.pressed:
67+
red_led.value = True # blink to show packet has been received
68+
if packet.button == ButtonPacket.UP:
69+
neopixels.fill(color)
70+
motor_1.throttle = FWD
71+
motor_2.throttle = FWD
72+
elif packet.button == ButtonPacket.DOWN:
73+
neopixels.fill(color)
74+
motor_1.throttle = REV
75+
motor_2.throttle = REV
76+
elif packet.button == ButtonPacket.RIGHT:
77+
color = YELLOW
78+
neopixels.fill(color)
79+
motor_2.throttle = 0
80+
motor_1.throttle = FWD
81+
elif packet.button == ButtonPacket.LEFT:
82+
color = YELLOW
83+
neopixels.fill(color)
84+
motor_2.throttle = FWD
85+
motor_1.throttle = 0
86+
elif packet.button == ButtonPacket.BUTTON_1:
87+
neopixels.fill(RED)
88+
motor_1.throttle = 0.0
89+
motor_2.throttle = 0.0
90+
time.sleep(0.5)
91+
neopixels.fill(color)
92+
elif packet.button == ButtonPacket.BUTTON_2:
93+
color = GREEN
94+
neopixels.fill(color)
95+
elif packet.button == ButtonPacket.BUTTON_3:
96+
color = BLUE
97+
neopixels.fill(color)
98+
elif packet.button == ButtonPacket.BUTTON_4:
99+
color = PURPLE
100+
neopixels.fill(color)
101+
# do this when some buttons are released
102+
elif isinstance(packet, ButtonPacket) and not packet.pressed:
103+
if packet.button == ButtonPacket.UP:
104+
neopixels.fill(RED)
105+
motor_1.throttle = 0
106+
motor_2.throttle = 0
107+
if packet.button == ButtonPacket.DOWN:
108+
neopixels.fill(RED)
109+
motor_1.throttle = 0
110+
motor_2.throttle = 0
111+
if packet.button == ButtonPacket.RIGHT:
112+
neopixels.fill(RED)
113+
motor_1.throttle = 0
114+
motor_2.throttle = 0
115+
if packet.button == ButtonPacket.LEFT:
116+
neopixels.fill(RED)
117+
motor_1.throttle = 0
118+
motor_2.throttle = 0

0 commit comments

Comments
 (0)