Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- ########################################################################
- # Filename : ButtonLED.py
- # Description : Controlling an led by button.
- # Author : freenove
- # modification: 2018/08/02
- ########################################################################
- import RPi.GPIO as GPIO
- import time
- ledPin = 11 # define the ledPin
- buttonPin = 12 # define the buttonPin
- def setup():
- print ('Program is starting...')
- GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
- GPIO.setup(ledPin, GPIO.OUT) # Set ledPin's mode is output
- GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high level(3.3V)
- def loop():
- led = GPIO.input(ledPin)
- btnWas = GPIO.input(buttonPin)
- t = time.clock()
- while True:
- btn = GPIO.input(buttonPin)
- if btn != btnWas :
- # remember button
- btnWas = btn
- print('button: %d' %btn)
- if btn == GPIO.LOW :
- if time.clock() - t < 0.2 :
- print ("button: blink")
- for i in range(1, 5):
- GPIO.output(ledPin, GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(ledPin, GPIO.LOW)
- time.sleep(0.5)
- t = time.clock()
- # change LED
- if led == GPIO.LOW :
- led = GPIO.HIGH
- else :
- led = GPIO.LOW
- GPIO.output(ledPin, led)
- def destroy():
- GPIO.output(ledPin, GPIO.LOW) # led off
- GPIO.cleanup() # Release resource
- if __name__ == '__main__': # Program start from here
- setup()
- try:
- loop()
- except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
- destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement