-
Notifications
You must be signed in to change notification settings - Fork 33
Home
Version 0005:
- Fixed Arduino 1.0 mess.
- Added Polarity control.
- Up to 20 channels can be defined.
- Temporary: http://rogue-code.googlecode.com/files/Wiring-Library-SoftPWM-V0005.zip
- (will update to GitHub releases)
TODO
An Arduino and Wiring Library to produce PWM signals on any arbitrary pin.
It was originally designed for use controlling the brightness of LEDs, but could be modified to control servos and other low frequency PWM controlled devices as well.
It uses a single hardware timer (Timer 2) on the microcontroller to generate up to 20 PWM channels.
- Arbitrary output pins.
- Up to 20 different channels can be created.
- True zero level, i.e. off = off
- Separate fade rates for on and off.
Notes:
This library doesn't work well with servos, so beware that it may not work as you may expect if used to control servos.
The reason being is that the SoftPWM range of values is over the entire pulse width, whereas a servo expects a pulse over a tight range (0.5 mS to 2.5 mS and repeated every 20 mS). Hence, the range of usable values for a servo or such would be something like 40 to 50 resulting in big jumps in the servo position.
#include <SoftPWM.h>
void setup()
{
// Initialize
SoftPWMBegin();
// Create and set pin 13 to 0 (off)
SoftPWMSet(13, 0);
// Set fade time for pin 13 to 100 ms fade-up time, and 500 ms fade-down time
SoftPWMSetFadeTime(13, 100, 500);
}
void loop()
{
// Turn on - set to 100%
SoftPWMSetPercent(13, 100);
// Wait for LED to turn on - you could do other tasks here
delay(100);
// Turn off - set to 0%
SoftPWMSetPercent(13, 0);
// Wait for LED to turn off
delay(500);
}
SoftPWMBegin([
defaultPolarity
])
- Initializes the library - sets up the timer and other tasks.
- optional
defaultPolarity
allows all newly defined pins to take on this polarity.- Values:
SOFTPWM_NORMAL
,SOFTPWM_INVERTED
- Values:
SoftPWMSet(
pin
,
value
)
-
pin
is the output pin. -
value
is a value between 0 and 255 (inclusive).
SoftPWMSetPercent(
pin
,
percent
)
-
pin
is the output pin. -
percent
is a value between 0 and 100 (inclusive).
SoftPWMSetFadeTime(
pin
,
fadeUpTime
,
fadeDownTime
)
-
pin
is the output pin. -
fadeuptime
is the time in milliseconds that it will take the channel to fade from 0 to 255.- Range: 0 to 4000
-
fadedowntime
is the time in milliseconds that it will take the channel to fade from 255 to 0.- Range: 0 to 4000
SoftPWMSetPolarity(
pin
,
polarity
)
-
pin
is the output pin. -
polarity
is the polarity for the given pin.
Notes
- You can use
ALL
in place of the pin number to have the function act on all currently set channels.- e.g.
SoftPWMSetFadeTime(ALL, 100, 400)
- this will set all created channels to have a fade-up time of 100 ms and a fade-down time of 400.
- e.g.
- The polarity setting of the pin is as follows:
-
SOFTPWM_NORMAL
means that the pin LOW when the PWM value is 0, whereasSOFTPWM_INVERTED
indicates the pin should be HIGH when the PWM value is 0.
-
Arduino Duemilanove LED Blink example - available as library example:
rDuino LEDHead Bounce example - available as library example:
More demos:
http://www.youtube.com/view_play_list?p=33BB5D2E20609C52