Advertisement
Guest User

tilt_buzzer.py

a guest
May 2nd, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import time
  4. import math
  5. from grovepi import *
  6.  
  7. buzzer_pin = 2
  8. switch_pin = 4
  9. pinMode(buzzer_pin,"OUTPUT")
  10. pinMode(switch_pin,"INPUT")
  11.  
  12. while True:
  13.   try:
  14.     switch_status = digitalRead(switch_pin)
  15.     if switch_status:
  16.       accelerometer = acc_xyz()
  17.       print '\nX:', accelerometer[0], '\nY:', accelerometer[1], '\nZ:', accelerometer[2]
  18.  
  19.       if accelerometer[0] > 0:
  20.         digitalWrite(buzzer_pin,1)
  21.         print '\tBuzzing'
  22.       else:
  23.         digitalWrite(buzzer_pin,0)
  24.  
  25.     else:
  26.       print "Off"
  27.  
  28.     time.sleep(0.1)
  29.  
  30.   except KeyboardInterrupt:
  31.     digitalWrite(buzzer_pin,0)
  32.     break
  33.   except (IOError,TypeError) as e:
  34.     print "Error"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement