Guys Hi could you please help me with my project:
I want to turn on an LED with 2 modes: 1-with button one it should starts and after 5 second it should tun off 2-with button two, it should turn on and stays ON and then if I push Button 2 I want it to be turned off. here is my code, I know I should compare different states but I don't understand it, I can use another button, but I like to learn how to use memory.
from machine import Pin, Timer
import time
White_LED = Pin(15, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
def func(pin):
if button2.value() == 1:
White_LED.on()
while True:
button2.irq(func)
if button1.value() == 1:
White_LED.on()
time.sleep(5)
White_LED.off()
I could manage o solve it with two functions now, but the problem is that the button2 won't react as fast as it should and I have to push it couple of times to turn the LED, ON and OFF
from machine import Pin, Timer
import time
White_LED = Pin(15, Pin.OUT)
Blue_LED = Pin(16,Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
def func(pin):
if button2.value() == 1 & White_LED.value()== 0:
White_LED.on()
def func2(pin):
if button2.value() == 1 & White_LED.value()== 1:
White_LED.off()
while True:
button2.irq(func)
button2.irq(func2)
if button1.value() == 1:
White_LED.on()
time.sleep(5)
White_LED.off()
I did this and it seems it's working:
from machine import Pin, Timer
import time
White_LED = Pin(15, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
while True:
if button1.value() == 1:
White_LED.on()
time.sleep(5)
White_LED.off()
elif button2.value() == 1:
if White_LED.value()==0:
time.sleep(2)
White_LED.on()
else:
time.sleep(2)
White_LED.off()