#******************************************************************************************************
#תרגיל 6 הדלקת נורה עם 2 לחצנים להאצת והאטת קצב הבהוב
from machine import Pin, Timer
#הגדרת הפינים
buttonRateUp_pin = 26
buttonRateDown_pin = 25
led_pin=13
buttonRateUp = Pin(buttonRateUp_pin, Pin.IN, Pin.PULL_UP)
buttonRateDown = Pin(buttonRateDown_pin, Pin.IN, Pin.PULL_UP)
led=Pin(led_pin, Pin.OUT)
#משתנים לשמירת מצב הלחצנים
buttonRateUp_state=1
buttonRateDown_state=1
#אתחול מצב הלד
led.value(0)
# טיימרים
debounce_timer = Timer(0)
blink_timer = Timer(1)
DEBOUNCE_MS = 50
Blink_Ms=1000
# פונקציה לביצוע לאחר debounce לכפתור RateUp
def debounce_buttonRateUp(timer):
global buttonLedOn_state
new_state = buttonLedOn.value()
if new_state != buttonLedOn_state:
buttonLedOn_state = new_state
if new_state == 0:
print("כפתור RateUp נלחץ")
Blink_Ms=Blink_Ms*3/2
blink_timer.init(period=Blink_Ms, mode=Timer.PERIODIC, callback=handleBlink)
else:
print("כפתור RateUp שוחרר")
# פונקציה לביצוע לאחר debounce לכפתור RateDown
def debounce_buttonRateDown(timer):
global buttonLedOff_state
new_state = buttonLedOff.value()
if new_state != buttonLedOff_state:
buttonLedOff_state = new_state
if new_state == 0:
print("כפתור RateDown נלחץ")
Blink_Ms=Blink_Ms*2/3
blink_timer.init(period=Blink_Ms, mode=Timer.PERIODIC, callback=handleBlink)
else:
print("כפתור RateDown שוחרר")
# פונקציות interrupt – רק מפעילות את הטיימר המתאים
def buttonRateUp_handler(pin):
debounce_timerLedOn.init(mode=Timer.ONE_SHOT, period=DEBOUNCE_MS, callback=debounce_buttonRateUp)
def buttonRateDown_handler(pin):
debounce_timerLedOff.init(mode=Timer.ONE_SHOT, period=DEBOUNCE_MS, callback=debounce_buttonRateUp)
#פונקציה לביצוע הבהוב נורה
def handleBlink(Timer):
led1.value(not led1.value())
print (led.value())
# הגדרת ה-interrupts
buttonRateUp.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=buttonRateUp_handler)
buttonRateDown.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=buttonRateDown_handler)