maxint-rd / FastPwmPin

Arduino library to generate a fast PWM signal on an output pin at maximum frequency. Example included.
40 stars 14 forks source link

Changing duty cycle in loop #7

Closed loeckchendasboesschen closed 2 years ago

loeckchendasboesschen commented 2 years ago

Hey hey,

I try using your library to control power leds for high speed video recordings. Everything is working great so far, now I reached a critical point. I'm trying to change the duty cycle with a DMX Signal. I need to keep the frequency constant and only change the duty cycle that dims the LEDs. I know how to change the duty cyle by hand (worked great). So far I couldn't find a way to change the duty cycle while running the program. Any ideas? Looking forward hearing from you! Cheers

Mo

` #include

include

int dycy = 50; int dmx_add = 1; int dmx_val = 0; float conv_val = 0; const int Led1Pin = 5; // PWM Pin für die erste LED

void setup() {

pinMode(Led1Pin, OUTPUT); DMXSerial.init(DMXReceiver); FastPwmPin::enablePwmPin(10, 150000L, dycy); }

void loop() {

dmx_val = DMXSerial.read(1); analogWrite(Led1Pin, DMXSerial.read(1)); conv_val = dmx_val * 0,196; dycy = ((int)conv_val)+30;

}`

maxint-rd commented 2 years ago

Hello @loeckchendasboesschen ,

I am not very familiar with DMX, but I think I understand what you try to accomplish. Whereas the library examples don't show this, it is very well possible to repeatedly call the enablePwmPin() method with changed parameters.

This is the test sketch I've used myself to test different frequencies and duty cycles: mm_FastPwmPin_testfrequencies.ino.txt In it you can see various calls to enablePwmPin(). (Had to rename the ino to .txt for github to allow this attachment)

Likewise you should be able to modify only the dutycycle. But please mind that 1) the MCU requires some time to change the PWM output and 2) the precision of the PWM signal depends on the frequency and on the resolution of the timer that is used. Also: The availability of timers in various resolution differs per MCU.

To give you a quick response I haven't tried anything myself today, but I hope this response is helpful to you...

Edit: see this table for an overview of the various MCUs I used to test this library. Although this library was originally aimed at generating very high frequencies, you probably don't need them for dimming the LEDs. Although you do require timers with higher resolution to get the best precision, the duty cycle is an integer of value 0-100, so only whole percentages are supported.

loeckchendasboesschen commented 2 years ago

Hey Max,

its working exactly as described. Thank you very very much!!!

Any chance to buy you coffee or donate?

maxint-rd commented 2 years ago

Hello @loeckchendasboesschen ,

That's nice to read! Thank you for reporting your positive findings. I haven't signed up for any support system yet. If you ever have plans to come to The Hague (Netherlands) feel free to buy me that coffee!

Seriously though, your successful use of my library and your post already show appreciation of my work. Perhaps you want to publish your work on GitHub too? (In the future I may want to learn a bit more about DMX and your sketch could be helpful to me and to others.)