File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import time
2
+ import board
3
+ import pulseio
4
+ from adafruit_motor import servo
5
+ from adafruit_bluefruit_connect .packet import Packet
6
+ from adafruit_bluefruit_connect .button_packet import ButtonPacket
7
+ from adafruit_ble import BLERadio
8
+ from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
9
+ from adafruit_ble .services .nordic import UARTService
10
+
11
+ ble = BLERadio ()
12
+ uart = UARTService ()
13
+ advertisement = ProvideServicesAdvertisement (uart )
14
+
15
+ pwm = pulseio .PWMOut (board .A3 , duty_cycle = 2 ** 15 , frequency = 50 )
16
+
17
+ my_servo = servo .Servo (pwm )
18
+
19
+ while True :
20
+ print ("WAITING..." )
21
+ # Advertise when not connected.
22
+ ble .start_advertising (advertisement )
23
+ while not ble .connected :
24
+ pass
25
+
26
+ # Connected
27
+ ble .stop_advertising ()
28
+ print ("CONNECTED" )
29
+
30
+ # Loop and read packets
31
+ while ble .connected :
32
+ if uart .in_waiting :
33
+ packet = Packet .from_stream (uart )
34
+ if isinstance (packet , ButtonPacket ):
35
+ # if buttons in the app are pressed
36
+ if packet .pressed :
37
+ if packet .button == ButtonPacket .DOWN :
38
+ print ("pressed down" )
39
+ for angle in range (90 , 170 , 90 ): # 90 - 170 degrees, 90 degrees at a time.
40
+ my_servo .angle = angle
41
+ time .sleep (0.05 )
42
+ if packet .button == ButtonPacket .UP :
43
+ print ("pressed up" )
44
+ for angle in range (170 , 90 , - 90 ): # 170 - 90 degrees, 9 degrees at a time.
45
+ my_servo .angle = angle
46
+ time .sleep (0.05 )
47
+ # Disconnected
48
+ print ("DISCONNECTED" )
You can’t perform that action at this time.
0 commit comments