I see hundred of ‘ blinking led ‘ and ‘ switch on led with pushbutton ‘ examples. Just a few lines of code. I tried them all and they worked. I’m using Thonny as IDE, micropython and NodeMCU V3. I just can’t combine let’s say 10 lines of code and get them working. I give the following example : I use a timerfunction I found on internet, a function I created myself and the pushbutton example…
Part 1 : I created the def led_ctrl() function that is called by the timer instruction. Result is a blinking led1.
def led_contr():
led1.value(not led1.value())
timer2 = Timer(-1)
timer2.init(period=93, mode=Timer.PERIODIC, callback=lambda t:led_contr())
Part 2 : the pushbutton example. This works fine : led is switched on/off according the button pushed or not. I notice that if I delete the while True cmd and I put the led1.value instruction NOT INDENTED, it doesn’t work.
while True:
led1.value(button.value())
Now, can someone tell me what to do to have the two processes running together.
Hi Ivo.
I’m not sure if I understood your question.
You need the while True statement so that the program keeps running the indented lines that follow.
If you don’t have anything indented after the while True statement, the program will get “blocked” on the while True line and won’t run anything else that follows. To get out of a while True statement, you need a break statement
This article explains pretty well how these loops work: https://realpython.com/python-while-loop/#the-python-break-and-continue-statements
I hope this is clear. I’m not sure if this was the answer you were looking for.
Regards,
Sara