Skip to content

Instantly share code, notes, and snippets.

@mcoms
Created November 8, 2012 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcoms/4035597 to your computer and use it in GitHub Desktop.
Save mcoms/4035597 to your computer and use it in GitHub Desktop.
WiringPi PWM Test
source 'https://rubygems.org'
gem 'wiringpi'
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
@mcoms
Copy link
Author

mcoms commented Nov 8, 2012

Basically works, must be run as root.
https://www.dropbox.com/s/12ojde9n9suugbl/pwm-led.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment