Created
February 10, 2023 03:19
-
-
Save CountParadox/01b1055b2b9777f77e6690c1ac19aa8e 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 time | |
import digitalio | |
import board | |
import busio | |
# Initialize the buttons | |
buttons = [digitalio.DigitalInOut(board.GPIO0), digitalio.DigitalInOut(board.GPIO1)] | |
# Initialize the LEDs | |
leds = [digitalio.DigitalInOut(board.GPIO2), digitalio.DigitalInOut(board.GPIO3), | |
digitalio.DigitalInOut(board.GPIO4), digitalio.DigitalInOut(board.GPIO5)] | |
# Set the button direction and pull-up resistance | |
for button in buttons: | |
button.direction = digitalio.Direction.INPUT | |
button.pull = digitalio.Pull.UP | |
# Set the LED direction | |
for led in leds: | |
led.direction = digitalio.Direction.OUTPUT | |
# Initialize the serial port | |
uart = busio.UART(board.TX, board.RX, baudrate=9600) | |
# Define the button states | |
states = [0, 0] | |
while True: | |
for i, button in enumerate(buttons): | |
# Check the button | |
if not button.value: | |
# Change the state | |
states[i] = not states[i] | |
# Turn on or off the LEDs | |
leds[2 * i].value = states[i] | |
leds[2 * i + 1].value = not states[i] | |
# Determine the serial command based on the LED state | |
if leds[2 * i].value: | |
command = "M" + str(i) + "0" | |
else: | |
command = "M" + str(i) + "1" | |
# Send the serial command | |
uart.write(command.encode()) | |
# Wait for the button release | |
while not button.value: | |
time.sleep(0.01) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment