Advertisement
Guest User

Pi Shutdown With LED

a guest
Dec 29th, 2014
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. # This script will wait for a button to be pressed and then shutdown
  2.  
  3. # the Raspberry Pi.
  4.  
  5. # The button is to be connected on pin 5 (GPIO 03) and any ground
  6.  
  7. # GPIO03 is used because it can also wake from halt (hardware function)
  8.  
  9.  
  10. # http://kampis-elektroecke.de/?page_id=3740
  11. # http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
  12.  
  13. # https://pypi.python.org/pypi/RPi.GPIO
  14.  
  15.  
  16. import RPi.GPIO as GPIO
  17.  
  18. import time
  19.  
  20. import os
  21.  
  22.  
  23.  
  24. # we will use the pin numbering of the SoC, so our pin numbers in the code are
  25. # the same as the pin numbers on the gpio headers
  26.  
  27.  
  28. GPIO.setmode(GPIO.BCM)  
  29.  
  30.  
  31. # Pin 05 (gpio03) will be input and will have his pull up resistor activated
  32. # so we only need to connect a button to ground
  33.  
  34. GPIO.setup(03, GPIO.IN, pull_up_down = GPIO.PUD_UP)  
  35.  
  36.  
  37. # Pin 17 is our power pin for the LED light.  When on, the circuit completes.
  38.  
  39. GPIO.setup(17, GPIO.OUT)
  40.  
  41.  
  42. # Flash the LED as the system starts up, then leave it on.
  43.  
  44. GPIO.output(17,1)
  45.  
  46. time.sleep(.25)
  47.  
  48. GPIO.output(17,0)
  49.  
  50. time.sleep(.10)
  51.  
  52. GPIO.output(17,1)
  53.  
  54. time.sleep(.25)
  55.  
  56. GPIO.output(17,0)
  57.  
  58. time.sleep(.10)
  59.  
  60. GPIO.output(17,1)
  61.  
  62. time.sleep(.25)
  63.  
  64. GPIO.output(17,0)
  65.  
  66. time.sleep(.10)
  67.  
  68. GPIO.output(17,1)
  69.  
  70.  
  71.  
  72.  
  73. # ISR: if our button is pressed, we will have a falling edge on pin 5
  74.  
  75. # this will trigger this interrupt
  76.  
  77. :
  78. def Int_shutdown(channel):  
  79.    
  80. # flash LED on pin 17
  81.        
  82. GPIO.output(17,0)
  83.        
  84. time.sleep(.15)
  85.        
  86.  GPIO.output(17,1)
  87.        
  88.  time.sleep(.10)
  89.      
  90.   GPIO.output(17,0)
  91.  
  92. GPIO.output(17,1)
  93.        
  94.  time.sleep(.10)
  95.  
  96. time.sleep(.10)
  97.      
  98.   GPIO.output(17,0)
  99.        
  100.    GPIO.cleanup()                                
  101.      
  102.   # shutdown our Raspberry Pi
  103.    
  104.     os.system("sudo shutdown -h now")
  105.          
  106.  
  107.    
  108.  
  109. # Now we are programming pin 5 as an interrupt input
  110.  
  111. # it will react on a falling edge and call our interrupt routine "Int_shutdown"
  112.  
  113.  
  114. GPIO.add_event_detect(03, GPIO.FALLING, callback = Int_shutdown, bouncetime = 2000)  
  115.  
  116.  
  117.  
  118. # do nothing while waiting for button to be pressed
  119.  
  120.  
  121. while 1:
  122.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement