Hi Badwater,
It seems to be correct.
I extended a little bit.
Tomorrow I also put cbTen so as to verify some behaviors to possibly implement control over time.
void adjustVctlPWM_2()
{
// This should reach a stable PWM output value / a stable 10000000.00 frequency
// after an hour or so, and 10000000.000 after eight hours or so
// check first if we have the data, then do ultrafine and veryfine frequency
// adjustment, when we are very close
// ultimately the objective is 10000000.000 over the last 1000s (16min40s)
if ((cbTho_full) && (avgftho >= 9999999.990) && (avgftho <= 10000000.010))
{
// decrease frequency; 1000s based
if (avgftho >= 10000000.001)
{
if (avgftho >= 10000000.005)
{
// decrease PWM by 5 bits = very fine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output - 5;
Serial.print(F("VF-5 RULE 1 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " vf-");
}
else {
// decrease PWM by one bit = ultrafine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output - 1;
Serial.print(F("UF-1 RULE 2 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " uf-");
}
}
// or increase frequency; 1000s based
else if (avgftho <= 9999999.999)
{
if (avgftho <= 9999999.995)
{
// increase PWM by 5 bits = very fine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output + 5;
Serial.print(F("VF+5 RULE 3 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " vf+");
}
else {
// increase PWM by one bit = ultrafine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output + 1;
Serial.print(F("UF+1 RULE 4 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " uf+");
}
}
}
else if ((cbHun_full) && (avgfhun >= 9999999.90) && (avgfhun <= 10000000.10))
{
///// next check the 100s values in second place because we are too far off
// decrease frequency; 100s based
if (avgfhun >= 10000000.01)
{
if (avgfhun >= 10000000.10)
{
// decrease PWM by 100 bits = coarse
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output - 100;
Serial.print(F("COARSE-100 RULE 5 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " c- ");
}
else {
// decrease PWM by ten bits = fine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output - 10;
Serial.print(F("FINE-10 RULE 6 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " f- ");
}
}
// or increase frequency; 100s based
else if (avgfhun <= 9999999.99)
{
if (avgfhun <= 9999999.90)
{
// increase PWM by 100 bits = coarse
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output + 100;
Serial.print(F("COARSE+100 RULE 7 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " c+ ");
}
else {
// increase PWM by ten bits = fine
prev_adjusted_PWM_output = adjusted_PWM_output;
adjusted_PWM_output = adjusted_PWM_output + 10;
Serial.print(F("FINE+10 RULE 8 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " f+ ");
}
}
else {
// here we keep setting, because it is exact 10000000.000MHz
prev_adjusted_PWM_output = adjusted_PWM_output;
Serial.print(F("HIT RULE 9 ADJUST PWM: "));
Serial.println(adjusted_PWM_output);
strcpy(trendstr, " hit");
}
}
// write the computed value to PWM
analogWrite(VctlPWMOutputPin, adjusted_PWM_output);
gpsdatetostrings();
must_adjust_DAC = false; // clear flag and we are done
} // end adjustVctlPWM_2
change adjustVctlPWM() to adjustVctlPWM_2() in the calling function
good night all.
A.