gospina
Posts: 3
Joined: Thu Jan 17, 2019 8:20 pm

Loop different applications based on GPIO input

Tue Apr 02, 2019 1:36 pm

I'm trying to cycle through several applications/commands based on a GPIO input (buttons I have)
The kicker is that each application may use GPIO inputs as well.

So if I use GPIO 25 for example, each time I press it, it goes on to the next one:

1st app: Play a video
2nd app: display a flying bird (GPIO 24 flaps right wing, 23 flaps left wing)
3rd app: shows weather app (python script) (GPIO 24 changes the city)
4th app: shows system info (node.js script) (GPIO 24 shows different things)
5th app: does a slideshow (GPIO 24 goes next, GPIO 23 goes previous)

Once I get to the 5th app, cycles back to the 1st app, based on GPIO 25.

Now, I get each of these working individually. but can't figure out how to cycle through them.
I was trying to accomplish this via python but once I get into an subprocess app, it stops listening to GPIO 25.

pcmanbob
Posts: 5881
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Loop different applications based on GPIO input

Wed Apr 03, 2019 7:55 pm

From the information supplied, I think you need to continue to monitor gpio 25 for a button press inside each of your functions and then do something if the button is pressed, for example exit the current function and return to the main program loop to act on the button press.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Rabbit_Pi
Posts: 23
Joined: Fri Sep 23, 2016 3:03 am

Re: Loop different applications based on GPIO input

Sun Apr 28, 2019 12:42 am

in your code, make sure you have a true and false statement. I use ruby but its not far off from python.

if pin # == true
led_pin == true
if pin # == false
led_pin == false

setting your code up like so will keep any loops from happening and ensures the correct command is ran for the correct input(button)

you can take your project a step further using kernel commands.

if button_1 == true
`startx`.chomp
else
`kodi`.chomp
end

another way to do it is to make a loop process to check each state of the button.

Here is a driver script i wrote for my pi to add a power button. In the firmware, if you ground pin 5 to gnd (closest is pin 6) it will wake the pi up from the shutdown state, meaning you don't have to disconnect power to it to boot it back up. this script adds the second half to that.

#!/usr/bin/env ruby
require 'rpi_gpio'

$pin = RPi::GPIO.set_numbering :board
$button = $pin.setup 5, :as => :input
$power_led = $pin.setup #{$user_pin} , :as => :output, :initialize => :high

def button_state
$pin.low? 5
end

loop do
button_state
sleep(1)
if button_state == true
$pin.set_low #{$user_pin}
`shutdown -h now`
end

I hope from these examples will help you.

Return to “General programming discussion”