Page 1 of 1

led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 1:09 pm
by Rissy
Why does this script flash the onboard LED on the Pico W, 10 times, instead of 5!?

Code: Select all

import machine
import utime

led = machine.Pin("LED", machine.Pin.OUT)

count = 0
led.off()

while count < 5:
    led.toggle()
    utime.sleep(0.1)
    led.toggle()
    count = count + 1
    utime.sleep(0.5)

Re: led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 2:24 pm
by Rissy
This is my boot.py for the Pico W.

When i run it through Thonny, the LED flashes 10 times.

When i run it outside of Thonny, on boot up, the LED flashes 5 times as programmed.

Has anyone else had something similar this this type of oddity?

Re: led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 3:23 pm
by beetle
Rissy, the code you show flashes the Led 5 times. exactly 5 times. As it happens I was putting the latest micropython on to a new rpi PicoW (V1.19.1) so just incase a bug had crept in as theres no stable release showing as yet I put your code on the board and ran it. 5 nice green flashes. :)

Edit: I see you have been putting in more info since I posted this about the code being in a boot.py. I tried to rename my flashing.py to boot.py but got some errors from Thonny. Possibly something to do with Thonny only having a Pico interpreter setting and not a PicoW interpreter. ??

Re: led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 3:43 pm
by Roberthh
Looks like a Thonny thing, executing boot.py twice, once in raw mode for the board set-up magic it does and once for the regular boot.
So a good rule, if something strange happens is go back to barre bone and kick out any "helpful" tools.

Re: led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 3:53 pm
by Rissy
beetle wrote:
Sat Aug 27, 2022 3:23 pm
Rissy, the code you show flashes the Led 5 times. exactly 5 times. As it happens I was putting the latest micropython on to a new rpi PicoW (V1.19.1) so just incase a bug had crept in as theres no stable release showing as yet I put your code on the board and ran it. 5 nice green flashes. :)

Edit: I see you have been putting in more info since I posted this about the code being in a boot.py. I tried to rename my flashing.py to boot.py but got some errors from Thonny. Possibly something to do with Thonny only having a Pico interpreter setting and not a PicoW interpreter. ??
Does that mean the Pico doesn't have a boot.py privilege then?

Re: led flash 10 times, not 5?

Posted: Sat Aug 27, 2022 3:54 pm
by Rissy
Roberthh wrote:
Sat Aug 27, 2022 3:43 pm
Looks like a Thonny thing, executing boot.py twice, once in raw mode for the board set-up magic it does and once for the regular boot.
So a good rule, if something strange happens is go back to barre bone and kick out any "helpful" tools.
Weird one for people to note, that one is!

Re: led flash 10 times, not 5?

Posted: Fri Sep 02, 2022 11:38 am
by jimmo
Remember that boot.py should only be used to configure hardware that needs to happen before main.py runs (or is required to load main.py, for example configuring filesystems). On the pyboard for example, you must configure the USB mode in boot.py if you want to see the output of main.py on the USB serial.

There are very few reasons to use boot.py on the Pico.

Re: led flash 10 times, not 5?

Posted: Tue Sep 06, 2022 5:30 pm
by dhylands
boot.py will run every time that the micropython board is soft reset, which included entering the RAW REPL. Programs like Thonny, rshell, mpremote all use the raw repl for every command that they execute.

rshell executes about 5 commands when it connects to a board, so your boot.py will get run 5 times. However, main.py doesn't get run when entering the RAW repl, just boot.py.