from machine import Pin
from time import sleep
import time
# Define the GPIO pins for the segments (A to G)
segments = [Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(4, Pin.OUT), Pin(5, Pin.OUT), Pin(6, Pin.OUT), Pin(7, Pin.OUT), Pin(8, Pin.OUT)]
# Control pins for multiplexing the two displays (common cathodes)
display2 = Pin(18, Pin.OUT) # Control first display (units)
display1 = Pin(19, Pin.OUT) # Control second display (tens)
# Define pushbuttons
button_a = Pin(11, Pin.IN, Pin.PULL_DOWN) # Button A for increment
button_b = Pin(12, Pin.IN, Pin.PULL_DOWN) # Button B for decrement
# Number patterns for each digit on seven-segment display (0-9)
digits = [
[1, 1, 1, 1, 1, 1, 0], # 0
[0, 1, 1, 0, 0, 0, 0], # 1
[1, 1, 0, 1, 1, 0, 1], # 2
[1, 1, 1, 1, 0, 0, 1], # 3
[0, 1, 1, 0, 0, 1, 1], # 4
[1, 0, 1, 1, 0, 1, 1], # 5
[1, 0, 1, 1, 1, 1, 1], # 6
[1, 1, 1, 0, 0, 0, 0], # 7
[1, 1, 1, 1, 1, 1, 1], # 8
[1, 1, 1, 1, 0, 1, 1] # 9
]
# Initial count
count = 0
prev_button_a = 0 # Previous state of Button A
prev_button_b = 0 # Previous state of Button B
# Function to clear all segments (turn off all LEDs)
def clear_segments():
for segment in segments:
segment.value(0)
# Function to display a digit on a specific display (units or tens)
def display_digit(digit, num):
clear_segments() # Clear segments before displaying the new number
for i in range(7):
segments[i].value(digits[num][i]) # Set the segment pattern for the digit
digit.value(1) # Turn on the display (digit1 or digit2)
sleep(0.002) # Small delay for multiplexing
digit.value(0) # Turn off the display after showing the digit
# Segmentleri görüntüleme fonksiyonu
def display_number(number):
tens = number // 10 # Onlar basamağı
ones = number % 10 # Birler basamağı
# Onlar basamağı için display1 aktif
display1.on()
display2.off()
for i in range(7):
segments[i].value(digits[tens][i])
time.sleep(0.005)
# Birler basamağı için display2 aktif
display1.off()
display2.on()
for i in range(7):
segments[i].value(digits[ones][i])
time.sleep(0.005)
# Ana döngü
while True:
# Butonların önceki ve şimdiki durumlarını kontrol et
current_button_a = button_a.value()
current_button_b = button_b.value()
# İki butona aynı anda basıldığında sıfırla
if current_button_a == 1 and current_button_b == 1:
count = 0 # Sayaç sıfırlanır
time.sleep(0.2) # Gürültüyü önlemek için bekleme
# Artış için Button A'nın durum değişikliği kontrolü
elif current_button_a == 1 and prev_button_a == 0:
count = (count + 1) % 100 # Sayaç 0-99 arasında
time.sleep(0.2) # Gürültüyü önlemek için bekleme
# Azalış için Button B'nin durum değişikliği kontrolü
elif current_button_b == 1 and prev_button_b == 0:
count = (count - 1) % 100 # Sayaç 0'ın altına düşmesin
time.sleep(0.2) # Gürültüyü önlemek için bekleme
# Önceki durumları güncelle
prev_button_a = current_button_a
prev_button_b = current_button_b
# Sayacı ekrana yazdır
display_number(count)