I'm trying to develop a programme that essentially uses a single-digit 7-segment display to count to 5 from 0, and have attached my code so far which works but would like some suggestions on how to make this programme take up fewer lines. Also... is it possible to define the functions for all the numbers in a separate file and then call them in my final counter script?
Apologies if this doesn't make sense - very new to it all!
Code: Select all
import machine
import utime
#The next 8 lines of code assigns each LED to the correct pin on the pico board
dp = machine.Pin(16, machine.Pin.OUT)
a = machine.Pin(0, machine.Pin.OUT)
b = machine.Pin(1, machine.Pin.OUT)
c = machine.Pin(2, machine.Pin.OUT)
d = machine.Pin(3, machine.Pin.OUT)
e = machine.Pin(4, machine.Pin.OUT)
f = machine.Pin(5, machine.Pin.OUT)
g = machine.Pin(6, machine.Pin.OUT)
def off(): # defines a function for the off state
# this allows the code to reset the display between each number
a.value(0)
b.value(0)
c.value(0)
d.value(0)
e.value(0)
f.value(0)
g.value(0)
dp.value(0)
def zero(): # defines the zero state
a.value(1)
b.value(1)
c.value(1)
d.value(1)
e.value(1)
f.value(1)
def one():
b.value(1)
c.value(1)
def two():
a.value(1)
b.value(1)
g.value(1)
e.value(1)
d.value(1)
def three():
a.value(1)
b.value(1)
g.value(1)
c.value(1)
d.value(1)
def four():
b.value(1)
g.value(1)
f.value(1)
c.value(1)
def five():
a.value(1)
f.value(1)
g.value(1)
c.value(1)
d.value(1)
off()
zero()
utime.sleep(1)
off()
one()
utime.sleep(1)
off()
two()
utime.sleep(1)
off()
three()
utime.sleep(1)
off()
four()
utime.sleep(1)
off()
five()