|
26 | 26 |
|
27 | 27 | UP_ANGLE = 180
|
28 | 28 | NEUTRAL_ANGLE = 120
|
29 |
| -DOWN_ANGLE = 80 |
30 |
| - |
| 29 | +DOWN_ANGLE = 60 |
31 | 30 | crickit.servo_1.angle = NEUTRAL_ANGLE
|
32 | 31 |
|
| 32 | +angle = NEUTRAL_ANGLE # use to track state |
| 33 | + |
| 34 | +print("BLE Light Switch") |
| 35 | +print("Use Adafruit Bluefruit app to connect") |
33 | 36 | while True:
|
34 | 37 | blue_led.value = False
|
35 | 38 | uart_server.start_advertising()
|
36 | 39 |
|
37 | 40 | while not uart_server.connected:
|
38 | 41 | # Wait for a connection.
|
39 | 42 | pass
|
40 |
| - |
41 |
| - blue_led.value = True |
| 43 | + blue_led.value = True # turn on blue LED when connected |
42 | 44 | while uart_server.connected:
|
43 |
| - crickit.servo_1.angle = NEUTRAL_ANGLE |
44 | 45 | if uart_server.in_waiting:
|
45 | 46 | # Packet is arriving.
|
46 |
| - red_led.value = False |
| 47 | + red_led.value = False # turn off red LED |
47 | 48 | packet = Packet.from_stream(uart_server)
|
48 |
| - |
49 | 49 | 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