bhagman / softpwm Goto Github PK
View Code? Open in Web Editor NEWA Wiring (and Arduino) Library to produce PWM signals on arbitrary pins.
License: MIT License
A Wiring (and Arduino) Library to produce PWM signals on arbitrary pins.
License: MIT License
The library works with Arduino Nano and Teensy (AVR/ARM) but not with EVERY boards (MEGAAVR).
When compiling, following error is given...
In file included from Arduino\libraries\SoftPWM\SoftPWM.cpp:44:0:
Arduino\libraries\SoftPWM\SoftPWM.cpp: In function 'void SoftPWMBegin(uint8_t)':
Arduino\libraries\SoftPWM\SoftPWM_timer.h:39:3: error: 'TIFR2' was not declared in this scope
TIFR2 = (1 << TOV2); /* clear interrupt flag */ \
Counld someone provide an example of using softpwm to control a servo? I don't know how to do it correctly. Many thanks.
Hey,
I was wondering what exact pwm frequence the lib generates on a Arduino Mini with a Atmel 328p?
Arduino: 1.8.12 (Mac OS X), Board: "ChipKit uC32"
RoomControlCode:2:10: fatal error: SoftPWMServo.h: No such file or directory
#include <SoftPWMServo.h>
^~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
SoftPWMServo.h: No such file or directory
Hi there!
Is there an option to increase the frequency of the PWM signal? I'm trying to control small DC motors using this library, but there are significant vibrations going on to the motor with using the PWM. I suppose it is cause of the low frequency of the PWM signal. If yes, how can I do that?
I would like to activate multiple pins at once using soft pwm...
maybe the ability to pass an array instead of a single value?
Would this be possible?
Hi Brett
I like your SoftPWM-library for arduino a lot. Just today I found out, that millis() stops working, as soon as more than 4 LEDs are controlled using our library. I wasn't able to find a solution to that problem. Do you have an idea?
Best regards,
Dani
I used your example-code (), which uses 8 LEDs:
When checking with the serial monitor, I get constantly
26
26
26
Here is the code that doesn't work:
#include <SoftPWM.h>
#define DELAY 100
uint8_t leds[8] = {11,12,13,18,17,16,15,10};
void setup()
{
Serial.begin(57600);
SoftPWMBegin();
for (int i = 0; i < 8; i++)
SoftPWMSet(leds[i], 0);
SoftPWMSetFadeTime(ALL, 50, 400);
}
void loop()
{
int i;
Serial.println(millis());
for (i = 0; i < 7; i++)
{
SoftPWMSet(leds[i+1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
for (i = 7; i > 0; i--)
{
SoftPWMSet(leds[i-1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
}
As soon as I change the code, so that it uses only 4 LEDs, it works as expected:
3014
5851
8695
Here is the code that works:
#include <SoftPWM.h>
#define DELAY 100
uint8_t leds[8] = {11,12,13,18,17,16,15,10};
void setup()
{
Serial.begin(57600);
SoftPWMBegin();
for (int i = 0; i < 4; i++)
SoftPWMSet(leds[i], 0);
SoftPWMSetFadeTime(ALL, 50, 400);
}
void loop()
{
int i;
Serial.println(millis());
for (i = 0; i < 3; i++)
{
SoftPWMSet(leds[i+1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
for (i = 3; i > 0; i--)
{
SoftPWMSet(leds[i-1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
}
How to get this running on an ATtiny85 or ATtiny167 with no Timer2?
"on" is not fully "on", i.e. pwm of 255 have a short period of 0
This is how i patched it to work for my application (DC motor - h-bridge with "break" and "coast" ):
Note: My "h-bridge" ic require "full width high" for a "break" and "full width low" for a "coast"
What I did is:
see the modified interrupt function below:
{
uint8_t i;
int16_t newvalue;
int16_t direction;
if (++_isr_softcount == 0)
{
// set all channels high - let's start again
// and accept new checkvals
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{
if (_softpwm_channels[i].fadeuprate > 0 || _softpwm_channels[i].fadedownrate > 0)
{
// we want to fade to the new value
direction = _softpwm_channels[i].pwmvalue - _softpwm_channels[i].checkval;
// we will default to jumping to the new value
newvalue = _softpwm_channels[i].pwmvalue;
if (direction > 0 && _softpwm_channels[i].fadeuprate > 0)
{
newvalue = _softpwm_channels[i].checkval + _softpwm_channels[i].fadeuprate;
if (newvalue > _softpwm_channels[i].pwmvalue)
newvalue = _softpwm_channels[i].pwmvalue;
}
else if (direction < 0 && _softpwm_channels[i].fadedownrate > 0)
{
newvalue = _softpwm_channels[i].checkval - _softpwm_channels[i].fadedownrate;
if (newvalue < _softpwm_channels[i].pwmvalue)
newvalue = _softpwm_channels[i].pwmvalue;
}
_softpwm_channels[i].checkval = newvalue;
}
else // just set the channel to the new value
_softpwm_channels[i].checkval = _softpwm_channels[i].pwmvalue;
// now set the pin low (checkval == 0x00) otherwise high
if (_softpwm_channels[i].checkval == 0x00)
{
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
else
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
}
else
{
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
else
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
}
}
}
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{
if (_softpwm_channels[i].pin >= 0) // if it's a valid pin
{
// skip hit on checkval 0 and 255
if (_softpwm_channels[i].checkval != 0x00 && _softpwm_channels[i].checkval != 0xFF)
{
if (_softpwm_channels[i].checkval == _isr_softcount) // if we have hit the width
{
// turn off the channel
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
else
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
}
}
}
}
}
I wanted to use the library in a sketch including the IRremote library. Both librarys dont anymore as soon as they are enabled using irrecv.enableIRIn();
respectively SoftPWMBegin();
.
I can assume some reasons for that behavior, but I am devinitely not able to tear down the exact reason.
PWM 255 as well as digitalWrite(PIN,HIGH) isn't HIGH any more.
Now there is a PWM spike on the signal. Seems that PWM 254 is the maximum HIGH level now.
LOW Level works (as mentioned within the README.md "True zero level, i.e. off == off").
HIGH Level is not mentioned :-)
I really appreciate the purpose of this library.
The lib compiles fine on Arduino CREATE but fails to upload.
It is NOT a CREATE problem but appears to be with the samples.
Hello,
Would it be possible to control more than 20 pins (I'd need 24 pins) on Mega2560?
I don't have it yet so I can't test, just want to know if possible and how.
Thanks!
suggested add:
byte WLED = 13;
else you get:
Arduino: 1.8.5 (Linux), Board: "Arduino Nano, ATmega328P"
/home/pi/Arduino/libraries/SoftPWM/examples/SoftPWM_Wiring_HeartBeat/SoftPWM_Wiring_HeartBeat.pde: In function 'void setup()':
SoftPWM_Wiring_HeartBeat:8: error: 'WLED' was not declared in this scope
SoftPWMSet(WLED, 0);
^
/home/pi/Arduino/libraries/SoftPWM/examples/SoftPWM_Wiring_HeartBeat/SoftPWM_Wiring_HeartBeat.pde: In function 'void loop()':
SoftPWM_Wiring_HeartBeat:18: error: 'WLED' was not declared in this scope
SoftPWMSet(WLED, 255);
^
exit status 1
'WLED' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.