Hello,
I'm new here and inexperienced in Arduino programming. I just purchased an Arduino Mega (1280) in the hope I could implement something and I soon found about limitations. Hence I am trying to find help with my problem.
I need to make square wave with:
- fixed "HIGH" duration (which I should be able to set at the beginning)
- adjustable frequency up to 20Khz (ideally up to 50Khz)
- as many digital outputs capable to follow this signal (later to be capable to shifted them equally in time one from another)
This is why I chose "backwards" PWM in the topic, as opposed to PWM method where the period of signal is fixed and the "HIGH" duration is variable, I need the same "HIGH" duration while the period is variable.
I would highly appreciate any help in term of ideas, links to similar projects or even snippets of code one may suggest 
Thanks.
PS: I should specify that I don't need to sweep the frequency. I need just to set it then let Arduino run.
Write a blink without delay type sketch where you measure the timing in microseconds.
Something along these lines. Its 2AM and I'm a little tired, the logic may be flawed.
Add reading a pot or something to change the value of period & duration.
make the time elements type unsigned long
set period to 1/20,000, and duration to how long you want it high, must be less than 1/20,0000
loop()
{
current_time = micros();
elapsed_time = current_time - previous_time;
//can try doing all the math in the next statement, I have not seen it work tho using microseconds (or I did it wrong)
// i.e. if (current_time - previous_time >= period){
if (elapsed_time >= period && hi_lo_flag ==0){ // flag is used so the digitalWrite is not repeated every time
previous_time = current_time;
digitalWrite(outputPin, High);
hi_lo_flag == 1;}
if (elapsed_time >=(period+duration) && hi_lo_flag == 1){
digitalWrite (outputPin, LOW);
previous_time = period+duration;
hi_lo_flag = 0;}
}
If the mark is fixed and the space is variable, isn’t this PPM instead of PWM?
Calling it one vs the other doesn't change the requested functionality tho, I am good with the description provided.
Then from henceforth I shall refer to this as Giraffe Delineation Infracycling.
Thanks for replays.
I think I found a lead for my problem. It's a library developed by Jesse Tane and others and is called Timer3. In a nutshell, is using attachInterrupt function triggered by a HW internal timer to drive an output pin.
Since the board I have has 6 interrupts, it looks to me I can drive only 6 pins independently (for the phase shift), and I would need more...
Anyone knows tricks to overcome this? Or at least if and how I can drive multiple pins from the same timer?
Thanks again.