for HTTP-3A

Dependencies:   mbed TextLCD

Revision:
6:572219c7c378
Parent:
5:1d817b3c42f1
Child:
7:e22e9fd1a526
diff -r 1d817b3c42f1 -r 572219c7c378 main.cpp
--- a/main.cpp	Thu Jul 11 12:24:29 2019 +0000
+++ b/main.cpp	Thu Jul 11 13:01:52 2019 +0000
@@ -2,6 +2,7 @@
 #include "TextLCD.h"
 #define VMAX 0.36f
 #define VMIN 0.05f
+#define AVGNUM 20
 
 
 Serial  pc(USBTX, USBRX, 115200);
@@ -17,7 +18,9 @@
 bool armed = false;
 
 AnalogIn   res(PA_0);
-float volt = 0;
+float volt[AVGNUM];
+float voltAVG = 0;
+int index = 0;
 
 
 //I2C i2c_lcd(PB_7,PB_6); // SDA, SCL
@@ -38,11 +41,27 @@
 {
     if(armed)
     {
+        int CMD = 90 * (voltAVG-VMIN)/(VMAX-VMIN);
+        if (CMD > 90)
+            CMD = 90;
+        else if (CMD < 0)
+            CMD = 0;
+        data_msg[0] = (char)CMD;
+        data_msg[1] = (char)CMD;
+        data_msg[2] = (char)CMD;
+        can_msg_send = CANMessage(0x111,data_msg,3,CANData,CANStandard);
+        can1.write(can_msg_send); 
+    }
+    else
+    {
+        data_msg[0] = 0;
+        data_msg[1] = 0;
+        data_msg[2] = 0;
         can_msg_send = CANMessage(0x111,data_msg,3,CANData,CANStandard);
         can1.write(can_msg_send); 
     }
     
-    //printf("res: %f\n", res.read());
+    //printf("res: %f\n", voltAVG);
 }
 
 int main()
@@ -52,8 +71,6 @@
     can1.attach(&CAN_RX1, CAN::RxIrq);
     ticker1.attach(&sendCMD, 1);
     pc.printf("start\n");
-
-    
     
     lcd.setCursor(TextLCD::CurOff_BlkOff);
     lcd.setBacklight(TextLCD::LightOn); 
@@ -62,9 +79,26 @@
     lcd.setUDC(2, (char *) cross);
     pc.printf("set done\n\r");
     
+    for(int i = 0; i<AVGNUM; i++)
+    {
+        volt[i] = 0;   
+    }
+    
     while(1)
     {   
-        volt = res.read();
+        volt[index] = res.read();
+        index++;
+        if (index>=AVGNUM)
+            index = 0;
+          
+        float tmp = 0;  
+        for(int i = 0; i<AVGNUM; i++)
+        {
+          tmp = tmp + volt[i];   
+        } 
+        voltAVG = tmp/AVGNUM;
+        
+        
         showBar();
         showArm();
         
@@ -86,7 +120,7 @@
 
 void showBar(void)
 {
-    int bars = 16 * (volt-VMIN)/(VMAX-VMIN);    
+    int bars = 16 * (voltAVG-VMIN)/(VMAX-VMIN);    
     if (bars > 16)
         bars = 16;
     else if( bars < 0 )