Go back to your original code. Put some print statements to show what the calculated times are. (Your calculation for high time was incorrect, since it was done with integer arithmetic.)
For example, I measured times for various operations using micros(). I faked the values for the pot readings to try to get an 8 ms period with 50% duty cydle.
const int pot1Pin = 0;
const int pot2Pin = 1;
const int outPin = 13;
const int constPin = 8;
const int triggerPin = 9;
unsigned long tbegin, tend;
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT);
// Enable pullups on digital input pins
digitalWrite(constPin, HIGH);
digitalWrite(triggerPin, HIGH);
tbegin = micros();
analogRead(pot1Pin);
analogRead(pot2Pin);
tend = micros();
Serial.print("Time for two analogReads = ");
Serial.println(tend-tbegin);
tbegin = micros();
digitalRead(triggerPin);
digitalRead(constPin);
digitalWrite(outPin, HIGH);
digitalWrite(outPin, LOW);
tend = micros();
Serial.print("Time for two digitalReads + two digitaWrites = ");
Serial.println(tend-tbegin);
}
int old1 = 32767;
int old2 = 32767;
int new1, new2;
int period, dutycycle, hightime, lowtime;
volatile int triggerState, constState;
const int tol = 5;
void loop()
{
new1 = analogRead(pot1Pin);
new2 = analogRead(pot2Pin);
// I will ignore the values that I just read and give it what I want.
new1 = 0;
new2 = 512;
triggerState = digitalRead(triggerPin);
constState = digitalRead(constPin);
if ((abs(new1-old1) > tol) || (abs(new2-old2) > tol)) {
tbegin = micros();
period = new1*0.05+8; //calculate period between 8 and 60ms
dutycycle = new2*0.07+15; //calculate dutycycle between 15 and 85%
Serial.print("Period = ");
Serial.print(period);
Serial.print(", Duty cycle = ");
Serial.println(dutycycle);
hightime = (period/100.0)*dutycycle; //<=== Force it to use floating point here!
lowtime = period-hightime;
old1 = new1;
old2 = new2;
tend = micros();
Serial.print("Calculaton time = ");
Serial.println(tend-tbegin);
Serial.print("hightime = ");
Serial.print(hightime);
Serial.print(", lowtime = ");
Serial.println(lowtime);
}
// For now, ignore the buttons; make it toggle
constState = LOW;
triggerState = HIGH;
if(constState == HIGH) {
digitalWrite(outPin, HIGH);
}
else if(triggerState == HIGH) {//if pulsed output button is on
digitalWrite(outPin, HIGH);
delay(hightime);
digitalWrite(outPin, LOW);
delay(lowtime);
}
}
Output:
Time for two analogReads = 320
Time for two digitalReads + two digitaWrites = 24
Period = 8, Duty cycle = 50
Calculaton time = 29976
hightime = 4, lowtime = 4
period and hightime and lowtime are the times (in milliseconds) for the waveform that we are trying to generate, using delay(); other times reported above are from micros(), which gives microsecond timing with a granularity of 4 microseconds.
Output on a Duemilanove was an approximate square wave. Period was about 8.26 milliseconds and duty cycle was about 50%. Slight jitter (roughly 10 usec) is unavoidable due to Timer 0 interrupt used for millis(), delay() and other Arduino timing chores. In other words all measurements are consistent with the calculations.
Regards,
Dave
Footnote:
With the expression for hightime shown in the Original Post, integer arithmetic makes for an incorrect answer. I showed floating point arithmetic in my example above. (If I were really picky, I would have used rounding to get the integer approximation of the floating point value). Anyhow, the formula could be restated to keep with integer arithmetic:
hightime = (period*dutycycle)/100; //Re-arrange formula to avoid integer arithmetic problem
I found that results are about the same, and execution time is about the same, but the code size is smaller with integer arithmetic.
There may (or may not) be advantages to using the Arduino map() function or other equivalent integer arithmetic for calculating period and duty cycle from pot readings. The point is, if you are concerned about such things, you can instrument the code with print statements to tell you what differences there may be.
The important fact is that these calculations are only performed when there are changes to be made, so they don't add delay to the loop that would impose limits on the range of periods that can be generated.