Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'wiringpi'
- # Sound files
- open_sound = 'creaking-door.mp3'
- close_sound = ''
- # Initialise
- io = WiringPi::GPIO.new
- pin = 1 # GPIO 18
- current_value = 0
- previous_value = io.read(pin)
- def play_sound(file)
- if !file.empty? then system("mpg123 -q #{file}") end
- end
- def get_time
- time = Time.new
- time.strftime('%Y-%m-%d %H:%M:%S')
- end
- # Wait for the door...
- while true
- current_value = io.read(pin)
- if current_value == 0 && previous_value == 1
- print "Door opened at #{get_time}... "
- play_sound(open_sound)
- elsif current_value == 1 && previous_value == 0
- print "closed at #{get_time}\n"
- play_sound(close_sound)
- end
- sleep 0.5
- previous_value = current_value
- end
Add Comment
Please, Sign In to add comment