Index: trunk/Arduino/projects/OT_boiler/OT_boiler.ino
===================================================================
--- trunk/Arduino/projects/OT_boiler/OT_boiler.ino	(revision 31)
+++ trunk/Arduino/projects/OT_boiler/OT_boiler.ino	(revision 32)
@@ -2,4 +2,6 @@
 #include <Ethernet.h>
 #include <OT_Slave.h>
+
+#include "BoilerDefinition.h"
 
 //#define ACTIVITY_LED_PIN	LED_BUILTIN
@@ -32,14 +34,19 @@
 	long		cntVal;
 
-	long		pwm;
-
-  long    a2Val;
-	long		a2Dir;		// Boiler (VC-40) active detection
-  bool  a2Rising;
+	float		qRequest;
+	float		qCurrent;	// debug only ???
+	int		pwmRequest;
+	int		pwmCurrent;
+
+	long		a2Val;
+	long		a2Dir;		// Boiler (VC-506E) active detection
+	bool		a2Rising;
 
 	void		ReadData ();
 	void		CalcData ();
 
-	virtual double	AD2Temperature (int);
+	static double	Func_T_of_A (int a, int pin);
+	static double	Func_Q789_of_T (double t);
+	static double	Func_PWM_of_Q789 (double q);
 
 public:
@@ -100,21 +107,13 @@
 #endif
 	boiler.maintain();
-#ifdef _NEVER_
-//  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 2000);
-	unsigned long periode = msTicks / (boiler.IsActive() ? 2000 : 2000);
-	static unsigned long prev = 0;
-	if (prev != periode) {
-		prev = periode;
-#else
-  static unsigned long limit = 0;
-  if (!limit) {
-    unsigned long interval = boiler.IsActive() ? 500 : 2000;
-    limit = ((msTicks / interval) + 1 ) * interval;
-  }
-  static unsigned long periode = 0;
-  if (msTicks >= limit) {
-    limit = 0;
-    periode += 1;    
-#endif
+	static unsigned long limit = 0;
+	if (!limit) {
+		unsigned long interval = boiler.IsActive() ? 500 : 2000;
+		limit = ((msTicks / interval) + 1 ) * interval;
+	}
+	static unsigned long periode = 0;
+	if (msTicks >= limit) {
+		limit = 0;
+		periode += 1;    
 		int led = (periode & 1) ? HIGH : LOW;
 		if (Serial) {
@@ -137,6 +136,6 @@
 			Serial.print("\tDir(A2): ");
 			Serial.print(boiler.a2Dir);
-      Serial.print("\tRising(A2): ");
-      Serial.print(boiler.a2Rising);
+			Serial.print("\tRising(A2): ");
+			Serial.print(boiler.a2Rising);
 			float tReturn = boiler.GetReturnTemperature();
 			Serial.print("\tA3: ");
@@ -149,12 +148,9 @@
 			Serial.print(boiler.a5);
 			Serial.print("\tPWM: ");
-//			long _pwm = (msTicks / 20000) % 256;
-//			long _pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
-//			analogWrite(PWM_PIN, _pwm);
-			Serial.print(boiler.pwm);
+			Serial.print(boiler.pwmCurrent);
 			Serial.print("\tReq: ");
 			Serial.print(boiler.tRequest);
 			Serial.print("\tQ(A): ");
-			Serial.println(double(boiler.a5)/double(boiler.a4));
+			Serial.println(boiler.qCurrent);
 		}
 		digitalWrite(ACTIVITY_LED_PIN, led);
@@ -225,28 +221,8 @@
 }
 
-OT_Boiler::OT_Boiler ()
-:
-	a2(0),
-	a3(0),
-	a4(0),
-	a5(0),
-	cnt(0),
-	cntVal(0),
-  a2Val(0),
-	a2Dir(0),
-  a2Rising(false)
-{
-}
-
-void	OT_Boiler::maintain ()
-{
-	ReadData();
-	OT_Slave::maintain();
-}
-
 void	OT_Boiler::ReadData ()
 {
 	long a2Prev = a2Val;
-  a2Val = analogRead(A2);
+	a2Val = analogRead(A2);
 	a2 += a2Val;
 	a3 += analogRead(A3);
@@ -261,7 +237,7 @@
 			} else {
 				a2Dir -= 1;
-        if (a2Dir < -5) {
-          a2Rising = false;
-        }
+				if (a2Dir < -5) {
+					a2Rising = false;
+				}
 			}
 		} else {
@@ -270,7 +246,7 @@
 			} else {
 				a2Dir += 1;
-        if (a2Dir > 5) {
-          a2Rising = true;
-        }
+				if (a2Dir > 5) {
+					a2Rising = true;
+				}
 			}
 		}
@@ -290,9 +266,53 @@
 }
 
-double	OT_Boiler::AD2Temperature (int x)
-{
-	const double m = 0.1816;
-	const double b = -19.234;
-	return m*double(x)+b;
+double	OT_Boiler::Func_T_of_A (int a, int pin)
+{
+	double x(a);
+// 	'Messung.xlsx'::Messung-3.1.3M
+	const double a0(-19.234);
+	const double a1(0.1816);
+	return a0 + x * a1;
+}
+
+double	OT_Boiler::Func_Q789_of_T (double t)
+{
+//	'Heizung.xlsx'::'789-Calc'
+	const double a0(0.4283);
+	const double a1(0.0009);
+	const double a2(0.00006);
+	const double a3(-0.0000003);
+	return a0 + t * (a1 + t * (a2 + t * a3));
+}
+
+double	OT_Boiler::Func_PWM_of_Q789 (double q)
+{
+//	'Heizung.xlsx'::'789-Mess'
+	const double a0(-0.9097);
+	const double a1(164.44);
+	return a0 + q * a1;
+}
+
+OT_Boiler::OT_Boiler ()
+:
+	a2(0),
+	a3(0),
+	a4(0),
+	a5(0),
+	cnt(0),
+	cntVal(0),
+	qRequest(0),
+	qCurrent(0),
+	pwmRequest(0),
+	pwmCurrent(0),
+	a2Val(0),
+	a2Dir(0),
+	a2Rising(false)
+{
+}
+
+void	OT_Boiler::maintain ()
+{
+	ReadData();
+	OT_Slave::maintain();
 }
 
@@ -304,9 +324,18 @@
 void	OT_Boiler::SetRequestTemperature (float t)
 {
-	bool update(t != tRequest);
+	bool update = (t != tRequest);
 	OT_Slave::SetRequestTemperature(t);
-	if (update) {
-		pwm = long(0.9097+164.44*(0.4283+boiler.tRequest*(0.0009+boiler.tRequest*(0.00006-boiler.tRequest*0.0000003))));
-		analogWrite(PWM_PIN, pwm);
+	qRequest = Func_Q789_of_T(tRequest);
+	pwmRequest = Func_PWM_of_Q789(qRequest);
+	CalcData();
+	qCurrent = a5/a4;
+	int diff = round(100.0 * (qCurrent - qRequest));
+	if (diff) {
+		if (update) {
+			pwmCurrent = pwmRequest;
+		} else {
+			pwmCurrent += ((diff > 0) ? -1 : 1);
+		}
+		analogWrite(PWM_PIN, pwmCurrent);
 	}
 }
@@ -315,5 +344,5 @@
 {
 	CalcData();
-	tBoiler = AD2Temperature(a2);
+	tBoiler = Func_T_of_A(a2,A2);
 	return tBoiler;
 }
@@ -322,5 +351,5 @@
 {
 	CalcData();
-	tReturn = AD2Temperature(a3);
+	tBoiler = Func_T_of_A(a3,A3);
 	return tReturn;
 }
