Last active
March 4, 2019 09:26
-
-
Save nevadajames/144704ee9193ccc1f162e696d5299679 to your computer and use it in GitHub Desktop.
A few handy python scripts for raspberry pi
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
| 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) | |
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
| 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