Index: trunk/Arduino/projects/OT_boiler/OT_boiler.ino
===================================================================
--- trunk/Arduino/projects/OT_boiler/OT_boiler.ino	(revision 29)
+++ trunk/Arduino/projects/OT_boiler/OT_boiler.ino	(revision 31)
@@ -3,15 +3,16 @@
 #include <OT_Slave.h>
 
-//#define ACTIVITY_LED_PIN  LED_BUILTIN
-#define ACTIVITY_LED_PIN  9
-
-#define USE_ETHERNET  0
+//#define ACTIVITY_LED_PIN	LED_BUILTIN
+#define	ACTIVITY_LED_PIN	9
+#define PWM_PIN		10
+
+#define USE_ETHERNET	0
 
 #define CONSOLE(cmd)  do { if (Serial) Serial.cmd; } while (0)
 
-void  Sent_OT_Data (const OT_Action& data);
-
-byte  mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
-byte  server[] = { 192, 168, 0, 22 };
+void	Sent_OT_Data (const OT_Action& data);
+
+byte	mac[] = { 0x02, 0xDE, 0xAD, 0xDE, 0xAD, 0x01 };
+byte	server[] = { 192, 168, 0, 22 };
 
 volatile long  loops = 0;
@@ -24,16 +25,34 @@
 
 protected:
-	int		a2;
-  int   a3;
-  int   a4;
-  int   a5;
-
-  virtual double AD2Temperature (int);
+	long		a2;
+	long		a3;
+	long		a4;
+	long		a5;
+	long		cnt;
+	long		cntVal;
+
+	long		pwm;
+
+  long    a2Val;
+	long		a2Dir;		// Boiler (VC-40) active detection
+  bool  a2Rising;
+
+	void		ReadData ();
+	void		CalcData ();
+
+	virtual double	AD2Temperature (int);
 
 public:
+	OT_Boiler ();
+
+	virtual void	maintain ();
+
+	virtual bool	IsActive ();
+
+	virtual void	SetRequestTemperature (float);
 	virtual float	GetBoilerTemperature ();
 	virtual float	GetReturnTemperature ();
 
-friend void loop();
+	friend void	loop ();
 };
 
@@ -42,175 +61,266 @@
 EthernetClient	client;
 
