Merge two while loops - Raspberry Pi Forums
i have 2 scripts using gpio ports operate leds various signals on oo model railway. each script uses while loop , works well. wish combine them 1 script run simultaneously, hours of reading , trawling through web has been fruitless.
here 2 scripts , how combine them appreciated. thank in advance, rob
#!/usr/bin/python
#script operate level crossing lights
import rpi.gpio gpio
time import sleep
import atexit
gpio.setmode( gpio.board ) ; gpio.setup( 13 , gpio.out )
gpio.setup( 11 , gpio.out ) ; gpio.setup( 7 , gpio.out )
gpio.setup( 3 , gpio.in ) ; gpio.setup( 5 , gpio.in )
def cleanup() :
print( 'goodbye' )
gpio.cleanup()
atexit.register( cleanup )
red_flashing = false
def action() :
if red_flashing :
gpio.output(7, true ) ; sleep( 0.2 )
gpio.output(7, false )
gpio.output(11, false )
gpio.output(13, true ) ; sleep( 0.2 )
gpio.output(13, false )
else :
gpio.output(7, false )
gpio.output(11, true )
gpio.output(13, false )
print( 'push buttons start/stop led sequence' )
print( 'press ctrl+c exit' )
try :
while true :
if (gpio.input( 3 ) , gpio.input( 5 ) ) :
action()
elif gpio.input( 3 ) :
red_flashing = true
print( 'red lights flashing...'$
elif gpio.input( 5 ) :
red_flashing = false
print( 'green light...' )
$
except keyboardinterrupt :
print( '\nscript exited.' )
and
#!/usr/bin/python
#script operate red or green light using 2 push switches
import rpi.gpio gpio
import time
import atexit
gpio.setmode(gpio.board)
gpio.setup( 16 , gpio.out ) ; gpio.setup( 15 , gpio.out )
gpio.setup(12, gpio.in, pull_up_down=gpio.pud_up)
gpio.setup(22, gpio.in, pull_up_down=gpio.pud_up)
def cleanup() :
print( 'goodbye' )
gpio.cleanup()
atexit.register( cleanup )
red = false
def action() :
if red :
gpio.output(16, true ) ;
gpio.output(15, false )
else :
gpio.output(16, false )
gpio.output(15, true )
print( 'push buttons start/stop led sequence' )
print( 'press ctrl+c exit' )
try :
while true :
if (gpio.input( 12 ) , gpio.input( 22 ) ) :
action()
elif gpio.input( 12 ) :
red = true
print( 'red light...' )
elif gpio.input( 22 ) :
red = false
print( 'green light...' )
except keyboardinterrupt :
print( '\nscript exited.' )
here 2 scripts , how combine them appreciated. thank in advance, rob
#!/usr/bin/python
#script operate level crossing lights
import rpi.gpio gpio
time import sleep
import atexit
gpio.setmode( gpio.board ) ; gpio.setup( 13 , gpio.out )
gpio.setup( 11 , gpio.out ) ; gpio.setup( 7 , gpio.out )
gpio.setup( 3 , gpio.in ) ; gpio.setup( 5 , gpio.in )
def cleanup() :
print( 'goodbye' )
gpio.cleanup()
atexit.register( cleanup )
red_flashing = false
def action() :
if red_flashing :
gpio.output(7, true ) ; sleep( 0.2 )
gpio.output(7, false )
gpio.output(11, false )
gpio.output(13, true ) ; sleep( 0.2 )
gpio.output(13, false )
else :
gpio.output(7, false )
gpio.output(11, true )
gpio.output(13, false )
print( 'push buttons start/stop led sequence' )
print( 'press ctrl+c exit' )
try :
while true :
if (gpio.input( 3 ) , gpio.input( 5 ) ) :
action()
elif gpio.input( 3 ) :
red_flashing = true
print( 'red lights flashing...'$
elif gpio.input( 5 ) :
red_flashing = false
print( 'green light...' )
$
except keyboardinterrupt :
print( '\nscript exited.' )
and
#!/usr/bin/python
#script operate red or green light using 2 push switches
import rpi.gpio gpio
import time
import atexit
gpio.setmode(gpio.board)
gpio.setup( 16 , gpio.out ) ; gpio.setup( 15 , gpio.out )
gpio.setup(12, gpio.in, pull_up_down=gpio.pud_up)
gpio.setup(22, gpio.in, pull_up_down=gpio.pud_up)
def cleanup() :
print( 'goodbye' )
gpio.cleanup()
atexit.register( cleanup )
red = false
def action() :
if red :
gpio.output(16, true ) ;
gpio.output(15, false )
else :
gpio.output(16, false )
gpio.output(15, true )
print( 'push buttons start/stop led sequence' )
print( 'press ctrl+c exit' )
try :
while true :
if (gpio.input( 12 ) , gpio.input( 22 ) ) :
action()
elif gpio.input( 12 ) :
red = true
print( 'red light...' )
elif gpio.input( 22 ) :
red = false
print( 'green light...' )
except keyboardinterrupt :
print( '\nscript exited.' )
python needs code tags when post here because uses indentation , white space avoid using curly braces.rpjg51 wrote: here 2 scripts , how combine them appreciated. thank in advance, rob
#!/usr/bin/python
raspberrypi
Comments
Post a Comment