global variables

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
El_person
Posts: 4
Joined: Fri Aug 06, 2021 2:24 pm
Location: cairo

global variables

Post by El_person » Fri Aug 06, 2021 2:38 pm

so , i have made this code for my stepper motor control however it is giving me an error that i don't understand

Code: Select all

from machine import pin
delay=50

# define code initial values (pin1,oin2,pin3,pin4,delay)
def set_stepper(v,y,x,z,delay) :
    global delay
    a=Pin(str(v), mode = Pin.OUT)
    b=Pin(str(x), mode = Pin.OUT)
    c=Pin(str(y), mode = Pin.OUT)
    d=Pin(str(z), mode = Pin.OUT)
    global pins
    pins=[a,b,c,d]


def sequence(*pattern):
    for x in range(5):
        pins[x].value(pattern[x])

def step(steps) :

    fullsteps = ((0,1,0,1), (0,1,1,0), (1,0,1,0), (1,0,0,1))
    for i in range(0, steps):
        #looping the the whole sequence once for each step
        for pattern in fullsteps:
            sequence(*pattern)
            time.sleep_ms(delay)
def h_step(steps) :
    halfsteps = ((0,1,0,1), (0,1,0,0), (0,1,1,0), (0,0,1,0),(1,0,1,0), (1,0,0,0), (1,0,0,1), (0,0,0,1))
    #looping the the whole sequence once for each step
    for i in range(0, steps):
        for pattern in halfsteps:
            sequence(*pattern)
            time.sleep_ms(delay)


#def distance() :

# define the number of steps needed to complete the whole axe 
#def callibrate(length,end_pin) :


#def dis_fix() :


set_stepper(P16,P17,P18,P19,50)
step(100000000000000000)
Last edited by jimmo on Fri Aug 06, 2021 3:11 pm, edited 1 time in total.
Reason: Add [code] formatting

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: global variables

Post by Roberthh » Fri Aug 06, 2021 2:49 pm

The global statement had to be inside the function using the global symbols. That way you declare, that these are not local.

El_person
Posts: 4
Joined: Fri Aug 06, 2021 2:24 pm
Location: cairo

Re: global variables

Post by El_person » Fri Aug 06, 2021 2:55 pm

Roberthh wrote:
Fri Aug 06, 2021 2:49 pm
The global statement had to be inside the function using the global symbols. That way you declare, that these are not local.
i have tried adding global to both sequence and step functions however the same error still apears

line 7, in set_stepper
SyntaxError: identifier redefined as global

Code: Select all

def set_stepper(v,y,x,z,delay) :
    global delay
    a=Pin(str(v), mode = Pin.OUT)
    b=Pin(str(x), mode = Pin.OUT)
    c=Pin(str(y), mode = Pin.OUT)
    d=Pin(str(z), mode = Pin.OUT)
    global pins
    pins=[a,b,c,d]


def sequence(*pattern):
    global pins
    for x in range(5):
        pins[x].value(pattern[x])

def step(steps) :
    global delay
    fullsteps = ((0,1,0,1), (0,1,1,0), (1,0,1,0), (1,0,0,1))
    for i in range(0, steps):
        #looping the the whole sequence once for each step
        for pattern in fullsteps:
            sequence(*pattern)
            time.sleep_ms(delay)



User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: global variables

Post by jimmo » Fri Aug 06, 2021 3:20 pm

Code: Select all

def set_stepper(v,y,x,z,delay) :
    global delay
This is an error because "delay" is the name of one of the arguments.

I think it's probably worth clarifying the way "global" works in Python. At a high level, when you write "global x", it tells Python that anywhere you use the name "x" inside the function, it should refer to the global variable "x" rather than making a local variable.

In this case, because it's an argument, "delay" has to be a local and can't be made global.

Most of the confusion with globals in Python appears to come from the fact that if you access a variable that isn't assigned anywhere else in the function (and isn't an argument), then it will access it as a global instead.

So in summary:
- Any time you modify a variable "foo" in a function, and you want that to modify the global variable (not a local variable "foo"), you need to use "global foo".
- If you only access the global variable, then you don't need to use "global foo".

So in your case, in "set_stepper", you need "global pins" (because it modifies the global "pins" variable), but you do not need it in "sequence" (because it only accesses it, not modifies it). "delay" doesn't need to be global anywhere.

El_person
Posts: 4
Joined: Fri Aug 06, 2021 2:24 pm
Location: cairo

Re: global variables

Post by El_person » Fri Aug 06, 2021 6:54 pm

that is my main code without any modifications , it was exactly like ure suggestion but for some reason it still have an error in line 6
File "<stdin>", line 6
SyntaxError: invalid syntax

Code: Select all

from machine import pin
delay=50

# define code initial values (pin1,oin2,pin3,pin4,delay)
def set_stepper(v,y,x,z,del) :
    global delay
    delay=del
    a=Pin(str(v), mode = Pin.OUT)
    b=Pin(str(x), mode = Pin.OUT)
    c=Pin(str(y), mode = Pin.OUT)
    d=Pin(str(z), mode = Pin.OUT)
    global pins
    pins=[a,b,c,d]


def sequence(*pattern):
    for x in range(5):
        pins[x].value(pattern[x])

def step(steps) :
    fullsteps = ((0,1,0,1), (0,1,1,0), (1,0,1,0), (1,0,0,1))
    for i in range(0, steps):
        #looping the the whole sequence once for each step
        for pattern in fullsteps:
            sequence(*pattern)
            time.sleep_ms(delay)
def h_step(steps) :
    halfsteps = ((0,1,0,1), (0,1,0,0), (0,1,1,0), (0,0,1,0),(1,0,1,0), (1,0,0,0), (1,0,0,1), (0,0,0,1))
    #looping the the whole sequence once for each half step
    for i in range(0, steps):
        for pattern in halfsteps:
            sequence(*pattern)
            time.sleep_ms(delay)


#def distance() :

# define the number of steps needed to complete the whole axe
#def callibrate(length,end_pin) :


#def dis_fix() :


set_stepper(P16,P17,P18,P19,50)
step(100000000000000000)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: global variables

Post by jimmo » Fri Aug 06, 2021 11:12 pm

"del" is a reserved keyword in Python

JennaSys
Posts: 33
Joined: Tue Jul 15, 2014 8:29 am
Location: Southern California, US
Contact:

Re: global variables

Post by JennaSys » Sat Aug 07, 2021 12:27 am

When you use the global keyword in a function to declare a variable in that function as being global, the variable must have been previously defined (i.e. assigned a value) at the module level, outside of any functions in that module (in other words, the .py file itself).

In Python, it's the fact that a variable is created outside of any function or class that makes it global, not the global keyword. The global keyword is used inside of a function to let Python know that when you reference a variable in that function, that you want to use the module level variable and not have it create a new local (function level) variable that just happens to have the same name as the one declared outside of the function.

Also, as others have pointed out, you have a variable called delay as one of your function parameters which will interfere with your global variable of the same name. In practice, it is a good idea to not reuse (or shadow) a variable name that has already been used in an outer scope (i.e. a global variable).

Finally, if you are only accessing the value of a global (module level) variable and not changing it, you don't need to declare it as global in your function that is accessing the value (other than maybe to make your intent more explicit for readability purposes). You only need to declare it as global in a function if you need to change the value of the global variable. By doing so, it tells Python to use the module level variable and to not create a new function level (local) variable with the assignment.
John Sheehan

Post Reply