Skip to content

Commit 83333f8

Browse files
authored
Merge pull request #82 from caternuson/iss78
Update shake() method
2 parents a3c33ef + c03cf8b commit 83333f8

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

adafruit_lis3dh.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def acceleration(self) -> AccelerationTuple:
215215
return AccelerationTuple(x, y, z)
216216

217217
def shake(
218-
self, shake_threshold: int = 30, avg_count: int = 10, total_delay: float = 0.1
218+
self, shake_threshold: int = 20, avg_count: int = 10, total_delay: float = 0.1
219219
) -> bool:
220220
"""Detect when the accelerometer is shaken. Optional parameters:
221221
@@ -224,7 +224,7 @@ def shake(
224224
10 is the total acceleration if the board is not
225225
moving, therefore anything less than
226226
10 will erroneously report a constant shake detected.
227-
Defaults to :const:`30`
227+
Defaults to :const:`20`
228228
229229
:param int avg_count: The number of readings taken and used for the average
230230
acceleration. Default to :const:`10`
@@ -234,19 +234,15 @@ def shake(
234234
235235
"""
236236

237-
shake_accel = (0, 0, 0)
237+
xs = ys = zs = 0
238+
delay = total_delay / avg_count
238239
for _ in range(avg_count):
239-
# shake_accel creates a list of tuples from acceleration data.
240-
# zip takes multiple tuples and zips them together, as in:
241-
# In : zip([-0.2, 0.0, 9.5], [37.9, 13.5, -72.8])
242-
# Out: [(-0.2, 37.9), (0.0, 13.5), (9.5, -72.8)]
243-
# map applies sum to each member of this tuple, resulting in a
244-
# 3-member list. tuple converts this list into a tuple which is
245-
# used as shake_accel.
246-
shake_accel = tuple(map(sum, zip(shake_accel, self.acceleration)))
247-
time.sleep(total_delay / avg_count)
248-
avg = tuple(value / avg_count for value in shake_accel)
249-
total_accel = math.sqrt(sum(map(lambda x: x * x, avg)))
240+
x, y, z = self.acceleration
241+
xs += x * x
242+
ys += y * y
243+
zs += z * z
244+
time.sleep(delay)
245+
total_accel = math.sqrt((xs + ys + zs) / avg_count)
250246
return total_accel > shake_threshold
251247

252248
def read_adc_raw(self, adc: Literal[1, 2, 3]) -> int:

examples/lis3dh_shake.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2026 Carter Nelson for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
6+
import board
7+
import busio
8+
9+
import adafruit_lis3dh
10+
11+
# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
12+
# otherwise check I2C pins.
13+
if hasattr(board, "ACCELEROMETER_SCL"):
14+
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
15+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
16+
else:
17+
i2c = board.I2C() # uses board.SCL and board.SDA
18+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
19+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
20+
21+
# Hardware SPI setup:
22+
# spi = board.SPI()
23+
# cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin!
24+
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
25+
26+
# PyGamer or MatrixPortal I2C Setup:
27+
# i2c = board.I2C() # uses board.SCL and board.SDA
28+
# lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
29+
30+
# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
31+
# Increase as needed depending on the expected magintude of shaking.
32+
lis3dh.range = adafruit_lis3dh.RANGE_4_G
33+
34+
# OPTIONAL: Set threshold for shake, default is 20
35+
# SHAKE_THRESH = 20
36+
37+
# Loop forever checking for shake
38+
while True:
39+
# Check for shake
40+
if lis3dh.shake():
41+
print("SHAKE!!!")
42+
# Small delay to keep things responsive but give time for interrupt processing.
43+
time.sleep(0.1)

0 commit comments

Comments
 (0)