Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Serial Monitor out of sync with playground express?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial Monitor out of sync with playground express?

by michaelw3 on Wed Jul 29, 2020 12:07 pm

I am starting a new project to learn how to use relative timings with monotonic(). To make sure I understand the basics, I tried to write a short script that would blink a pattern with the D13 LED based on the time elapsed since an arbitrary starting point. It should be off for the first 9 seconds, on for the next 11, and then off again, resetting the cycle (eventually this will turn into a more elaborate pattern). I also added print statements to each state to make it easier to track:

Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import time


action_time_1 = 0


led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

while True:

    time_from_action = time.monotonic() - action_time_1
   

    if time_from_action < 9:
        led.value = False
        print(time_from_action)
        print("0000000000000000000000")
    if time_from_action > 9 and time_from_action < 20:
        led.value = True
        print(time_from_action)
        print("1111111111111111111111")
    elif time_from_action > 20:
        led.value = False
        print(time_from_action)
        print("2222222222222222222222")
        action_time_1 = time.monotonic()
    else:
        pass


Everything prints to the serial monitor, the timer resets at 20, and the LED switches on and off (yay!). However, the switching seems to be out of sync with the timer. The LED turns on at around 7 seconds (as displayed on the serial monitor), and then turns off again around 14 seconds. This is true even though the serial prints the 'correct' state (i.e., if it is second 18 it will be printing 18 as the time_from_action and a bunch of 111111111111, even though the LED is off). It seems extremely unlikely that the culprit is that the serial monitor is displaying a count that is delayed by seconds from the count of the board, but I also have no idea what else could be going on. For whatever it is worth, turning on the serial monitor also flips on the fan on my relatively new mac.

I have the current version of the bootloader, circuitpython, and the Mu editor. What am I missing?

thanks!

michaelw3
 
Posts: 54
Joined: Sun Mar 10, 2013 6:58 pm

Re: Serial Monitor out of sync with playground express?

by kevinjwalters on Wed Jul 29, 2020 12:35 pm

You might want to see how it behaves when the loop is slowed down to reduce the amount of data printed over the serial over USB link. Perhaps add a time.sleep(0.25) as an experiment to rule out any buffering/bottleneck issues with serial data.

time.monotonic() starts at 0.0 when the board is powered up, BTW. If you stop the program then restart it from REPL the time will be a larger number. Replacing your initialisation of action_time_1 = 0 with action_time_1 = time.monotonic() might make more sense depending on what you're after?

kevinjwalters
 
Posts: 665
Joined: Sun Oct 01, 2017 3:15 pm

Re: Serial Monitor out of sync with playground express?

by michaelw3 on Wed Jul 29, 2020 1:54 pm

That did it! Plus it makes it much easier to work with because there aren't numbers flying by at a rate that is too fast to really read.

Thank you!

michaelw3
 
Posts: 54
Joined: Sun Mar 10, 2013 6:58 pm

Please be positive and constructive with your questions and comments.