• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

combining micropython commands to a real program

Q&A Forum › Category: ESP8266 › combining micropython commands to a real program
0 Vote Up Vote Down
ivo Vonck asked 4 years ago

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.

1 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.