Thank you. I want the code to be in Python.
Unfortunately I don’t know how to continue with the code example you provided.
How would the While True function look like? Do I use if, elif, and else?
Is there anything I need to import?
In my old program I used this to get the GPIO to work
from gpiozero import Button
Here’s what I had previously.
I created a shell that would start each mode and another shell that would stop it.
mode1_start.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode1
sudo /usr/bin/python3 /home/pi/project/mode1/mode1.py
mode1_stop.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode1
sudo pkill -f -9 mode1.py
mode2_start.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode2
sudo fbi -T 1 -a --noverbose -blend 1000 -t 5 *
mode2_stop.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode2
sudo kill $(pgrep fbi)
mode3_start.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode3
sudo vlc --no-video-title-show -L -f mode3.mp4
mode3_stop.sh
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/bin
cd /home/pi/project/mode3
sudo kill $(pgrep vlc)
And here’s the program, mode.py
from gpiozero import Button
from time import sleep
import subprocess
button1 = Button(2)
button2 = Button(3)
button3 = Button(4)
while True:
if button1.is_pressed:
subprocess.run("sudo /bin/bash /home/pi/project/mode1/start.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode2/stop.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode3/stop.sh", shell=True)
sleep(0.25)
continue
elif button2.is_pressed:
subprocess.run("sudo /bin/bash /home/pi/project/mode1/stop.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode2/start.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode3/stop.sh", shell=True)
sleep(0.25)
continue
elif button3.is_pressed:
subprocess.run("sudo /bin/bash /home/pi/project/mode1/stop.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode2/stop.sh", shell=True)
subprocess.run("sudo /bin/bash /home/pi/project/mode3/start.sh", shell=True)
sleep(0.25)
continue
else:
continue
This kind-of works. If I press button 1, the correct program starts, but it doesnt close when I press button 2 or 3. But if I manually run the mode2/stop.sh then it closes. For some reason the program can’t close it.
If I press button 2 then mode2 starts, and if I press button 1 or 3 then mode2 closes and the other modes open. The call for the stop of mode 1 and 3 doesnt seem to work in the program.
But this is not really the function I wanted. I want only two buttons, Next/Previous.
I’m completely lost