Posts: 2
Joined: Mon Dec 24, 2018 12:26 pm

Start programme with push button

Mon Dec 24, 2018 12:46 pm

Please note I am a very old newbie.
I intend to make an simple burglar alarm but want to start at the beginning to try to understand coding.

My problem:
I have a Python code that is triggered by pressing a push button and prints "Button was pressed". (Code 1)
I also have a code that flashes an LED and starts by pressing F5 on keyboard or "Run" in Thonny Python. (Code 2)
Q1. How do I make Code 1 trigger Code 2?

Q2. How do I get Code 1 (or Thonny) to start at Pi Startup so that it is waiting for the button to be pressed without any extra input from me?

Thanks in anticipation from an old newbie.

Posts: 1705
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Start programme with push button

Tue Dec 25, 2018 3:36 am

Code: Select all

import os
os.system('python hello.py')
just be mindful to use the full path/filename so your code will work from wherever it is called.

to have your code start up with the computer there are a few ways you can look up.

1) rc.local https://www.raspberrypi.org/documentati ... c-local.md
2) systemd https://www.raspberrypi.org/documentati ... systemd.md
init.d tab

select the method that ensures that all the resources your program needs will have started by the time you start your script for example you will have problems if your code relies on a network connection and it starts up before your puter has joined your network.

google is your friend here just use the keywords "Pi" and "Python" along with a 1 or 2 word synopsis of your interest ie
"Python Pi autostart script"

have fun

Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: Start programme with push button

Tue Dec 25, 2018 5:18 am

Code 1 has a print statement. That's what needs replacing with your flashing led code. The part which makes the led flash is someting like

Code: Select all

while True:
    GPIO.output(23, GPIO.HIGH)
    GPIO.output(23, GPIO.LOW)

Posts: 2
Joined: Mon Dec 24, 2018 12:26 pm

Re: Start programme with push button

Fri Dec 28, 2018 12:43 pm

Many thanks for your replies.
I'll give them a try when the festivities are over (i.e. the family have gone home) and get back to you.
Happy New Year, whichever calendar you use.

Return to “Beginners”