This now works. I've added a second button to change the sequence of the LEDs. The second button needs to cancel any previous running .py scripts first. Can that be done through a kill all, or is there a better way? How do you assign multiple tasks to a single os.system?
#!/usr/bin/env python
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
GPIO.setup(5, GPIO.IN)
while True:
if ( GPIO.input(6) == False ):
print("Button 1")
os.system("python /home/pi/runmyleds.py")
if ( GPIO.input(5) == False ):
print("Button 2")
os.system("python /home/pi/runmyleds2.py")
The indentation is fine here, it got corrupted in copy/paste.
I'd like a .py to run on a button press, to twinkle some Christmas LEDs Using the code below, it works for a simple "hello world" message, but not to fire of my seasonal lights, it doesn't wait for a button press, it just runs. Nice, but not what I want..... Any ideas as to why, please?
import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
button = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
if (GPIO.input(button)):
# This line works OK when button is pressed.
os.system("python /home/pi/helloworld.py")
# This line [when not commented out!] just runs automatically, a button press is not required.
# os.system("python /home/pi/runmyleds.py")
while True). Please check the proposed edit.