Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Code only works in REPL
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Code only works in REPL

by Tengwar3 on Thu Jun 11, 2020 12:26 am

I have a simple code that i'm learning with to run a motor. The code works in REPL but the code.py file does not run the motor. The LED is green though which says the code.py is running. This is on a Seeed XIAO running CircuitPython 5.3.0. The following file is from the readthedocs.io for 5.0x pulseio Module.

Code: Select all | TOGGLE FULL SIZE
import pulseio
from board import *

pwm = pulseio.PWMOut(D13)
pwm.duty_cycle = 2 ** 15

Been working this for a little longer than I'd like to admit. Thank you for you help.

Tengwar3
 
Posts: 1
Joined: Mon Jul 15, 2019 7:40 pm

Re: Code only works in REPL

by kevinjwalters on Thu Jun 11, 2020 9:53 am

One different between REPL and running a program from code.py from the file system is termination. In REPL things are rather open ended, it takes an explicit Control-D from the interactive user to end the session. A python program terminates when it reaches the last line. That termination triggers various peripherals to reset to their default state. It's common to see

Code: Select all | TOGGLE FULL SIZE
while True:
   pass


at the end of these types of program to prevent termination using this infinite while loop. Whilst infinite loops are highly problematic in many cases for general computing, for a dedicated microcontroller sometimes they make sense. You can still interrupt this with Control-C on the serial console over USB.

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

Re: Code only works in REPL

by XuanXL on Sat Sep 12, 2020 10:44 am

kevinjwalters wrote:One different between REPL and running a program from code.py from the file system is termination. In REPL things are rather open ended, it takes an explicit Control-D from the interactive user to end the session. A python program terminates when it reaches the last line. That termination triggers various peripherals to reset to their default state. It's common to see

Code: Select all | TOGGLE FULL SIZE
while True:
   pass


at the end of these types of program to prevent termination using this infinite while loop. Whilst infinite loops are highly problematic in many cases for general computing, for a dedicated microcontroller sometimes they make sense. You can still interrupt this with Control-C on the serial console over USB.


This is such an important basic knowledge, which should have been added in the REPL page of Welcome to Circuitpython! Thanks!

XuanXL
 
Posts: 1
Joined: Sat Sep 12, 2020 10:42 am

Re: Code only works in REPL

by kevinjwalters on Sun Sep 13, 2020 8:45 am

Glad it was useful.

Comparing with other worlds, it's interesting to note that MicroPython seeds this requirement to some extent with the default (board-specific) example in https://python.microbit.org/

Code: Select all | TOGGLE FULL SIZE
# Add your Python code here. E.g.
from microbit import *


while True:
    display.scroll('Hello, World!')
    display.show(Image.HEART)
    sleep(2000)


Arduino world take more of an enforcement approach by hiding C's main from the user and giving them just setup and loop:

Code: Select all | TOGGLE FULL SIZE
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

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

Please be positive and constructive with your questions and comments.