-void setup() 
-{
-  msTicks = millis();
-  digitalWrite(ACTIVITY_LED_PIN, LOW);	// Built-in LED off
-  pinMode(ACTIVITY_LED_PIN, OUTPUT);		// Built-in LED active
-  // Console
-  Serial.begin(19200);
-  CONSOLE(println("\nSystem starting..."));
-  // Setup OT
-  CONSOLE(print("OpenTherm.begin..."));
-  boiler.begin(Sent_OT_Data);
-  CONSOLE(println("done"));
+void	setup() 
+{
+	msTicks = millis();
+	digitalWrite(ACTIVITY_LED_PIN, LOW);	// Activity LED off
+	pinMode(ACTIVITY_LED_PIN, OUTPUT);	// Activity LED active
+	pinMode(PWM_PIN, OUTPUT);		// PWM output
+	// Console
+//	Serial.begin(19200);
+	Serial.begin(115200);
+	CONSOLE(println("\nSystem starting..."));
+	// Setup OT
+	CONSOLE(print("OpenTherm.begin..."));
+	boiler.begin(Sent_OT_Data);
+	CONSOLE(println("done"));
 #if USE_ETHERNET
-  // Setup Ethernet
-  CONSOLE(print("Ethernet.begin..."));
-  Ethernet.begin(mac);
-  CONSOLE(println("done"));
+	// Setup Ethernet
+	CONSOLE(print("Ethernet.begin..."));
+	Ethernet.begin(mac);
+	CONSOLE(println("done"));
 #else
-  CONSOLE(println("Ethernet is disabled!"));
+	CONSOLE(println("Ethernet is disabled!"));
 #endif
 }
 
-void loop() 
-{
-  loops++;
+void	loop() 
+{
+	loops++;
 /*
-  Serial.print("[");
-  Serial.print(loops);
-  Serial.print("|");
-  Serial.print(state,HEX);
-  Serial.println("]");
+	Serial.print("[");
+	Serial.print(loops);
+	Serial.print("|");
+	Serial.print(state,HEX);
+	Serial.println("]");
 */
-  msTicks = millis();
+	msTicks = millis();
 #if USE_ETHERNET
-  maintainEthernet();
+	maintainEthernet();
 #endif
-  boiler.maintain();
+	boiler.maintain();
 #ifdef _NEVER_
-  unsigned long base = boiler.IsActive() ? 500 : 3000;
-  unsigned long periode = msTicks / base;
-  int led = (periode & 1) ? HIGH : LOW;
-  if (Serial) {
-	  static unsigned long prev = 0;
-	  if (prev != periode) {
-		  prev = periode;
-		  Serial.print("Time: ");
-		  Serial.print(msTicks);
-		  Serial.print("\tPeriode: ");
-		  Serial.print(periode);
-		  Serial.print("\tLED: ");
-		  Serial.println(led);
-	  }
+//  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;
   }
-  digitalWrite(ACTIVITY_LED_PIN, led);
-  //digitalWrite(ACTIVITY_LED_PIN, boiler.IsActive() ? HIGH : LOW);
-#else
-  unsigned long periode = msTicks / (boiler.IsActive() ? 500 : 1000);
-  static unsigned long prev = 0;
-  if (prev != periode) {
-	  prev = periode;
-	  int led = (periode & 1) ? HIGH : LOW;
-	  if (Serial) {
-		  Serial.print("Time: ");
-		  Serial.print(msTicks);
-		  Serial.print("\tPeriode: ");
-		  Serial.print(periode);
-		  Serial.print("\tLED: ");
-		  Serial.print(led);
-      float tBoiler = boiler.GetBoilerTemperature();
-		  Serial.print("\tA2: ");
-		  Serial.print(boiler.a2);
-		  Serial.print("\tT(A2): ");
-		  Serial.print(tBoiler);
-      float tReturn = boiler.GetReturnTemperature();
-      Serial.print("\tA3: ");
-      Serial.print(boiler.a3);
-      Serial.print("\tT(A3): ");
-      Serial.print(tReturn);
-      boiler.a4 = analogRead(A4);   // read the input pin
-      Serial.print("\tA4: ");
-      Serial.print(boiler.a4);
-      boiler.a5 = analogRead(A5);   // read the input pin
-      Serial.print("\tA5: ");
-      Serial.println(boiler.a5);
-	  }
-	  digitalWrite(ACTIVITY_LED_PIN, led);
-  }
+  static unsigned long periode = 0;
+  if (msTicks >= limit) {
+    limit = 0;
+    periode += 1;    
 #endif
-}
-
-void maintainEthernet ()
-{
-  // Check for Ethernet hardware present
-  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
-    return;
-  }
-  byte rc = Ethernet.maintain();
-  switch (rc) {
-    case 1: // renew failed
-      CONSOLE(println("Ethernet: renew failed!"));
-      break;
-    case 2: // renew success
-      CONSOLE(println("Ethernet: renew success!"));
-      break;
-    case 3: // rebind failed
-      CONSOLE(println("Ethernet: rebind failed!"));
-      break;
-    case 4: // rebind success  
-      CONSOLE(println("Ethernet: rebind success!"));
-      break;
-  }
-  if (client.connected()) {
-    if (!_connected) {
-      _connected = true;
-      CONSOLE(println("Ethernet: connected!"));  
-    }
-  } else {
-    if (_connected) {
-      _connected = false;
-      CONSOLE(println("Ethernet: connection lost!"));
-    }
-//    CONSOLE(println("Ethernet: restarting..."));
-    client.stop();
-//    CONSOLE(println("Ethernet: connecting..."));
-    client.connect(server, 23456);  
-  }
+		int led = (periode & 1) ? HIGH : LOW;
+		if (Serial) {
+			boiler.CalcData();
+			Serial.print("Time: ");
+			Serial.print(msTicks);
+		  	Serial.print("\tPeriode: ");
+			Serial.print(periode);
+/*
+			Serial.print("\tLED: ");
+			Serial.print(led);
+*/
+			Serial.print("\tCnt: ");
+			Serial.print(boiler.cntVal);
+			float tBoiler = boiler.GetBoilerTemperature();
+			Serial.print("\tA2: ");
+			Serial.print(boiler.a2);
+			Serial.print("\tT(A2): ");
+			Serial.print(tBoiler);
+			Serial.print("\tDir(A2): ");
+			Serial.print(boiler.a2Dir);
+      Serial.print("\tRising(A2): ");
+      Serial.print(boiler.a2Rising);
+			float tReturn = boiler.GetReturnTemperature();
+			Serial.print("\tA3: ");
+			Serial.print(boiler.a3);
+			Serial.print("\tT(A3): ");
+			Serial.print(tReturn);
+			Serial.print("\tA4: ");
+			Serial.print(boiler.a4);
+			Serial.print("\tA5: ");
+			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("\tReq: ");
+			Serial.print(boiler.tRequest);
+			Serial.print("\tQ(A): ");
+			Serial.println(double(boiler.a5)/double(boiler.a4));
+		}
+		digitalWrite(ACTIVITY_LED_PIN, led);
+	}
+}
+
+void	maintainEthernet ()
+{
+	// Check for Ethernet hardware present
+	if (Ethernet.hardwareStatus() == EthernetNoHardware) {
+		return;
+	}
+	byte rc = Ethernet.maintain();
+	switch (rc) {
+		case 1: // renew failed
+			CONSOLE(println("Ethernet: renew failed!"));
+			break;
+		case 2: // renew success
+			CONSOLE(println("Ethernet: renew success!"));
+			break;
+		case 3: // rebind failed
+			CONSOLE(println("Ethernet: rebind failed!"));
+			break;
+		case 4: // rebind success  
+			CONSOLE(println("Ethernet: rebind success!"));
+			break;
+	}
+	if (client.connected()) {
+		if (!_connected) {
+			_connected = true;
+			CONSOLE(println("Ethernet: connected!"));  
+		}
+	} else {
+		if (_connected) {
+			_connected = false;
+			CONSOLE(println("Ethernet: connection lost!"));
+		}
+//		CONSOLE(println("Ethernet: restarting..."));
+		client.stop();
+//		CONSOLE(println("Ethernet: connecting..."));
+		client.connect(server, 23456);  
+	}
 }
 
 void	Sent_OT_Data (const OT_Action& data)
 {
-  state = OPENTHERM::getMode();
-  if (client.connected()) {
-    client.print("[");
-    client.print(loops);
-    client.print("|");
-    client.print(state,HEX);
-    client.print("]\t");
+	state = OPENTHERM::getMode();
+	if (client.connected()) {
+		client.print("[");
+		client.print(loops);
+		client.print("|");
+		client.print(state,HEX);
+		client.print("]\t");
 /*
-    client.print(data.type,HEX);
-    client.print(":");
-    client.print(data.id,HEX);
-    client.print(":");
-    client.print(data.valueHB,HEX);
-    client.print(":");
-    client.print(data.valueLB,HEX);
+		client.print(data.type,HEX);
+		client.print(":");
+		client.print(data.id,HEX);
+		client.print(":");
+		client.print(data.valueHB,HEX);
+		client.print(":");
+		client.print(data.valueLB,HEX);
 */
-    client.print(data.GetRequestValue(),HEX);
-    client.print("\t");
-    client.print(data.GetResponseValue(),HEX);
-    client.println("");
-  }
-}
-
-double OT_Boiler::AD2Temperature (int x)
-{
-  const double m = 0.2059;
-  const double b = -27.951;
-  return m*double(x)+b;
-}
-
-float OT_Boiler::GetBoilerTemperature ()
-{
-	a2 = analogRead(A2);   // read the input pin
+		client.print(data.GetRequestValue(),HEX);
+		client.print("\t");
+		client.print(data.GetResponseValue(),HEX);
+		client.println("");
+	}
+}
+
+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);
+	a2 += a2Val;
+	a3 += analogRead(A3);
+	a4 += analogRead(A4);
+	a5 += analogRead(A5);
+	cnt++;
+	// Calc A2 direction...
+	if (a2Val != a2Prev) {
+		if (a2Val < a2Prev) {
+			if (a2Dir > 0) {
+				a2Dir = -1;
+			} else {
+				a2Dir -= 1;
+        if (a2Dir < -5) {
+          a2Rising = false;
+        }
+			}
+		} else {
+			if (a2Dir < 0) {
+				a2Dir = 1;
+			} else {
+				a2Dir += 1;
+        if (a2Dir > 5) {
+          a2Rising = true;
+        }
+			}
+		}
+	}
+}
+
+void	OT_Boiler::CalcData ()
+{
+	if (cnt > 1) {
+		cntVal = cnt;
+		a2 /= cnt;
+		a3 /= cnt;
+		a4 /= cnt;
+		a5 /= cnt;
+		cnt = 1;
+	}
+}
+
+double	OT_Boiler::AD2Temperature (int x)
+{
+	const double m = 0.1816;
+	const double b = -19.234;
+	return m*double(x)+b;
+}
+
+bool	OT_Boiler::IsActive ()
+{
+	return a2Rising;
+}
+
+void	OT_Boiler::SetRequestTemperature (float t)
+{
+	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);
+	}
+}
+
+float	OT_Boiler::GetBoilerTemperature ()
+{
+	CalcData();
 	tBoiler = AD2Temperature(a2);
 	return tBoiler;
 }
 
-float OT_Boiler::GetReturnTemperature ()
-{
-  a3 = analogRead(A3);   // read the input pin
-  tReturn = AD2Temperature(a3);
-  return tReturn;
-}
+float	OT_Boiler::GetReturnTemperature ()
+{
+	CalcData();
+	tReturn = AD2Temperature(a3);
+	return tReturn;
+}
