Skip to content

Instantly share code, notes, and snippets.

@neriberto
Created July 25, 2021 22:40
Show Gist options
  • Save neriberto/5dd550676f6a3f1495c971423aa947ca to your computer and use it in GitHub Desktop.
Save neriberto/5dd550676f6a3f1495c971423aa947ca to your computer and use it in GitHub Desktop.
raspberry code to read from gpio
import RPi.GPIO as GPIO
count = 0
last_button = 'nothing'
def button_up_callback(channel):
global last_button
global count
if last_button == 'up':
return
count += 1
print(f"Button up was pushed: {count}!\n")
last_button = 'up'
def button_down_callback(channel):
global last_button
global count
if last_button == 'down':
return
count += 1
print(f"Button down as pushed: {count}!\n")
last_button = 'down'
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(10, GPIO.RISING, callback=button_up_callback)
GPIO.add_event_detect(8, GPIO.RISING, callback=button_down_callback)
message = input("Press enter to quit\n\n")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment