Worldfrog
Posts: 9
Joined: Mon Dec 28, 2015 12:35 am

GPIO shutdown program help

Mon Dec 28, 2015 8:54 pm

So I made a shutdown program, and the program worked the first few times, but appears to not work when I push the button that should execute code

Code: Select all

import RPi.GPIO as GPIO
import os
import time
import Tkinter
import tkMessageBox
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN, pull_up_down=GPIO.PUD_DOWN
try:
while True:
if(GPIO.input(16)==1):
time.sleep(2);os.system("sudo shutdown -h now")
else:
#not necessary but added to show the program is alive
time.sleep(15);print("On")
except KeyboardInterrupt:
GPIO.cleanup();window=Tkinter.Tk();window.wm_withdraw();tkMessageBox.showinfo(title="Warning", message="Shutdown Program has been halted")
So I'm wondering if
A) My GPIO pin got messed up or
B) My code has something wrong

Worldfrog
Posts: 9
Joined: Mon Dec 28, 2015 12:35 am

Re: GPIO shutdown program help

Mon Dec 28, 2015 9:18 pm

Update: removing the time.sleep(15) before print("On") seems to have fixed it... why?

gordon77
Posts: 5076
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO shutdown program help

Mon Dec 28, 2015 9:23 pm

How long are you pressing the button? You could need to press it for 15 seconds.

Worldfrog
Posts: 9
Joined: Mon Dec 28, 2015 12:35 am

Re: GPIO shutdown program help

Tue Dec 29, 2015 7:19 am

gordon77 wrote:How long are you pressing the button? You could need to press it for 15 seconds.
I was pressing for maybe a second at most. However, I agree, it probably wasn't working because of my sleep command which froze all my code.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GPIO shutdown program help

Tue Dec 29, 2015 7:22 am

You seem to have lost all your indentation (spacing) on posting that code and you have multiple statements per line, which makes it difficult to follow.

Can you repost it with the leading spaces and the code on separate lines so it makes sense?

gordon77
Posts: 5076
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO shutdown program help

Tue Dec 29, 2015 8:44 am

Worldfrog wrote:
gordon77 wrote:How long are you pressing the button? You could need to press it for 15 seconds.
I was pressing for maybe a second at most. However, I agree, it probably wasn't working because of my sleep command which froze all my code.
sleep(15) will stop it doing anything for 15 seconds, even reading your switch.

Worldfrog
Posts: 9
Joined: Mon Dec 28, 2015 12:35 am

Re: GPIO shutdown program help

Tue Dec 29, 2015 5:23 pm

rpdom wrote:You seem to have lost all your indentation (spacing) on posting that code and you have multiple statements per line, which makes it difficult to follow.

Can you repost it with the leading spaces and the code on separate lines so it makes sense?
I can, here it is:

Code: Select all

import RPi.GPIO as GPIO
import os
import time
import Tkinter
import tkMessageBox
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN, pull_up_down=GPIO.PUD_DOWN
try:
    while True:
        if(GPIO.input(16)==1):
            time.sleep(2)
            os.system("sudo shutdown -h now")
        else:
#not necessary but added to show the program is alive
            time.sleep(15);print("On")
except KeyboardInterrupt:
    GPIO.cleanup()
    window=Tkinter.Tk()
    window.wm_withdraw()
    tkMessageBox.showinfo(title="Warning", message="Shutdown Program has been halted")
But I've figured out my problem, as gordon said, my time.sleep(15) paused all my code

Return to “Troubleshooting”