Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'wiringpi'
  3.  
  4. # Sound files
  5. open_sound = 'creaking-door.mp3'
  6. close_sound = ''
  7.  
  8. # Initialise
  9. io = WiringPi::GPIO.new
  10. pin = 1 # GPIO 18
  11. current_value = 0
  12. previous_value = io.read(pin)
  13.  
  14. def play_sound(file)
  15. if !file.empty? then system("mpg123 -q #{file}") end
  16. end
  17.  
  18. def get_time
  19. time = Time.new
  20. time.strftime('%Y-%m-%d %H:%M:%S')
  21. end
  22.  
  23. # Wait for the door...
  24. while true
  25. current_value = io.read(pin)
  26. if current_value == 0 && previous_value == 1
  27. print "Door opened at #{get_time}... "
  28. play_sound(open_sound)
  29. elsif current_value == 1 && previous_value == 0
  30. print "closed at #{get_time}\n"
  31. play_sound(close_sound)
  32. end
  33. sleep 0.5
  34. previous_value = current_value
  35. end
Add Comment
Please, Sign In to add comment