its still the simpler option , because what ever you do you need to have a program running to check for the button press, if you do that in one program and then use that to call the other programs you are just adding another layer of complexity, doing it all in the one program will allow you to do it from on while true loop.pcmanbob wrote: ↑Wed Mar 27, 2019 6:09 pmI think what you actually need is to have your program running but wait for a button press to execute turning the motor and playing the engine sound.
So the program would work like this.
Set up gpio for output for motor control
Set up gpio for button input
Check button status
If not pressed wait 0.5 seconds and check again
If pressed then
Start motor to spin propeller and play the motor sound
Wait 25 seconds
Stop motor and stop sound
Go back to checking the button.
So the button checking , motor control & play sound all happens within the running program.
Code: Select all
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(21, GPIO.IN, GPIO.PUD_UP) GPIO.setup(25, GPIO.OUT) # this is set up for pwm p = GPIO.PWM(25, 50) # main loop print (" Start program") while True: # loop waiting for button press while GPIO.input(21) == 1: time.sleep(0.2) # here is were you will play your sound file print(" this is were you will play your sound file using which ever program you choose ") # now we start the motor print ("Start motor") p.start(0) for dc in range(100): p.ChangeDutyCycle(dc) time.sleep(0.3) # time delay with motor at full speed time.sleep(5) # end of loop so stop motor print ("loop complete stop motor go back to waiting for button press") p.ChangeDutyCycle(0)