|
6 | 6 | import board
|
7 | 7 | from digitalio import DigitalInOut, Direction, Pull
|
8 | 8 | from adafruit_funhouse import FunHouse
|
| 9 | +from adafruit_debouncer import Debouncer |
9 | 10 |
|
10 | 11 | i2c = board.I2C()
|
11 | 12 |
|
12 | 13 | RED = 0x200000
|
13 | 14 | GREEN = 0x002000
|
14 | 15 |
|
15 | 16 | funhouse = FunHouse(default_bg=None)
|
16 |
| -funhouse.peripherals.set_dotstars(RED, RED, RED, RED, RED) |
| 17 | +funhouse.peripherals.dotstars.fill(RED) |
17 | 18 |
|
18 |
| -switch = DigitalInOut(board.A1) |
19 |
| -switch.direction = Direction.INPUT |
20 |
| -switch.pull = Pull.UP |
21 |
| - |
22 |
| -# Initialize a new MQTT Client object |
23 |
| -funhouse.network.init_io_mqtt() |
24 |
| - |
25 |
| -print("Connecting to Adafruit IO...") |
26 |
| -funhouse.network.mqtt_connect() |
27 |
| - |
28 |
| -last_door = 1 |
| 19 | +switch_pin = DigitalInOut(board.A1) |
| 20 | +switch_pin.direction = Direction.INPUT |
| 21 | +switch_pin.pull = Pull.UP |
| 22 | +switch = Debouncer(switch_pin) |
29 | 23 |
|
30 | 24 |
|
31 | 25 | def send_io_data(door_value):
|
32 | 26 | funhouse.peripherals.led = True
|
33 | 27 | print("Sending data to adafruit IO!")
|
34 |
| - funhouse.network.mqtt_publish("door", door_value) |
| 28 | + funhouse.network.push_to_io("door", door_value) |
35 | 29 | funhouse.peripherals.led = False
|
36 | 30 |
|
37 | 31 |
|
38 | 32 | send_io_data(0)
|
39 | 33 |
|
40 | 34 | while True:
|
41 | 35 |
|
42 |
| - if switch.value and last_door is 0: |
| 36 | + switch.update() |
| 37 | + if switch.rose: |
43 | 38 | print("Door is open")
|
44 | 39 | funhouse.peripherals.play_tone(2000, 0.25)
|
45 | 40 | funhouse.peripherals.dotstars.fill(RED)
|
46 | 41 | last_door = 1
|
47 | 42 | send_io_data(0)
|
48 | 43 |
|
49 |
| - elif not switch.value and last_door is 1: |
| 44 | + if switch.fell: |
50 | 45 | print("Door is closed")
|
51 | 46 | funhouse.peripherals.play_tone(800, 0.25)
|
52 | 47 | funhouse.peripherals.dotstars.fill(GREEN)
|
|
0 commit comments