Heater for threaded program

Dependents:   LEX_Threaded_Programming

Revision:
11:785a0329f802
Parent:
10:0e16d8430d66
Child:
12:8a048f111140
Child:
13:8629d78fd93b
--- a/Heater.cpp	Mon Jul 22 10:46:26 2019 +0000
+++ b/Heater.cpp	Tue Jul 23 08:04:51 2019 +0000
@@ -14,6 +14,8 @@
 extern Timer timer;
 extern DigitalIn adc_busy;
 extern MODSERIAL pc;  
+extern int log_count;
+extern float R_avg;
 
 
     
@@ -23,12 +25,16 @@
 float Heater::R_to_T(float R) {return R*corr_grad + corr_int;}
 float Heater::T_to_R(float T) {return (T - corr_int)/corr_grad;}
 
+void Heater::output()
+{
+    pc.printf("%d,%f,%f\n",timer.read_ms(),R_to_T(R),R);
+}
 
 void Heater::read()
 {
     //Reads R and then resets the drive back to its previous value
-    timer.reset();
     int i = 0;
+
     int drive_prev = drive;     //Store previous value of drive
     drive = 1;
     wait_us(MEAS_DELAY);        //Wait for ADC to settle
@@ -50,6 +56,12 @@
     if (v<0) {pc.printf("v is %d",v);}
     //if (curr > 0) {R = (float)v/curr;}        //Avoid dividing by 0
     R = (float)v/curr;
+    log_count++;
+    if (log_count > 0)
+    {
+    log_count = 0;
+    output();
+    }
 }
 
 
@@ -64,14 +76,16 @@
     while (timer.read_ms() < end_time)
     {
         read();
-        if (R > R_ref)
+        R_avg = (((N_ROLL_AVG - 1) * R_avg) + R)/N_ROLL_AVG;
+        if (R_avg > R_ref)
         {
             drive = 0;
-            wait_ms(10);  //Minimum duty cycle of 10%
+            wait_ms(5);  //Minimum duty cycle of 10%
         }
         else
         {
             drive = 1;
+            wait_ms(5);
         }
   
     }