Issues with while loop in async mode of LCD Display

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Issues with while loop in async mode of LCD Display

Post by nikhiledutech » Tue Jan 23, 2018 7:41 am

Hello,
So i am currently interfacing sound sensor with stm32f407 disc board. i am printing Sound detected on LCD when ADC value reach user defined threshold.

Below is my code:
#Water Sensor Interfacing
#Program to detect water
import pyb
from pyb import ADC, LED, Pin, delay
import uasyncio as asyncio
import utime as time
from alcd import LCD, PINLIST

#Initalising ADC
sig = pyb.Pin('PC3',Pin.IN,Pin.PULL_DOWN) #sensor pin init
adc = ADC(sig)
led = pyb.LED(1) #LED Initalising
lcd = LCD(PINLIST, cols = 16) #Initalising LCD


async def sound_sensor():
ADC = adc.read()
while ADC > 200:
led.toggle()
print("Detected")
lcd[0] = "Sound Detected"

pyb.delay(100)
# else:
# led.off()
# lcd[0] = " "


while True:
loop = asyncio.get_event_loop()
loop.run_until_complete(sound_sensor())

In the while loop, the Sound Detected is not getting printed on LCD, but detected gets printed on terminal. If i interchange, while with if condition, its displaying the O/P on lCD.

So anyone know why while loop doesnt print data on LCD ? Or can anyone correct my code.

Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Issues with while loop in async mode of LCD Display

Post by pythoncoder » Tue Jan 23, 2018 10:01 am

The problem here is that your while loop never yields control to the scheduler. Replace pyb.delay(100) with await asyncio.sleep_ms(100).

That allows the scheduler to run the LCD display task while the delay is in progress.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: Issues with while loop in async mode of LCD Display

Post by nikhiledutech » Tue Jan 23, 2018 11:51 am

okay.thank you sir :)

Post Reply