I got a question, I used your library to generate PWM signals on my arduino uno. I used the code you had written but added to it pin3, pin11 and pin10 along with pin9. so that all of them would generate a PWM signal of frequency 108KHz. Now the problem is that pin 11 can not generate that PWM signal while the rest of the pins can. I don't know why. I thought that pin 11 is broken but I tried another code on it to light an LED and it worked fine. I attached the code below. Thank you very much for your help and for posting that library.
#include <PWM.h>
//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9; // the pin that the LED is attached to
int led2 = 10; //added
int led3 = 11; //added
int led4 = 3; //added
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int32_t frequency = 108000; //frequency (in Hz)
void setup()
{
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(led, frequency);
bool success2 = SetPinFrequencySafe(led2, frequency); //added
bool success3 = SetPinFrequencySafe(led3, frequency); //added
bool success4 = SetPinFrequencySafe(led4, frequency); //added
//if the pin frequency was set successfully, turn pin 13 on
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
//use this functions instead of analogWrite on 'initialized' pins
pwmWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
pwmWrite(led2, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
pwmWrite(led3, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
pwmWrite(led4, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
}