Noob Python Question: loop a script w/ start/stop

Posted on
Wed Apr 16, 2014 8:45 pm
BassMint offline
Posts: 105
Joined: Dec 24, 2013

Noob Python Question: loop a script w/ start/stop

I am new to python. Still in diapers new.
I'm stuck at trying to loop a section of this script with the ability to start and stop it on command.

I am making light sequences for the disco lights and want to loop it. Want to be able to send a command from indigo to start and stop it.

the bottom section is what I want to loop.

any critique on what I'm doing or how to do it better is more than welcome

thanks.

Code: Select all
#!/usr/bin/python

#  Time ---------------------------------------------
from time import sleep


#  Switches ------------------------------------
 
def laser2_off():
   indigo.actionGroup.execute(901866970)
   indigo.server.log("DL: Laser2 off")

def laser2_on():
   indigo.actionGroup.execute(97815650)
   indigo.server.log("DL: Laser2 on")

def laser2_toggle():
   indigo.actionGroup.execute(219863992)
   indigo.server.log("DL: Laser2_toggle")

def Strobe_off():
   indigo.actionGroup.execute(872675280)
   indigo.server.log("DL: Strobe off")
   
def Strobe_on():
   indigo.actionGroup.execute(688533553)
   indigo.server.log("DL: Strobe on")
   
def Strobe_toggle():
   indigo.actionGroup.execute(1101214550)
   indigo.server.log("DL: Strobe toggle")

def Strobe_trigger(arg1):
    indigo.server.log("DL: Strobe trigger")
    indigo.actionGroup.execute(688533553)
    sleep(arg1)
    indigo.actionGroup.execute(872675280)
   

# laser 1, laser2, pins, circle, blacklight, star, mb, fan, fog power, fog trigger
indigo.server.log('light start -----------------------------------')
Strobe_trigger(5)

sleep(10)
laser2_on()

sleep(10)
laser2_off()

sleep(10)
Strobe_trigger(5)

sleep(10)
laser2_on()

sleep(10)
laser2_off()

indigo.server.log(' end -----------------------------------')

Posted on
Wed Apr 16, 2014 9:28 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Noob Python Question: loop a script w/ start/stop

Code: Select all
#!/usr/bin/python

#  Time ---------------------------------------------
from time import sleep


#  Switches ------------------------------------
 
def laser2_off():
   indigo.actionGroup.execute(901866970)
   indigo.server.log("DL: Laser2 off")

def laser2_on():
   indigo.actionGroup.execute(97815650)
   indigo.server.log("DL: Laser2 on")

def laser2_toggle():
   indigo.actionGroup.execute(219863992)
   indigo.server.log("DL: Laser2_toggle")

def Strobe_off():
   indigo.actionGroup.execute(872675280)
   indigo.server.log("DL: Strobe off")
   
def Strobe_on():
   indigo.actionGroup.execute(688533553)
   indigo.server.log("DL: Strobe on")
   
def Strobe_toggle():
   indigo.actionGroup.execute(1101214550)
   indigo.server.log("DL: Strobe toggle")

def Strobe_trigger(arg1):
    indigo.server.log("DL: Strobe trigger")
    indigo.actionGroup.execute(688533553)
    sleep(arg1)
    indigo.actionGroup.execute(872675280)
   

# laser 1, laser2, pins, circle, blacklight, star, mb, fan, fog power, fog trigger
indigo.server.log('light start -----------------------------------')

while True:
 if indigo.variables["stopMyProgram"].value =="stop": break
 Strobe_trigger(5)
 sleep(10)
 laser2_on()

 sleep(10)
 laser2_off()

 sleep(10)
 Strobe_trigger(5)

 sleep(10)
 laser2_on()

 sleep(10)
 laser2_off()

indigo.server.log(' end -----------------------------------')


You have to put it into an external script NOT into the code window in Indigo. There Indigo will kill it after 10 seconds.

create a variable "stopMyProgram" and set it to "stop" to stop


Karl

Posted on
Wed Apr 16, 2014 10:03 pm
BassMint offline
Posts: 105
Joined: Dec 24, 2013

Re: Noob Python Question: loop a script w/ start/stop

kw123 wrote:

You have to put it into an external script NOT into the code window in Indigo. There Indigo will kill it after 10 seconds.

create a variable "stopMyProgram" and set it to "stop" to stop


Karl


I learned about that 10 seconds early.
Could you elaborate a bit more, as I am a bit clueless at this point.

add:
Ok that tidbit might have been what I needed thanks


thanks

Posted on
Wed Apr 16, 2014 11:06 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Noob Python Question: loop a script w/ start/stop

save your script to e.g. desktop, name= test1.py
then see attached click on "choose" and select test1.py
Attachments
script.tiff
script.tiff (87.87 KiB) Viewed 2126 times

