element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum 1 Button to Start, Shutdown or Shutdown after 60 minutes
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 4 replies
  • Subscribers 664 subscribers
  • Views 928 views
  • Users 0 members are here
  • raspberry_pi
Related

1 Button to Start, Shutdown or Shutdown after 60 minutes

Former Member
Former Member over 9 years ago

Hello and thanks for reading my question!

 

I am building a simple Pi-based nightstand radio for my elderly mother, she likes to listen at night when she can't sleep.

It works now, it boots and automatically begins playing a radio stream from her favorite radio stations's website.

That's ALL she wants it to do and it does it well.

 

What I want to add is a single button, if possible, that serves 3 functions:

 

1-second button press = Pi boots up

3-second button press = Pi shuts down

6-second button press = Pi waits 60 minutes, then shuts down

 

Is this possible with just 1 button? I'd like to keep it to 1 button for simplicity sake

(mom is 84 and will be pressing the button in very low light) but if I must use more than 1 button I will.

 

Thanks for any ideas!

  • Sign in to reply
  • Cancel
  • clem57
    0 clem57 over 9 years ago

    I can give you some of the logic like this:

     

    1. Loop forever.
    2. Pause .1 seconds.
    3. If pressed button, add 1 to counter.
    4. If not pressed, if counter > 6 seconds(60) then go to shut down in 60 minutes.
    5. Else If counter > 3 seconds(30) then go to shut down in 1 minute(I like to wait a bit).
    6. Else if counter >  1 seconds(10) then go to reboot Pi (the Pi is running to test the button, but maybe it is not playing music?).
    7. Otherwise clear counter to 0.
    8. Loop back to 1.
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago

    I think, you can use together with "click", "double click", and "long press".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dougw
    0 dougw over 9 years ago

    How about this sequence...

    1 button, 2 LEDs

    1 push = power on (power  LED ON, Auto LED OFF)

    2nd push = 60 minutes auto shutdown (Power LED ON, Auto LED ON)

    3rd push = shutdown (all LEDs OFF)

     

    If you want just one LED, auto mode could be a slow blink or a different colour.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to clem57

    OK, I'm a newb, BTW, I just connected a single pushbutton SW to GPIO pins 18 and Ground. I then copied and pasted in a python script, set it to run at boot via rc.local, and I have the shutdown working when I press the button.

     

    Interpret Singh here on Element 14 forums posted a article and I took the code from that.

     

    Here is the code I have working now, and I need to add your ideas to it, (or do I need to scrap this code and start all over?)

     

    <code>

    1. #!/bin/python 
    2. # Simple script for shutting down the raspberry Pi at the press of a button. 
    3. # by Inderpreet Singh 
    4.  
    5. import RPi.GPIO as GPIO 
    6. import time 
    7. import os 
    8. # Use the Broadcom SOC Pin numbers 
    9. # Setup the Pin with Internal pullups enabled and PIN in reading mode. 
    10. GPIO.setmode(GPIO.BCM) 
    11. GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP) 
    12. # Our function on what to do when the button is pressed 
    13. def Shutdown(channel): 
    14.     os.system("sudo shutdown -h now") 
    15. # Add our function to execute when the button pressed event happens 
    16. GPIO.add_event_detect(18, GPIO.FALLING, callback = Shutdown, bouncetime = 2000) 
    17. # Now wait! 
    18. while 1: 
    19.     time.sleep(1)

    </code>

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube