Created
November 8, 2012 00:21
-
-
Save mcoms/4035597 to your computer and use it in GitHub Desktop.
WiringPi PWM Test
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
source 'https://rubygems.org' | |
gem 'wiringpi' |
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
require 'rubygems' | |
require 'bundler/setup' | |
require 'wiringpi' | |
$io = WiringPi::GPIO.new | |
PIN = 6 # GPIO pin 25 | |
$io.mode PIN, OUTPUT | |
FRAME = 1.0/50.0 | |
$pulse_width = 0.0 | |
$pulse_off = FRAME | |
def set_duty_cycle(duty_cycle) | |
if (duty_cycle == 0) | |
$pulse_width = 0 | |
$pulse_off = FRAME | |
else | |
$pulse_width = FRAME * (duty_cycle/100.0) | |
$pulse_off = FRAME - $pulse_width | |
end | |
end | |
def pwm | |
puts "Light is online." | |
while true | |
$io.write PIN, HIGH | |
sleep $pulse_width | |
$io.write PIN, LOW | |
sleep $pulse_off | |
end | |
end | |
t = Thread.new { pwm } | |
100.downto(1).each do |duty_cycle| | |
set_duty_cycle duty_cycle | |
puts "Set #{duty_cycle}%." | |
sleep 0.1 | |
end | |
1.upto(100).each do |duty_cycle| | |
set_duty_cycle duty_cycle | |
puts "Set #{duty_cycle}%." | |
sleep 0.1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically works, must be run as root.
https://www.dropbox.com/s/12ojde9n9suugbl/pwm-led.jpg