Created
September 21, 2022 19:16
-
-
Save D2theR/05ea46e990aae973c8752a400a71b15f to your computer and use it in GitHub Desktop.
Raspberry Pi Pico Python Scale Simulator & Button Interface
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
""" | |
BEHOLD!!! | |
A 'SCALE-Y' solution for the Python tinkerer masses! | |
This should work on any Pi model with a little bit of tweaking to | |
the GPIO pins. Read comments below for more information. | |
Feel free to fork and contribute! | |
""" | |
from machine import Timer, Pin, PWM, UART, ADC | |
from time import sleep | |
# On Board LED Blink to show script activity | |
led = Pin(25, Pin.OUT) | |
# These GPIOs are wired to a Storm Interfaces 4 BUTTON SWITCH KEYPAD | |
# https://www.storm-interface.com/pub/media/productattachments/files/File-1396359302.pdf | |
btn_1_up = Pin(1, Pin.IN, Pin.PULL_UP) | |
btn_2_down = Pin(5, Pin.IN, Pin.PULL_UP) | |
btn_3_left = Pin(9, Pin.IN, Pin.PULL_UP) | |
btn_4_right = Pin(13, Pin.IN, Pin.PULL_UP) | |
timer = Timer() | |
# Separate UART GPIO wired into a potentiometer to act as the scale's base. | |
uart = UART(1,115200) | |
adc = ADC(Pin(26)) | |
def blink(timer): | |
led.toggle() | |
timer.init(freq=1, mode=Timer.PERIODIC, callback=blink) | |
# Checks button state for each GPIO using ternary condition statements | |
def button_pressed(btn_1_up, btn_2_down, btn_3_left, btn_4_right): | |
b_list = [] | |
UP = b_list.append("UP") if btn_1_up == 0 else None | |
DOWN = b_list.append("DOWN") if btn_2_down == 0 else None | |
LEFT = b_list.append("LEFT") if btn_3_left == 0 else None | |
RIGHT = b_list.append("RIGHT") if btn_4_right == 0 else None | |
if len(b_list) > 0: | |
return b_list | |
# Starts loop | |
while True: | |
button_state = button_pressed(btn_1_up.value(), btn_2_down.value(), btn_3_left.value(), btn_4_right.value()) | |
if button_state != None: | |
tare = -0.05 | |
net = round((float(adc.read_u16()/65535))*100,2) | |
gross = round(net + tare,2) | |
data_str = f'[{{"tare":{tare},"net":{net},"gross":{gross},"actions":{button_state}}}]' | |
uart.write(data_str) | |
print(data_str) | |
sleep(1) | |
else: | |
tare = -0.05 | |
net = round((float(adc.read_u16()/65535))*100,2) | |
gross = round(net + tare,2) | |
data_str = f'[{{"tare":{tare},"net":{net},"gross":{gross},"actions":[]}}]' | |
uart.write(data_str) | |
print(data_str) | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment