Skip to content

Commit 6f2d614

Browse files
authored
Merge pull request adafruit#509 from jedgarpark/ble-light-switch
added in anticipation motion code
2 parents ae287cd + c95a2f9 commit 6f2d614

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

BLE_Crickit_Light_Switch/ble_crickit_light_switch.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,39 @@
2626

2727
UP_ANGLE = 180
2828
NEUTRAL_ANGLE = 120
29-
DOWN_ANGLE = 80
30-
29+
DOWN_ANGLE = 60
3130
crickit.servo_1.angle = NEUTRAL_ANGLE
3231

32+
angle = NEUTRAL_ANGLE # use to track state
33+
34+
print("BLE Light Switch")
35+
print("Use Adafruit Bluefruit app to connect")
3336
while True:
3437
blue_led.value = False
3538
uart_server.start_advertising()
3639

3740
while not uart_server.connected:
3841
# Wait for a connection.
3942
pass
40-
41-
blue_led.value = True
43+
blue_led.value = True # turn on blue LED when connected
4244
while uart_server.connected:
43-
crickit.servo_1.angle = NEUTRAL_ANGLE
4445
if uart_server.in_waiting:
4546
# Packet is arriving.
46-
red_led.value = False
47+
red_led.value = False # turn off red LED
4748
packet = Packet.from_stream(uart_server)
48-
4949
if isinstance(packet, ButtonPacket) and packet.pressed:
50-
red_led.value = True
51-
if packet.button == ButtonPacket.UP:
52-
# The Up button was pressed.
53-
crickit.servo_1.angle = UP_ANGLE
54-
elif packet.button == ButtonPacket.DOWN:
55-
# The Down button was pressed.
56-
crickit.servo_1.angle = DOWN_ANGLE
57-
58-
# Wait a bit before returning to neutral position.
59-
time.sleep(0.25)
50+
red_led.value = True # blink to show a packet has been received
51+
if packet.button == ButtonPacket.UP and angle != UP_ANGLE: # UP button pressed
52+
angle = NEUTRAL_ANGLE - 45 # set anticipation angle, opposite of goal angle
53+
for a in range(angle, UP_ANGLE+1, 1): # anticipation angle, ramp to goal angle
54+
crickit.servo_1.angle = a
55+
time.sleep(0.1) # wait a moment
56+
crickit.servo_1.angle = NEUTRAL_ANGLE # then return to neutral angle
57+
angle = UP_ANGLE # set state to prevent redundant hits
58+
elif packet.button == ButtonPacket.DOWN and angle != DOWN_ANGLE: # DOWN button
59+
angle = NEUTRAL_ANGLE + 45
60+
for a in range(angle, DOWN_ANGLE-1, -1):
61+
crickit.servo_1.angle = a
62+
time.sleep(0.1) # wait a moment
63+
crickit.servo_1.angle = NEUTRAL_ANGLE # then return to neutral angle
64+
angle = DOWN_ANGLE # set state to prevent redundant hits

0 commit comments

Comments
 (0)