Python Forum

Full Version: raspberry pico second core ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,

why am I working on the 2nd core?
I am working with a stepper motor that makes a movement from left to right, which takes about 25,000 pulses, 1 pulse is about 1 millisecond.
for every 100 pulses I want to send a message over the UART, which takes about 75 milliseconds.
but the movement of the stepper motor must remain smooth.
so I think: I send a start veriable every 100 pulses to the 2nd core, and let the message be sent there.
I think that should just work.
I am testing that in the code below.
Now I actually have 2 questions:
1 - Does that work with the method I want to use?
2 - I get an error message in this code: where is the error comming from ? the global with test is working fine.
thank you in advance.
Traceback (most recent call last):
  File "<stdin>", line 33, in <module>
SyntaxError: identifier redefined as global
from machine import Pin
import machine
import utime
import time
import _thread

button=Pin(22, Pin.IN, Pin.PULL_DOWN)

led_15              = Pin(15, Pin.OUT)
led_15. value(0)
led_16              = Pin(16, Pin.OUT)
led_16. value(0)

test = False

def core1_task():
    global test
        
    while True:
        time.sleep_ms (5)
        if test == True:
            led_15. value(1)
            time.sleep_ms (1)
            led_15. value(0)
            print ("meassage frome core_1")
            test = False
        _thread.exit()
        
once = 0

while True:
    global once
    
    led_16. value(1)
    time.sleep_ms (1)
    led_16. value(0)
    time.sleep_ms (5)
    
    if button() == 0 and once == 0:
        once = 1
        test = True
        _thread.start_new_thread(core1_task, ())        
        
    print ("meassage frome core_0")
global is only used inside functions. The purpose of global is to tell python that the variable(s) is in the global scope, and assignment to the variable should not create a local variable inside the function. The code where you use "once" is not inside a function. Any variables assigned in the while loop are global.

Interestingly, this is a syntax error:
once = 0
while True:
    global once
    if once == 0:
But this is not. It raises a NameError when run, but it is not a syntax error.
while True:
    global once
    if once == 0:
Thank you.
thank you