import machine
import utime
import time
# ANODE 0 1 2 3 4 5 6 7 8 9
# SEGCODE = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]
# CLEARSEGCODE = 0x00
# FLOODSEGCODE = 0xff
# CATHODE 0 1 2 3 4 5 6 7 8 9
SEGCODE = [0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90]
CLEARSEGCODE = 0xff
FLOODSEGCODE = 0x00
# Variables
# A_____
# Seven Segment Display A - DS PIN from 74HC595 A
disA_DS = machine.Pin(0, machine.Pin.OUT)
# Seven Segment Display A - STCP PIN from 74HC595 A
disA_STcp = machine.Pin(1, machine.Pin.OUT)
# Seven Segment Display A - SHCP PIN from 74HC595 A
disA_SHcp = machine.Pin(2, machine.Pin.OUT)
# _B____
# Seven Segment Display B - DS PIN from 74HC595 B
disB_DS = machine.Pin(3, machine.Pin.OUT)
# Seven Segment Display B - STCP PIN from 74HC595 B
disB_STcp = machine.Pin(4, machine.Pin.OUT)
# Seven Segment Display B - SHCP PIN from 74HC595 B
disB_SHcp = machine.Pin(5, machine.Pin.OUT)
# __C___
# Seven Segment Display C - DS PIN from 74HC595 C
disC_DS = machine.Pin(6, machine.Pin.OUT)
# Seven Segment Display C - STCP PIN from 74HC595 C
disC_STcp = machine.Pin(7, machine.Pin.OUT)
# Seven Segment Display C - SHCP PIN from 74HC595 C
disC_SHcp = machine.Pin(8, machine.Pin.OUT)
# ___D__
# Seven Segment Display D - DS PIN from 74HC595 D
disD_DS = machine.Pin(9, machine.Pin.OUT)
# Seven Segment Display D - STCP PIN from 74HC595 D
disD_STcp = machine.Pin(10, machine.Pin.OUT)
# Seven Segment Display D - SHCP PIN from 74HC595 D
disD_SHcp = machine.Pin(11, machine.Pin.OUT)
# ____E_
# Seven Segment Display E - DS PIN from 74HC595 E
disE_DS = machine.Pin(16, machine.Pin.OUT)
# Seven Segment Display E - STCP PIN from 74HC595 E
disE_STcp = machine.Pin(17, machine.Pin.OUT)
# Seven Segment Display E - SHCP PIN from 74HC595 E
disE_SHcp = machine.Pin(18, machine.Pin.OUT)
# _____F
# Seven Segment Display E - DS PIN from 74HC595 F
disF_DS = machine.Pin(19, machine.Pin.OUT)
# Seven Segment Display E - STCP PIN from 74HC595 F
disF_STcp = machine.Pin(20, machine.Pin.OUT)
# Seven Segment Display E - SHCP PIN from 74HC595 F
disF_SHcp = machine.Pin(21, machine.Pin.OUT)
# Button B - Yellow - Reset
button2 = machine.Pin(12, machine.Pin.IN)
# Button A - Red - Subtract
button0 = machine.Pin(13, machine.Pin.IN)
# Button C - Green - Add
button1 = machine.Pin(14, machine.Pin.IN)
# LED A - Red
led1 = machine.Pin(15, machine.Pin.OUT)
# Count Variable
count = 0
# Welcome Message
print("Raspberry Pico. Button Counter Led Test.")
print("Press Green Button For Adding To Count")
print("Press Red Button For Subtracting To Count")
print("Press Yellow Button To Reset Count")
# Definitions
# Displays dat(SEGCODE) To 74HC595 3
def h595_shift(ds,STcp,SHcp,segcode):
STcp.low()
time.sleep_ms(1)
for bit in range(7, -1, -1):
SHcp.low()
time.sleep_ms(1)
value = 1 & (segcode >> bit)
ds.value(value)
time.sleep_ms(1)
SHcp.high()
time.sleep_ms(1)
STcp.high()
time.sleep_ms(1)
# Displays dat(int(Number)) To Displays(in segments)
def showNumber(dat):
# Take dat and turn it into string to break into an array
strNumerals = str(dat);
arrNumerals = [char for char in strNumerals]
numeralLen = len(arrNumerals)
A_digit_seg = CLEARSEGCODE
B_digit_seg = CLEARSEGCODE
C_digit_seg = CLEARSEGCODE
D_digit_seg = CLEARSEGCODE
E_digit_seg = CLEARSEGCODE
F_digit_seg = CLEARSEGCODE
# Check How Many Digits
if numeralLen == 1:
# 1 Digit
F_digit_seg = SEGCODE[int(arrNumerals[0])]
elif numeralLen == 2:
# 2 Digit
E_digit_seg = SEGCODE[int(arrNumerals[0])]
F_digit_seg = SEGCODE[int(arrNumerals[1])]
elif numeralLen == 3:
# 3 Digit
D_digit_seg = SEGCODE[int(arrNumerals[0])]
E_digit_seg = SEGCODE[int(arrNumerals[1])]
F_digit_seg = SEGCODE[int(arrNumerals[2])]
elif numeralLen == 4:
# 4 Digit
C_digit_seg = SEGCODE[int(arrNumerals[0])]
D_digit_seg = SEGCODE[int(arrNumerals[1])]
E_digit_seg = SEGCODE[int(arrNumerals[2])]
F_digit_seg = SEGCODE[int(arrNumerals[3])]
elif numeralLen == 5:
# 5 Digit
B_digit_seg = SEGCODE[int(arrNumerals[0])]
C_digit_seg = SEGCODE[int(arrNumerals[1])]
D_digit_seg = SEGCODE[int(arrNumerals[2])]
E_digit_seg = SEGCODE[int(arrNumerals[3])]
F_digit_seg = SEGCODE[int(arrNumerals[4])]
elif numeralLen == 6:
# 6 Digit
A_digit_seg = SEGCODE[int(arrNumerals[0])]
B_digit_seg = SEGCODE[int(arrNumerals[1])]
C_digit_seg = SEGCODE[int(arrNumerals[2])]
D_digit_seg = SEGCODE[int(arrNumerals[3])]
E_digit_seg = SEGCODE[int(arrNumerals[4])]
F_digit_seg = SEGCODE[int(arrNumerals[5])]
else:
# Empty all should be low(cleared)
A_digit_seg = CLEARSEGCODE
B_digit_seg = CLEARSEGCODE
C_digit_seg = CLEARSEGCODE
D_digit_seg = CLEARSEGCODE
E_digit_seg = CLEARSEGCODE
F_digit_seg = CLEARSEGCODE
# Update Each 74HC595 with seleted SEGCODES in order F then E then D then C then B then A
h595_shift(disF_DS, disF_STcp, disF_SHcp, F_digit_seg)
h595_shift(disE_DS, disE_STcp, disE_SHcp, E_digit_seg)
h595_shift(disD_DS, disD_STcp, disD_SHcp, D_digit_seg)
h595_shift(disC_DS, disC_STcp, disC_SHcp, C_digit_seg)
h595_shift(disB_DS, disB_STcp, disB_SHcp, B_digit_seg)
h595_shift(disA_DS, disA_STcp, disA_SHcp, A_digit_seg)
while True:
# If Add Button Is Pressed
if button1.value() == 0:
# Increase Count By 1
count += 1
# Print The Button Message along with count
print("Addition button pressed! ", count, " current count")
# Turn Red Light Off
led1.value(0)
# Update Display
showNumber(count)
# Else If Subtract Button Pressed
elif button0.value() == 0:
# Only Accept The Push If Count Greater Than 0
if count > 0:
# Decrease Count By 1
count -= 1
# Print The Button Message along with count
print("Subtraction button pressed! ", count, " current count")
# Turn Red Light Off
led1.value(0)
# Update Display
showNumber(count)
# Else If Reset Button Pressed
elif button2.value() == 0:
# Only Accept The Push If Count Greater Than 0
if count > 0:
# Set Count To 0
count = 0
# Region Print The Reset Button Message along with count
print("Reset button pressed! ", count, " current count")
# Turn Red Light On
led1.value(0)
# Update Display
showNumber(count)
# DEFAULT: Else If Red Light Is Off
elif led1.value() != 1:
# Update Display
showNumber(count)
# Turn Red Light On
led1.value(1)