Posted on
Wed Apr 16, 2014 11:25 pm
BassMint offline
Posts: 105
Joined: Dec 24, 2013

Re: Noob Python Question: loop a script w/ start/stop

I figured out how to execute the external script. the problem is implementing a stoppable loop.

I originally planned to sequence 5 or 10 minutes, then loop it back.
Just can't get how to word it to do it.
I get the variable thing, change the variable to the check at the start of the loop. Just having a problem with the syntax.

Posted on
Thu Apr 17, 2014 12:26 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Noob Python Question: loop a script w/ start/stop

the code as posted will stop when the variable changes to "stop"


Code: Select all
while True:
   if "something true": break
   rest of loop

next statements


will stop the loop if "something true" . The "break" does it. break goes to the first statement after the loop

Posted on
Thu Apr 17, 2014 11:17 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Noob Python Question: loop a script w/ start/stop

BassMint:

the attached code has a start / stop / and end function


variable stopMyProgram=
start : do your strobing
stop : no strobing
end : exit program

Karl


Code: Select all
#!/usr/bin/python

#  Time ---------------------------------------------
from time import sleep


#  Switches ------------------------------------
 
def laser2_off():
   indigo.actionGroup.execute(901866970)
   indigo.server.log("DL: Laser2 off")

def laser2_on():
   indigo.actionGroup.execute(97815650)
   indigo.server.log("DL: Laser2 on")

def laser2_toggle():
   indigo.actionGroup.execute(219863992)
   indigo.server.log("DL: Laser2_toggle")

def Strobe_off():
   indigo.actionGroup.execute(872675280)
   indigo.server.log("DL: Strobe off")
   
def Strobe_on():
   indigo.actionGroup.execute(688533553)
   indigo.server.log("DL: Strobe on")
   
def Strobe_toggle():
   indigo.actionGroup.execute(1101214550)
   indigo.server.log("DL: Strobe toggle")

def Strobe_trigger(arg1):
    indigo.server.log("DL: Strobe trigger")
    indigo.actionGroup.execute(688533553)
    sleep(arg1)
    indigo.actionGroup.execute(872675280)
   

# laser 1, laser2, pins, circle, blacklight, star, mb, fan, fog power, fog trigger
indigo.server.log('light start -----------------------------------')

#  Switches ------------------------------------
 
def laser2_off():
   indigo.actionGroup.execute(901866970)
   indigo.server.log("DL: Laser2 off")

def laser2_on():
   indigo.actionGroup.execute(97815650)
   indigo.server.log("DL: Laser2 on")

def laser2_toggle():
   indigo.actionGroup.execute(219863992)
   indigo.server.log("DL: Laser2_toggle")

def Strobe_off():
   indigo.actionGroup.execute(872675280)
   indigo.server.log("DL: Strobe off")
   
def Strobe_on():
   indigo.actionGroup.execute(688533553)
   indigo.server.log("DL: Strobe on")
   
def Strobe_toggle():
   indigo.actionGroup.execute(1101214550)
   indigo.server.log("DL: Strobe toggle")

def Strobe_trigger(arg1):
    indigo.server.log("DL: Strobe trigger")
    indigo.actionGroup.execute(688533553)
    sleep(arg1)
    indigo.actionGroup.execute(872675280)
   

# laser 1, laser2, pins, circle, blacklight, star, mb, fan, fog power, fog trigger
indigo.server.log('light start -----------------------------------')

while True:

   if indigo.variables["stopMyProgram"].value =="end": break
   sleep(10)

   if indigo.variables["stopMyProgram"].value =="start":
      while True:
         if indigo.variables["stopMyProgram"].value =="stop": break
         Strobe_trigger(5)
         sleep(10)
         laser2_on()
         sleep(10)
         laser2_off()

         sleep(10)
         Strobe_trigger(5)

         sleep(10)
         laser2_on()

         sleep(10)
         laser2_off()

indigo.server.log(' end -----------------------------------')

Posted on
Thu Apr 17, 2014 7:25 pm
BassMint offline
Posts: 105
Joined: Dec 24, 2013

Re: Noob Python Question: loop a script w/ start/stop

kw123 wrote:
BassMint:

the attached code has a start / stop / and end function


variable stopMyProgram=
start : do your strobing
stop : no strobing
end : exit program



thank you very much Karl.

Posted on
Thu Apr 17, 2014 8:54 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Noob Python Question: loop a script w/ start/stop

have not tested it, just try it, but it should give you the principle of how to do it.

You can get more fancier in your main loop: (next step in learning py)

try:
do your things
except:
exit()

just in case indigo does not answer, the program would crash; the try // except //exit() produces a proper exit.


Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests