Created
March 15, 2022 10:55
-
-
Save hapiel/9b57df1f235944287e246e5425178e85 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
# Siteswap state machine by Daniel Simu, 2022 | |
# Inspired by my notation research project: http://danielsimu.nl/research/ | |
# | |
# Long press to change amount of balls (default is 3) | |
# Short press to change state. The system only responds if a valid move is made. | |
# | |
# Hardware setup on the Pi Pico: | |
# | |
# Leds on GP pins 16-23, 25 & 26. | |
# Buttons on pin ADC2 (GP28) | |
# Buttons are linked with 10 1k resistors, pulldown is 10k | |
# It could work with different resistor values, | |
# but you may have to puzzle with the numbers in button_val() | |
from machine import Pin, Timer, ADC | |
import time | |
#setup button pin | |
button = ADC(Pin(28)) | |
# setup led pins | |
leds = [] | |
for i in range(9): | |
if i < 7: | |
leds.append(Pin(i + 16, Pin.OUT)) | |
else: | |
leds.append(Pin(i + 19, Pin.OUT)) | |
# functions: | |
def all_off(): | |
for led in leds: | |
led.off() | |
def set_balls(balls): | |
all_off() | |
for i in range(balls): | |
leds[i].on() | |
def button_val(): | |
val = button.read_u16() /65535 * 10 | |
# when the leds are on the values shift in the lower range | |
if 1 < val < 4 : | |
for led in leds: | |
if led.value(): | |
val -= 0.15/(val + 1) | |
val = round(val) | |
return val | |
def shift_values(): | |
for i in range(len(leds)): | |
if i == 0: | |
leds[0].off() | |
time.sleep(0.03) | |
else: | |
if leds[i].value(): | |
leds[i].off() | |
time.sleep(0.05) | |
leds[i-1].on() | |
time.sleep(0.03) | |
time.sleep(0.2) | |
def change_state(sswap): | |
if leds[0].value(): | |
if sswap == 9: | |
shift_values() | |
leds[sswap - 1].on() | |
elif not leds[sswap].value(): | |
shift_values() | |
leds[sswap - 1].on() | |
else: | |
if sswap == 0: | |
shift_values() | |
# begin! | |
set_balls(3) | |
while(True): | |
b_press = button_val() | |
if b_press > 0: | |
# debounce | |
time.sleep(0.02) | |
if b_press == button_val(): | |
hold_time = time.ticks_ms() | |
while(button_val() > 0): | |
if hold_time + 1000 < time.ticks_ms(): | |
set_balls(b_press - 1) | |
time.sleep(0.1) | |
all_off() | |
time.sleep(0.1) | |
set_balls(b_press - 1) | |
time.sleep(0.6) | |
break | |
else: | |
change_state(b_press - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment