I was trying to be a little coy and not give away the answer too easily, but I think I've been withholding too much.
I think the hangup you have over finding the right place for the digitalWrite(HIGH) statement is because you have a conception of the repeating sequence that is too rigid. Based on the code you're posting, you probably think of the sequence like this:
- Write output HIGH
- Increment PWM from min to max at rampLength intervals (positive ramp segment)
- Write output LOW
- Decrement PWM from max to min (negative ramp segment)
- Hold PWM at min for INTERVAL time period.
Because of this, I can see that you're keeping the output HIGH statement towards the top of loop(). It doesn't need to be there, and I'll run through the thinking I used to find a place that works.
This is one correct way to list of the steps, but because you are continuously looping through this sequence it is not the only unique way to list the steps. A loop makes the list circular: 1 -> 2 -> 3 -> 4 -> 5 -> 1 -> 2 -> 3 -> etc. The end of the loop wraps back to the beginning. Circles like that do not have a beginning, so I can choose ANY one of those 5 steps to label as #1, so there is a bit more freedom to restructure things to make implementation easier.
In this case, we only need to rotate everything up 1 space like this:
- Increment PWM from min to max at rampLength intervals (positive ramp segment)
- Write output LOW
- Decrement PWM from max to min (negative ramp segment)
- Hold PWM at min for INTERVAL time period.
- Write output HIGH
All of the steps are looped through in the exact same order, the only thing I have changed is which one we think of as #1. This frees you from thinking that the output HIGH statement has to be towards the top of the loop() function, and suggests a different place to try: immediately after the INTERVAL delay.
ramplength = map(analogRead(ramplengthPin),0,1023,2,11800); // adjust map for packetcount
}
while(micros()-delayStartTime<=INTERVAL);
digitalWrite(output,HIGH); <-- BOOM!! There is is!
}
StartUs += ramplength; //loose control of ramplengthPin & pwmMaxPin // only intervalPin works // with int Dir=2; min 4ms
//loose control of ramplengthPin -- pwmMaxPin && intervalPin works // with int static Dir=1;
//StartUs=micros(); //loose control of pwmMaxPin // ramplenghtPin & intervalPin works // with int Dir=2; min 4ms
} // All three work!!!! but....now Triangle wave!!!!! // with int static Dir=1;
There is one small caveat to rotating the , which deals with the fact that microcontroller loops are not infinite. A microcontroller loop never terminates unless there is a power loss or malfunction, and those events are likely to happen independently of what step in the loop sequence you are in. The end of the loop is not defined, and can be thought of as unbounded in the future direction. However, they do have a defined beginning, which starts at power up. The steady-state behavior of the two ways of the two sequence lists will be identical, but the very first iteration will be different. For all sequence iterations except the first, the #1 step has had a #5 step immediately before it. The first iteration after power up though, is starting from scratch.
In this specific case, using the second sequence list, it means that on only the first pulse after startup, the output will not be HIGH for the positive ramp; all other iterations of the pulse will behave properly. You have two options: 1) Decide that this is acceptable behavior and leave it as it is, or 2) Add the "missing" beginning of the sequence to the end of setup(), like this:
GTCCR = 1<<PWM1B | 2<<COM1B0;
pinMode(outPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(outPin, HIGH); <-- Right there
}
I haven't tested it, but that should solve your problem. I hope this made sense, I've been trying to teach you the concept instead of just giving you a chunk of text to paste in without knowing how it works.
Also, just remove the do-while loop around the analog reads. Since you're using the delay function anyway, there's no point too it. Just read them once and move on.