/* AVRfloat_test.ino */
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Floating point Test 6...7 digits max!");
float t=90.0;
float Change=-2.0;
float k, m;
float GradientDecrease, GradientIncrease, Offset, Vset, PWM_float;
unsigned int PWM;
// GradientIncrease
k = (0.6-1.013)/(76.8-24.6);
m=1.013-k*24.6;
GradientIncrease=k*t+m;
// GradientDecrease
k=(-0.68 - -0.875)/(76.8-24.6);
m=-0.875-k*24.6;
GradientDecrease=-(k*t + m);
// Offset
k=(0.308-0.255)/(76.8-24.6);
m=0.255-k*24.6;
Offset=k*t+m;
if(Change < 0) Vset=Offset+Change*GradientDecrease;
else Vset=Offset+Change*GradientIncrease;
Serial.print("Voltage: ");
Serial.println(Vset);
// PWM 10 bit
k=1024.0/5.0;
m=1024.0-k*2.5;
PWM_float=k*Vset+m;
PWM=(int) PWM_float;
Serial.print("PWM setting: ");
Serial.println(PWM);
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10);
}