AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 2:42 am

Hello,

I will first start off by saying I'm new to this thing and don't know a lot. This program is for a project I'm building. :?

So to paint a picture of what I'm wanting to do, I am writing a program that will be for a diorama I'm making of a 1:32 model airplane. My idea is to have the engine start up and run, have lights turn on and then eventually shut down. But I want this program to only run once a button is pressed.

The way I want this to go is as such:
1. Press button
2. Propeller starts turning and sound file playing.
3. program runs until complete
4. Program does not start until button is pressed again.

I have a grasp on how this should work which is I need to have the pi constantly checking to see if the button is pushed, and then once pushed have the program run. But how do I go about doing that? The button and the motor and all the lights will be all connected to different GPIO pins so I'm confused on how to make 2 gpio pins talk to each other as theyre going to need to in order for the program to start properly. Am I over thinking this. :| :?:

Please help thank you :oops:

Andyroo
Posts: 4180
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 2:50 am

I would start with this book https://www.raspberrypi.org/magpi/issue ... o-zero-v1/. It’s available as a free PDF or can be purchased to support the Foundation :D

This will show you how to connect LEDs and buttons and how simple Python programs can detect button presses and set the LEDs on.

Playing sounds can be built around https://projects.raspberrypi.org/en/pro ... -music-box

Not sure on the motor though - depends on the size and speed you need but the above will keep you busy to start with :lol:
Need Pi spray - these things are breeding in my house...

User avatar
rpiMike
Posts: 852
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 6:47 am

Why not have your program wait for the button press. When the button is pressed turn propellor and play sound. Then loop back to waiting for button press.

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

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 9:23 am

You asked the same basic question in this thread https://www.raspberrypi.org/forums/view ... 1&t=236781

back then I suggested the same as rpiMike
pcmanbob wrote:
Wed Mar 27, 2019 6:09 pm
I 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.

Program start
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.
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.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 10:22 pm

I guess I'm just really confused on how to make it do that. Does anyone know I good place to get some tutorials on how to set this program?

User avatar
B.Goode
Posts: 8252
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 10:27 pm

AeroFixerB1 wrote:
Thu May 16, 2019 10:22 pm
I guess I'm just really confused on how to make it do that. Does anyone know I good place to get some tutorials on how to set this program?

The Raspberry Pi Foundation have this documentation:
https://projects.raspberrypi.org/en/pro ... -computing

If that doesn't help there are dozens of similar projects on the same website: https://projects.raspberrypi.org/en/

AeroFixerB1
Posts: 19
Joined: Wed Mar 27, 2019 3:36 am

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 11:46 pm

I guess what I need to know now is how to set up the code. I understand I'm going to need to set up a continuous loop and put in sleep times but i guess with me not being experienced at all with programing i don't how to set it up correctly. I feel the more videos i watch trying to learn how to set this up the more confused and stressed i get about this. :? :oops:

Andyroo
Posts: 4180
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Can Someone Help Me Start My Program With A Button

Thu May 16, 2019 11:51 pm

Best thing is to start simply.

Start with just a LED / resistor and get that to light up. That will get you used to using a breadboard, DuPont wire and checking things.

Then replace that with a small switch and get used to detecting when it is open and closed.

Then put them together so the LED responds when the button is pushed / released

Build up slowly - the more you watch / read the more complex it becomes.

Experiment and have fun - that’s where the Pi shines. Even if you do not use everything, just get a simple starter kit and play :lol:
Need Pi spray - these things are breeding in my house...

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

Re: Can Someone Help Me Start My Program With A Button

Fri May 17, 2019 10:36 am

Try this simple example it will help you get started.

First you need to connect a switch and an LED to your pi gpio like this

Image

then you can run this code

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)
    
 
I have added note and print statements so you can work out what's happening.

hope it helps
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”