Hi to all, allow me a small intro : i am self taught in programing , first learn to program in "c" (arduino) and later playing with a raspberry pi board i start reading in internet articles etc about python programming.
i have written a python program to function a system with motor-belt movement and works fine as i wanted without any problem, so not need to change anything.
The problem is that because i was familiar with c coding it was easier for me to use global variables to my code that is something i read a lot that is not the right way to do with python
i wrote a simple python code that i am using some global variables, so if any one has the free time to alter the code to see how should be if was written using local variables .
i have seen some online examples how to use local variables but havent understand how could be applied to my code so that is why i wrote the code above to understand it as an example. (i have some variables that are used in a lot of different Functions and can be changed also inside these functions )
Description of the code : At 1st boot moves the belt to the left , after that waits to import/type the position want to move
Here is the example Code :
Ps: English is not my native language
i have written a python program to function a system with motor-belt movement and works fine as i wanted without any problem, so not need to change anything.
The problem is that because i was familiar with c coding it was easier for me to use global variables to my code that is something i read a lot that is not the right way to do with python
i wrote a simple python code that i am using some global variables, so if any one has the free time to alter the code to see how should be if was written using local variables .
i have seen some online examples how to use local variables but havent understand how could be applied to my code so that is why i wrote the code above to understand it as an example. (i have some variables that are used in a lot of different Functions and can be changed also inside these functions )
Description of the code : At 1st boot moves the belt to the left , after that waits to import/type the position want to move
Here is the example Code :
# Virtual mode of a motor moving a belt in 3 positions, 1 Start/left, 2 Middle, 3 End/Right # Normal code is based to work with the physical world with the help of Raspberry GPIO pins for switches inputs ( switches for 3 positions left middle and right ) # This one is simplified to bare minimal (no import for gpios etc ) to work in any system with Python installed import time Position = "" call = "" def stop(): print("Motor stopped") if call == "1" : print("Belt at Left Position") if call == "2" : print("Belt at Middle Position") if call == "3" : print("Belt at Right Position") time.sleep(2.0) main() def Left(): global Position command = input("Type stop to stop the motor : ") while command != "stop" : try : print("keep moving to : " + call) command = input("Type stop to stop the motor : ") except : pass if command == "stop" : print("Slow down motor") Position = "Left" stop() def Middle(): global Position command = input("Type stop to stop the motor : ") while command != "stop": try: print("keep moving to : " + call) command = input("Type stop to stop the motor : ") except: pass if command == "stop": print("Slow down motor") Position = "Middle" stop() def Right(): global Position command = input("Type stop to stop the motor : ") while command != "stop": try: print("keep moving to : " + call) command = input("Type stop to stop the motor : ") except: pass if command == "stop": print("Slow down motor") Position = "Right" stop() def main(): global call print("###########################################################") print(" ") print("type : '1' = Move Left , '2' = Move middle , '3' = Move Right , 'Q' = Quit ") while True: call = input("Please type something : ") if call == "1" and Position != "Left": print("call is: " + call) Left() elif call == "1" and Position == "Left" : print("You are All ready on " + Position) if call == "2" and Position != "Middle": print("call is: " + call) Middle() elif call == "2" and Position == "Middle": print("You are All ready on " + Position) if call == "3" and Position != "Right": print("call is: " + call) Right() elif call == "3" and Position == "Right": print("You are All ready on " + Position) if call == "Q" : Quit() if call != "1" or call != "2" or call != "3" or call != "Q" : print("Wrong Type Command, Please try again") def first_boot(): global Position print(">>>>>>>>>>>>>>>>--<<<<<<<<<<<<<<<<<<<<") print(">>>>>>>>>>>>>>>>--<<<<<<<<<<<<<<<<<<<<") print(" ") print("Booting for 1st time") print("moving motor - belt to the left position - calibrate") time.sleep(3.5) print("OK !! LEFT position....... Starting main function") Position = "Left" time.sleep(2) def Quit(): print("Thanks for using me, Have a nice Day !!") quit() first_boot() main()Thanks in Advance
Ps: English is not my native language