Created
August 5, 2022 16:12
-
-
Save benevpi/6ce6651c9fca4060d50d1eceeba50f55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
from hx711.hx711_pio import HX711_PIO | |
import time | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
keyboard = Keyboard(usb_hid.devices) | |
keyboard_layout = KeyboardLayoutUS(keyboard) | |
jump_key = Keycode.UP_ARROW | |
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.4 | |
last_jump_time = 0 | |
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 | |
for number, sensor in enumerate(sensors): | |
value = sensor.read_raw() | |
running_total += value | |
up_down, spike = spike_detect(running_total, list_total, SIZE, THRESHOLD) | |
if(spike and not up_down): | |
print("jumped") | |
keyboard.press(jump_key) | |
time.sleep(0.1) | |
keyboard.release_all() | |
time.sleep(DEBOUNCE) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment