#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    east coast
    Posts
    20
    Rep Power
    0

    Global in python not working as expected


    good day
    i'm new to python and trying to learn how to control an arduino via serial communication, i have everything sorted out but!!!!!! i have three buttons that send commands to my arduino that all works fine, i need a way to lock cretin buttons out after others have been pushed.

    bup = boom up > boom motor ccw
    stop= stop bup and bdown
    bdown= boom down > boom motor cw

    so if bup has been pushed then bdown doesn't do anything untill stop has been pushed if bdown has been pushed then bup doesn't do anything till stop has been pushed.

    i can Wright the code in c on the arduino but i was hoping to be able to do it in python. i tried setting global variables but it doesn't seem to work correctly

    #!/usr/bin/env python
    from Tkinter import *
    from arduinoController import *
    the_port = '/dev/ttyACM0'
    close(the_port)
    init(the_port)
    bup = '0'
    bdown = '0'


    class MyApp(Frame):
    def __init__(self,master=None):
    Frame.__init__(self,master)
    self.grid(sticky=N+S+E+W)
    self.createWidgets()
    def createWidgets(self):
    top=self.winfo_toplevel()
    top.rowconfigure(0,weight=1)
    top.columnconfigure(0,weight=1)
    self.rowconfigure(0,weight=1)
    self.columnconfigure(0,weight=1)

    self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
    self.up.grid(row=1,column=1,sticky=N+S+E+W)

    self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
    self.ccw.grid(row=2,column=0,sticky=N+S+E+W)

    self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
    self.stop.grid(row=2,column=1,sticky=N+S+E+W)

    self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
    self.cw.grid(row=2,column=2,sticky=N+S+E+W)

    self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
    self.down.grid(row=3,column=1,sticky=N+S+E+W)

    self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
    self.wup.grid(row=1,column=4,sticky=N+S+E+W)

    self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
    self.wstop.grid(row=2,column=4,sticky=N+S+E+W)

    self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
    self.wdown.grid(row=3,column=4,sticky=N+S+E+W)


    def bup(self):
    global bdown
    global bup
    if bdown==0:
    bup = 1
    init(the_port)
    output(2, True)
    close(the_port)

    def ccw(self):
    init(the_port)
    output(3, True)
    close(the_port)

    def stop(self):
    global bdown
    global bup
    bdown = 0
    bup = 0
    init(the_port)
    output(2, False)
    output(3, False)
    close(the_port)

    def cw(self):
    init(the_port)
    output(3, False)
    close(the_port)

    def bdown(self):
    global bdown
    global bup
    if bup==0:
    bdown = 1
    init(the_port)
    output(3, True)
    close(the_port)

    def wup(self):
    init(the_port)
    output(2, True)
    time.sleep(1)
    output(3, True)
    close(the_port)

    def wstop(self):
    for x in range(1, 16):
    ser.write("7")

    def wdown(self):
    for x in range(1, 16):
    ser.write("8")



    app = MyApp()
    app.master.title("Serial Adruino Uno ")
    app.mainloop()



    what i tried to do :
    bup =0
    bdown = 0
    stop = bdown and bup set to 0

    is there a easy way to do this in python ?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,710
    Rep Power
    480
    you changed the datatype of your global variables from string to integer. Whatever. This solution doesn't depend on that. Look at the bup method for an idea about solving your problem. self.down.config(state=DISABLED)
    Code:
    #!/usr/bin/env python
    from Tkinter import *
    print('from arduinoController import *')
    the_port = '/dev/ttyACM0'
    print('close(the_port)')
    print('init(the_port)')
    bup = '0'
    bdown = '0'
    
    
    class MyApp(Frame):
    
        def __init__(self,master=None):
            Frame.__init__(self,master)
            self.grid(sticky=N+S+E+W)
            self.createWidgets()
    
        def createWidgets(self):
            top=self.winfo_toplevel()
            top.rowconfigure(0,weight=1)
            top.columnconfigure(0,weight=1)
            self.rowconfigure(0,weight=1)
            self.columnconfigure(0,weight=1)
    
            self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
            self.up.grid(row=1,column=1,sticky=N+S+E+W)
    
            self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
            self.ccw.grid(row=2,column=0,sticky=N+S+E+W)
    
            self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
            self.stop.grid(row=2,column=1,sticky=N+S+E+W)
    
            self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
            self.cw.grid(row=2,column=2,sticky=N+S+E+W)
    
            self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
            self.down.grid(row=3,column=1,sticky=N+S+E+W)
    
            self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
            self.wup.grid(row=1,column=4,sticky=N+S+E+W)
    
            self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
            self.wstop.grid(row=2,column=4,sticky=N+S+E+W)
    
            self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
            self.wdown.grid(row=3,column=4,sticky=N+S+E+W)
    
    
        def bup(self):
            self.down.config(state=DISABLED)
            print('init(the_port)')
            print('output(2, True)')
            print('close(the_port)')
    
        def ccw(self):
            print('init(the_port)')
            print('output(3, True)')
            print('close(the_port)')
    
        def stop(self):
            global bdown
            global bup
            bdown = 0
            bup = 0
            print('init(the_port)')
            print('output(2, False)')
            print('output(3, False)')
            print('close(the_port)')
    
        def cw(self):
            print('init(the_port)')
            print('output(3, False)')
            print('close(the_port)')
    
        def bdown(self):
            global bdown
            global bup
            if bup==0:
                bdown = 1
            print('init(the_port)')
            print('output(3, True)')
            print('close(the_port)')
    
        def wup(self):
            print('init(the_port)')
            print('output(2, True)')
            print('time.sleep(1)')
            print('output(3, True)')
            print('close(the_port)')
    
        def wstop(self):
            for x in range(1, 16):
                print('ser.write("7")')
    
        def wdown(self):
            for x in range(1, 16):
                print('ser.write("8")')
    
    app = MyApp()
    app.master.title("Serial Adruino Uno ")
    app.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    east coast
    Posts
    20
    Rep Power
    0
    good day
    thank you b49P23TIvg

    after butting my head with global i final found that i could make another file and put my variables in there and import the file then Wright back and forth to them.

    but i am going to try this, when importing the variables from another file every time i open the gui and it connects to my arduino i have to click on each stop buttons before it lets me turn anything on.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    east coast
    Posts
    20
    Rep Power
    0

    Global in python not working as expected


    good day
    thank you b49P23TIvg

    got disabling buttons working and it works much better that using variables i'm going to post my working code if others need an example.

    python Code:
     
    #!/usr/bin/env python
    from Tkinter import *
    from arduinoController import * 
    the_port = '/dev/ttyACM0'
    close(the_port)
    init(the_port)
     
     
     
    class MyApp(Frame):
            def __init__(self,master=None):
                    Frame.__init__(self,master)
                    self.grid(sticky=N+S+E+W)
                    self.createWidgets()
            def createWidgets(self):
                    top=self.winfo_toplevel()
                    top.rowconfigure(0,weight=1)
                    top.columnconfigure(0,weight=1)
                    self.rowconfigure(0,weight=1)
                    self.columnconfigure(0,weight=1)
     
                    self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
                    self.up.configure(state='normal')
                    self.up.grid(row=1,column=1,sticky=N+S+E+W)
     
                    self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
                    self.ccw.configure(state='normal')
                    self.ccw.grid(row=2,column=0,sticky=N+S+E+W)
     
                    self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
                    self.stop.grid(row=2,column=1,sticky=N+S+E+W)
     
                    self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
                    self.cw.configure(state='normal')
                    self.cw.grid(row=2,column=2,sticky=N+S+E+W)
     
                    self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
                    self.down.configure(state='normal')
                    self.down.grid(row=3,column=1,sticky=N+S+E+W)
     
                    self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
                    self.wup.configure(state='normal')
                    self.wup.grid(row=1,column=4,sticky=N+S+E+W)
     
                    self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
                    self.wstop.grid(row=2,column=4,sticky=N+S+E+W)
     
                    self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
                    self.wdown.configure(state='normal')
                    self.wdown.grid(row=3,column=4,sticky=N+S+E+W)
     
     
            def bup(self):
    				self.down.configure(state='disabled')					
    				init(the_port)
    				output(2, True)
    				close(the_port)                  
     
            def ccw(self):
    				self.cw.configure(state='disabled')
    				init(the_port)  
    				output(4, True)  
    				close(the_port)
     
            def stop(self):
    				self.up.configure(state='normal')
    				self.down.configure(state='normal')
    				self.cw.configure(state='normal')
    				self.ccw.configure(state='normal')
    				init(the_port)
    				output(2, False)
    				output(3, False)
    				output(4, False)
    				output(5, False)
    				close(the_port)          
     
            def cw(self):
    				self.ccw.configure(state='disabled')						
    				init(the_port)  
    				output(5, True)  
    				close(the_port)
     
            def bdown(self):
    				self.up.configure(state='disabled')
    				init(the_port)
    				output(3, True)
    				close(the_port) 
     
            def wup(self):
    				self.wdown.configure(state='disabled')
    				init(the_port)   
    				output(6, True)
    				close(the_port)
     
            def wstop(self):
                    self.wup.configure(state='normal')
                    self.wdown.configure(state='normal')
                    init(the_port)
                    output(6, False)  
                    output(7, False)
                    close(the_port)
     
            def wdown(self):
    				self.wup.configure(state='disabled')
    				init(the_port)
    				output(7, True)
    				close(the_port)
     
     
     
    app = MyApp()
    app.master.title("Serial Adruino Uno ")
    app.mainloop()




    hay b49P23TIvg i learned how to use code tags too lol

IMN logo majestic logo threadwatch logo seochat tools logo