0

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()
Jonas
  • 121,568
  • 97
  • 310
  • 388
Ebiram88
  • 3
  • 3

1 Answers1

0

The way you've written your code, you effectively have your while loop fighting your IRQ routines. If you just want the two buttons to act like toggle switches, the easiest solution is probably something like this:

from machine import Pin

LED = Pin(2, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_UP)
button2 = Pin(12, Pin.IN, Pin.PULL_UP)

while True:
    if LED() and not button1():
        print("ON")
        LED(0)
    elif not LED() and not button2():
        print("OFF")
        LED(1)

I've tested this code on Wemos D1 Mini running micropython v1.18.

Note that I've configured the buttons as Pin.PULL_UP, and the switches on my board are connected to ground (so pressing a switch brings the corresponding pin to logic 0).


enter image description here

larsks
  • 277,717
  • 41
  • 399
  • 399
  • I've edited my code you can check it, it seems it's working – Ebiram88 May 04 '22 at 22:41
  • If this answer has helped solve your problem, please click on the checkbox to the left of the answer to mark it as "accepted". This is both a way of saying "thanks" to the person who posts the answer and a way to let other people know the question has been resolved. – larsks May 04 '22 at 22:42