Created
January 26, 2020 17:49
-
-
Save alex3165/a03f7fdd2a915ad32e462e161c0803c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import RPi.GPIO as GPIO | |
import time | |
state = GPIO.LOW | |
def callback(channel): | |
global state | |
if GPIO.input(channel): | |
state = GPIO.LOW | |
else: | |
state = GPIO.HIGH | |
# Set our GPIO numbering to BCM | |
GPIO.setmode(GPIO.BCM) | |
# Define the GPIO pin that we have our digital output from our sensor connected to | |
channel = 17 | |
# Set the GPIO pin to an input | |
GPIO.setup(channel, GPIO.IN) | |
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) | |
# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW | |
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) | |
# This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function | |
GPIO.add_event_callback(channel, callback) | |
# This is an infinte loop to keep our script running | |
while True: | |
# This line simply tells our script to wait 0.1 of a second, this is so the script doesnt hog all of the CPU | |
time.sleep(0.1) | |
GPIO.output(8, state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment