I dunno if Arduino can do this.
The duty cycle of the first pulse is different from the second pulse. It goes like this
I dunno if Arduino can do this.
The duty cycle of the first pulse is different from the second pulse. It goes like this
I don't know with a UNO but I can tell you that it's simple with a DUE.
Yes, you can do that, but likely not using the timer hardware PWM mode. Some timers can be setup to provide different timing for both high and low phases on every cycle, by using the compare mode, toggle output on compare, and an interrupt that is invoked on each compare. I do exactly that to create acceleration/deceleration ramping for stepper and servo motor controllers using a 328P.
Regards,
Ray L.
Possibly OR-ing two channels can do this too.
ard_newbie:
I don't know with a UNO but I can tell you that it's simple with a DUE.
How do you do it with a DUE?
There is a register (PWM_DTYUPD i.e. PWM Duty Cycle Update) to update the duty cycle once a PWM channel has been enabled, and the update is done, automatically, right at the end of the previous period.
One method to obtain the correct waveform would be to trigger an interrupt at the end of each period, and update the duty cycle in the interrupt handler.
If we just for the sake of argument were to call the two different parts of pulse A as Ashort and Along, and of pulse B as Bshort and Blong (where Ashort may or may not be the same as Bshort, it doesn't matter) why don't you just digitalWrite() the pin high for Ashort, low for Along, high for Blong and low for Bshort, repeat.
You could use delay() for a quick-n-dirty solution, or put the 4x times (Ashort etc) in an array and cycle over them as an ever-changing blink without delay interval.
This might be one of those cases where you could better answers if you actually explained why you want to do this thing.
At what frequency?
aarg:
At what frequency?
If that's at me, the frequency would be implicit in the inverse of the sum of the shorts and longs, and there would presumably be some limit due to the inherent time it takes to do a digitalWrite().
If at OP; good question. Goes with my musing about why they want to do this.
hannah_mackinlay:
You could use delay() for a quick-n-dirty solution, or put the 4x times (Ashort etc) in an array and cycle over them as an ever-changing blink without delay interval.
delay provides the exact opposite of precision, with variation from ideal timing in the range of several milliseconds. Using a hardware timer is very simple, and can provide very precise (to the uSec or less) timing, with very little processor overhead. Using a hardware timer, the waveform output can be going on at the same time the processor is doing other useful work. Using delay ties up the processor the entire time the waveform is being generated, unless the waveform frequency is very low.
Regards,
Ray L.
delay provides the exact opposite of precision
Of course, but the OP didn't give even the tiniest clue as to what precision is required. For all we know, the time scale on that penciled drawing is fortnights. At primary school, we were taught to always label our axes, else a graph is (literally) meaningless.
I think the OP should say what they're trying to do.
RayLivingston:
Yes, you can do that, but likely not using the timer hardware PWM mode. Some timers can be setup to provide different timing for both high and low phases on every cycle, by using the compare mode, toggle output on compare, and an interrupt that is invoked on each compare. I do exactly that to create acceleration/deceleration ramping for stepper and servo motor controllers using a 328P.Regards,
Ray L.
Is it possible to use the timer interrupt to do this?
In my program, I use
TCCR0B = TCCR0B & B11111000 | B00000001;
to increase the PWM frequency to 62500Hz.
And I use a timer interrupt to update the duty cycle every 62500Hz.
Is it possible?
Yes. But Timer/Counter0 is used by millis() so this will break everything depending on it.
You will need to service the interrupt every 256 cycles which may load the CPU considerably. If you use other interrupts it is even possible you miss some interrupt.