from machine import Pin
from time import sleep, ticks_ms
# 7 Segment Display için GPIO Pin'leri
segments = [
Pin(0, Pin.OUT), # a (GP0)
Pin(1, Pin.OUT), # b (GP1)
Pin(2, Pin.OUT), # c (GP2)
Pin(3, Pin.OUT), # d (GP3)
Pin(4, Pin.OUT), # e (GP4)
Pin(5, Pin.OUT), # f (GP5)
Pin(6, Pin.OUT) # g (GP6)
]
# 7 Segment Display'de her sayıyı temsil eden segment düzeni
digits = [
[0,0,0,0,0,0,1], # 0
[1,0,0,1,1,1,1], # 1
[0,0,1,0,0,1,0], # 2
[0,0,0,0,1,1,0], # 3
[1,0,0,1,1,0,0], # 4
[0,1,0,0,1,0,0], # 5
[0,1,0,0,0,0,0], # 6
[0,0,0,1,1,1,1], # 7
[0,0,0,0,0,0,0], # 8
[0,0,0,1,1,0,0] # 9
]
# Butonlar için GPIO Pin'leri
green_button = Pin(12, Pin.IN, Pin.PULL_UP) # Yeşil buton (GP12)
red_button = Pin(19, Pin.IN, Pin.PULL_UP) # Kırmızı buton (GP19)
# Sayaç başlangıcı
counter = 0
# Son buton durumu, debouncing için
last_green_state = True
last_red_state = True
green_button_last_time = 0 # Yeşil buton için zaman kontrolü
red_button_last_time = 0 # Kırmızı buton için zaman kontrolü
# Debouncing için zaman kontrolü (200ms)
def check_button(button, last_state, last_time):
current_state = button.value()
current_time = ticks_ms()
# Debouncing logic: Check state change and timing
if current_state == 0 and last_state == 1 and (current_time - last_time) > 200: # Button press detected
return True, current_state, current_time
else: # No valid button press
return False, current_state, last_time
# Segmentleri gösteren fonksiyon
def display_digit(digit):
for i in range(7):
segments[i].value(digits[digit][i])
# Ana döngü
while True:
# Yeşil butona basılınca sayacı arttır
green_button_pressed, last_green_state, green_button_last_time = check_button(green_button, last_green_state, green_button_last_time)
if green_button_pressed:
counter += 1
if counter > 9: # Sayacın 9'u geçmesini engelle
counter = 0
display_digit(counter)
# Kırmızı butona basılınca sayacı azalt
red_button_pressed, last_red_state, red_button_last_time = check_button(red_button, last_red_state, red_button_last_time)
if red_button_pressed:
counter -= 1
if counter < 0: # Sayacın 0'ın altına inmesini engelle
counter = 9
display_digit(counter)
sleep(0.1) # Butona sürekli basmayı engellemek için kısa bekleme