Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created August 5, 2022 15:35
Show Gist options
  • Save benevpi/b51ff8cf5e785da20320af206ab269de to your computer and use it in GitHub Desktop.
Save benevpi/b51ff8cf5e785da20320af206ab269de to your computer and use it in GitHub Desktop.
import board
from hx711.hx711_pio import HX711_PIO
import time
print("initialising")
pin_data = board.GP2
pin_clk = board.GP3
hx1 = HX711_PIO(board.GP2, board.GP3, tare=True)
hx2 = HX711_PIO(board.GP6, board.GP7, tare=True)
hx3 = HX711_PIO(board.GP10, board.GP11, tare=True)
hx4 = HX711_PIO(board.GP21, board.GP20, tare=True)
sensors = [hx1, hx2, hx3, hx4]
list1 = []
list2 = []
list3 = []
list4 = []
lists = [list1, list2, list3, list4]
list_total = []
SIZE = 10
THRESHOLD = 200000
DEBOUNCE = 0.8
last_jump_time = 0
for number, this_list in enumerate(lists):
for i in range(SIZE):
this_list.append(sensors[number].read_raw())
for i in range(SIZE):
list_total.append(hx1.read_raw()+ hx2.read_raw()+ hx3.read_raw()+ hx4.read_raw())
def spike_detect(value, value_list, size, threshold):
average = 0
for val in value_list:
average += val
average = average/size
spike = abs(value - average) > threshold
up_down = value > average
value_list.pop(0)
value_list.append(value)
return up_down, spike
print("starting")
while True:
jumped = []
jumped_b = True
running_total = 0
#print("(", end="")
for number, sensor in enumerate(sensors):
value = sensor.read_raw()
running_total += value
#print(value, end=",")
#print("sensor: ", number, " value: ", value)
up_down, spike = spike_detect(value, lists[number], SIZE, THRESHOLD)
#print(up_down, spike)
jumped.append(spike)
if spike:
jumped_b = True
current_time = time.monotonic()
if (current_time - last_jump_time) > DEBOUNCE:
pass
#print("jump")
#last_jump_time = current_time
if not spike:
jumped_b = False
#print(")")
if jumped_b:
pass
# print("jumped")
up_down, spike = spike_detect(running_total, list_total, SIZE, THRESHOLD)
if(spike and not up_down):
now = time.monotonic()
if(now- last_jump_time) > DEBOUNCE:
print("jumped")
last_jump_time = now
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment