from machine import Pin, ADC
from time import sleep
x_axis = ADC(Pin(26))
y_axis = ADC(Pin(27))
led1 = Pin(21, Pin.OUT)
led2 = Pin(20, Pin.OUT)
led3 = Pin(19, Pin.OUT)
led4 = Pin(17, Pin.OUT)
buzzer = Pin(10, Pin.OUT)
com1 = Pin(8, Pin.OUT)
com2 = Pin(9, Pin.OUT)
seg = [0, 1, 2, 3, 4, 5, 6]
segs = [Pin(pin, Pin.OUT) for pin in seg]
digits = [
[0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0]
]
def reead():
x_value = x_axis.read_u16()
y_value = y_axis.read_u16()
return x_value, y_value
def display_number(num):
pattern = digits[num]
for i in range(7):
segs[i].value(pattern[i])
step = 0
counter = 0
while True:
x_value, y_value = reead()
led1.value(0)
led2.value(0)
led3.value(0)
led4.value(0)
if x_value < 20000:
led3.value(1)
elif x_value > 50000:
led1.value(1)
elif y_value < 2000:
led2.value(1)
elif y_value > 50000:
led4.value(1)
if step == 0:
val1 = 0
val2 = 1
elif step == 1:
val1 = 2
val2 = 3
elif step == 2:
val1 = 4
val2 = 5
elif step == 3:
val1 = 6
val2 = 7
elif step == 4:
val1 = 8
val2 = 9
elif step == 5:
val1 = 9
val2 = 9
# إطفاء الشاشتين معاً قبل كتابة الرقم لمنع التداخل (Ghosting)
com1.value(0)
com2.value(0)
display_number(val1)
com1.value(1) # تشغيل الشاشة الأولى فقط
# تشغيل البازر مع الشاشة الأولى لعمل تردد سريع (نغمة أفضل)
if val1 == 9 and val2 == 9:
buzzer.value(1)
else:
buzzer.value(0)
sleep(0.002)
# إطفاء الشاشتين معاً قبل كتابة الرقم الثاني
com1.value(0)
com2.value(0)
display_number(val2)
com2.value(1) # تشغيل الشاشة الثانية فقط
# إطفاء البازر مع الشاشة الثانية لإكمال التردد
if val1 == 9 and val2 == 9:
buzzer.value(0)
sleep(0.002)
counter = counter + 1
if counter == 150: # للتحكم في سرعة الانتقال بين الأرقام
counter = 0
if step < 5:
step = step + 1