Created
January 1, 2013 12:31
-
-
Save lavir/4427112 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment