Hi everyone,
I have a small issue. I’m trying to blink an LED with micropyhon without using the sleep() method. I used the time() method, but it is not blinking for the expected delay.
Here is my code:
from machine import Pin from time import time led = Pin(2, Pin.OUT) last_blink_time = 0 delay_time = 0.5 while True: led.value() if (time() - last_blink_time) > delay_time: if led.value() == 1: led.value(0) else: led.value(1) last_blink_time = time()
I will really appreciate your help.
Hi.
I’ve tested your code and it blinks the LED every seconds instead of every half a second.
That happens because the time() method has only second precision. For higher precision you need to use ticks_ms() method, for example. You can read all about this here:
https://docs.micropython.org/en/latest/library/utime.html#utime.time
I hope this helps solving your problem.
Let me know if you need further help.
Regards,
Sara