diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-05 09:24:48 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-05 09:24:48 +0100 |
| commit | 5466f96bd75f602f5b5fd44a7ccee745f2abfca0 (patch) | |
| tree | 52370ece205756f31dce32ddd607d1884ad1e2a0 /recourses/Buzzer.py | |
| parent | 6198d0333d432b4e0dad7e8da2c2395473a12995 (diff) | |
no clue what i did here
Diffstat (limited to 'recourses/Buzzer.py')
| -rw-r--r-- | recourses/Buzzer.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/recourses/Buzzer.py b/recourses/Buzzer.py new file mode 100644 index 0000000..8555184 --- /dev/null +++ b/recourses/Buzzer.py | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | |||
| 3 | import RPi.GPIO as GPIO | ||
| 4 | import time | ||
| 5 | |||
| 6 | # Set #17 as buzzer pin | ||
| 7 | BeepPin = 17 | ||
| 8 | |||
| 9 | def print_message(): | ||
| 10 | print("========================================") | ||
| 11 | print ("| Beep |") | ||
| 12 | print ("| ------------------------------ |") | ||
| 13 | print ("| Buzzer connect to GPIO0 |") | ||
| 14 | print ("| |") | ||
| 15 | print ("| Make Buzzer beep |") | ||
| 16 | print ("| |") | ||
| 17 | print ("| |") | ||
| 18 | print ("========================================\n") | ||
| 19 | print 'Program is running...' | ||
| 20 | print 'Please press Ctrl+C to end the program...' | ||
| 21 | raw_input ("Press Enter to begin\n") | ||
| 22 | |||
| 23 | def setup(): | ||
| 24 | # Set the GPIO modes to BCM Numbering | ||
| 25 | GPIO.setmode(GPIO.BCM) | ||
| 26 | # Set LedPin's mode to output, | ||
| 27 | # and initial level to High(3.3v) | ||
| 28 | GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.HIGH) | ||
| 29 | |||
| 30 | def main(): | ||
| 31 | print_message() | ||
| 32 | while True: | ||
| 33 | # Buzzer on (Beep) | ||
| 34 | print 'Buzzer On' | ||
| 35 | GPIO.output(BeepPin, GPIO.LOW) | ||
| 36 | time.sleep(0.1) | ||
| 37 | # Buzzer off | ||
| 38 | print 'Buzzer Off' | ||
| 39 | GPIO.output(BeepPin, GPIO.HIGH) | ||
| 40 | time.sleep(0.1) | ||
| 41 | |||
| 42 | def destroy(): | ||
| 43 | # Turn off buzzer | ||
| 44 | GPIO.output(BeepPin, GPIO.HIGH) | ||
| 45 | # Release resource | ||
| 46 | GPIO.cleanup() | ||
| 47 | |||
| 48 | # If run this script directly, do: | ||
| 49 | if __name__ == '__main__': | ||
| 50 | setup() | ||
| 51 | try: | ||
| 52 | main() | ||
| 53 | # When 'Ctrl+C' is pressed, the child program | ||
| 54 | # destroy() will be executed. | ||
| 55 | except KeyboardInterrupt: | ||
| 56 | destroy() \ No newline at end of file | ||
