Skip to content

Instantly share code, notes, and snippets.

@nevadajames
Last active March 4, 2019 09:26
Show Gist options
  • Select an option

  • Save nevadajames/144704ee9193ccc1f162e696d5299679 to your computer and use it in GitHub Desktop.

Select an option

Save nevadajames/144704ee9193ccc1f162e696d5299679 to your computer and use it in GitHub Desktop.
A few handy python scripts for raspberry pi
from time import sleep
import RPi.GPIO as GPIO
from pygame import mixer
mixer.init()
alert = mixer.Sound('bell.wav')
GPIO.setmode(GPIO.BOARD)
button1 = 16
button2 = 12
# function to test sleep
def sleep_test(n):
print('Hello, I am going to sleep for ' + str(n) + ' seconds')
sleep(n)
print('Hello, I am awake again')
def alert_and_rest():
alert.play()
sleep(0.5)
def button_message(alias, pin_number):
print('Listening for input on ' + alias + ' pin: ' + str(pin_number))
def check_for_input(button1, button2):
button_message('button1', button1)
button_message('button2', button2)
while(1):
if GPIO.input(button1) == 0:
print('Button 1 engaged')
alert_and_rest()
if GPIO.input(button2) == 0:
print('Button 2 engaged!')
alert_and_rest()
# This must be done unless using an external resistor!!
def set_pull_ups(buttons):
for button in buttons:
GPIO.setup(button, GPIO.IN,pull_up_down=GPIO.PUD_UP)
set_pull_ups([button1, button2])
sleep_test(0.5)
check_for_input(button1, button2)
import RPi.GPIO as GPIO
import time
channel = 21
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def switch_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn device on (relay closed)
def switch_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn device off (relay open)
if __name__ == '__main__':
try:
switch_on(channel)
time.sleep(1)
switch_off(channel)
time.sleep(1)
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment