Created
March 29, 2024 21:44
-
-
Save Leowbattle/08b9f5bf3b974b2718e346dc2de57c57 to your computer and use it in GitHub Desktop.
Python script for Raspberry Pi Pico for displaying numbers on a 7-segment display. The number shown is incremented every time a button is pushed, and resets back to 0 when it goes above 9.
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
from machine import Pin | |
import utime | |
p = [Pin(i, Pin.OUT) for i in range(7)] | |
button = Pin(28, Pin.IN, Pin.PULL_DOWN) | |
digits = [ | |
0b0111111, | |
0b0000110, | |
0b1011011, | |
0b1001111, | |
0b1100110, | |
0b1101101, | |
0b1111101, | |
0b0000111, | |
0b1111111, | |
0b1100111, | |
] | |
def show_digit(d): | |
if d < 0 or d > 9: | |
raise RangeError() | |
for i in range(7): | |
p[i](digits[d] & (1 << i)) | |
num = 0 | |
show_digit(num) | |
lastval = 0 | |
while True: | |
val = button.value() | |
if val == 1 and lastval == 0: | |
num = (num + 1) % 10 | |
show_digit(num) | |
lastval = val | |
utime.sleep(0.01) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment