Skip to content

Instantly share code, notes, and snippets.

@mame-n
Created May 3, 2016 08:55
Show Gist options
  • Save mame-n/10281048ab24e981c0be8509b7274191 to your computer and use it in GitHub Desktop.
Save mame-n/10281048ab24e981c0be8509b7274191 to your computer and use it in GitHub Desktop.
require 'wiringpi2'
$a_pin = 1 #18
$b_pin = 4 #23
$led_pin = 0 #17
def discharge
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode($a_pin, WiringPi::INPUT)
gpio.pin_mode($b_pin, WiringPi::OUTPUT)
end
io.digital_write($b_pin, WiringPi::LOW)
sleep 0.005
end
def charge_time
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode($b_pin, WiringPi::INPUT)
gpio.pin_mode($a_pin, WiringPi::OUTPUT)
end
io.digital_write($a_pin, WiringPi::HIGH)
count = 0
while io.digital_read($b_pin) == 0
count += 1
end
count
end
def analog_read
discharge
charge_time
end
def led_on
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode(0, WiringPi::OUTPUT)
end
io.digital_write(0, WiringPi::HIGH)
end
def led_off
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode(0, WiringPi::OUTPUT)
end
io.digital_write(0, WiringPi::LOW)
end
while 1
light = analog_read
light > 70 ? led_on : led_off
puts light
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment