|
4 | 4 | # Author: Tony DiCola
|
5 | 5 | import board
|
6 | 6 | import adafruit_lis3dh
|
| 7 | +import busio |
| 8 | +import digitalio |
7 | 9 |
|
8 | 10 |
|
9 | 11 | # Uncomment _one_ of the hardware setups below depending on your wiring:
|
10 | 12 |
|
11 | 13 | # Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
|
12 | 14 | # otherwise check I2C pins.
|
13 |
| -import busio |
14 | 15 | if hasattr(board, 'ACCELEROMETER_SCL'):
|
15 | 16 | i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
|
16 |
| - lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) |
| 17 | + int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) |
| 18 | + lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1) |
17 | 19 | else:
|
18 | 20 | i2c = busio.I2C(board.SCL, board.SDA)
|
19 |
| - lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) |
| 21 | + int1 = digitalio.DigitalInOut(board.D10) # Set this to the correct pin for the interrupt! |
| 22 | + lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
20 | 23 |
|
21 | 24 | # Software I2C setup:
|
22 |
| -#import bitbangio |
23 |
| -#i2c = bitbangio.I2C(board.SCL, board.SDA) |
24 |
| -#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) |
| 25 | +# import bitbangio |
| 26 | +# i2c = bitbangio.I2C(board.SCL, board.SDA) |
| 27 | +# lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) |
25 | 28 |
|
26 | 29 | # Hardware SPI setup:
|
27 |
| -#import busio |
28 |
| -#spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
29 |
| -#cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin! |
30 |
| -#lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs) |
31 |
| - |
| 30 | +# import busio |
| 31 | +# spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 32 | +# cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin! |
| 33 | +# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs) |
32 | 34 |
|
33 | 35 | # Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
|
34 |
| -lis3dh.range = adafruit_lis3dh.RANGE_2_G |
| 36 | +lis3dh.range = adafruit_lis3dh.RANGE_8_G |
35 | 37 |
|
36 | 38 | # Set tap detection to double taps. The first parameter is a value:
|
37 | 39 | # - 0 = Disable tap detection.
|
|
43 | 45 | # - 4G = 20-40 threshold
|
44 | 46 | # - 8G = 10-20 threshold
|
45 | 47 | # - 16G = 5-10 threshold
|
46 |
| -lis3dh.set_tap(2, 80) |
| 48 | +lis3dh.set_tap(2, 60) |
47 | 49 |
|
48 |
| -# Loop forever printing if a double tap is detected. A single double tap may cause multiple |
49 |
| -# `tapped` reads to return true so we make sure the last once was False. |
50 |
| -last_tap = False |
| 50 | +# Loop forever printing if a double tap is detected. |
51 | 51 | while True:
|
52 |
| - tap = lis3dh.tapped |
53 |
| - if tap and not last_tap: |
54 |
| - print('Double tap!') |
55 |
| - last_tap = tap |
| 52 | + if lis3dh.tapped: |
| 53 | + print('Tapped!') |
0 commit comments