Quentin Scholler / Mbed 2 deprecated MicroteslameterImplemantationVersion

Dependencies:   C12832 mbed

Fork of MicroteslameterV2 by Olivier Dubrulle

Revision:
1:50fbbe10dea4
Parent:
0:d35d9bdef147
diff -r d35d9bdef147 -r 50fbbe10dea4 main.cpp
--- a/main.cpp	Mon May 15 15:30:56 2017 +0000
+++ b/main.cpp	Mon Aug 21 13:50:02 2017 +0000
@@ -4,36 +4,101 @@
 #include "MLX90393.h"
 #define FIELD_LIMIT 100
 
-// Global variable:
+
+
+//******************************************************************************************************************
+                                                    // Global variable:
+//******************************************************************************************************************
 
-PwmOut r (p23); //PWM output for the RBG led
-PwmOut g (p24); //PWM output for the RBG led
-PwmOut b (p25); //PWM output for the RBG led
-AnalogIn p1(p19); // define analog input on p19 just for debug purpose
+DigitalOut LedRGR(p9);
+DigitalOut LedRGG(p10);
+DigitalOut LedG1(p17);
+DigitalOut LedG2(p18);
+DigitalOut LedG3(p19);
+DigitalOut LedG4(p20);
+DigitalOut LedG5(p21);
+DigitalOut LedG6(p22);
+DigitalOut LedG7(p23);
+DigitalOut LedR1(p24);
+DigitalOut LedR2(p25);
+DigitalOut LedR3(p26);
+
+AnalogIn BatteryVolt(p15); //  analog input on p15 for getting Battery level
 C12832 lcd(p5, p7, p6, p8, p11); //define pin for LCD screen
+
+
 InterruptIn button(p14); // button used for zeroing - joystick
+InterruptIn button2(p12); // button used for change Display on screen
+InterruptIn button3(p13);
 I2C i2c(p28, p27);
 Serial pc(USBTX, USBRX);
 Timeout to1; // timeer...
 MLX90393 sensor(MLX90393::i2c_address,&i2c);
 
 float CorrX,CorrY,CorrZ;
-bool Led_on=false;
-bool Blink_started=false;
-bool CleanScreen=false;
+bool Led_on = false;
+bool Blink_started = false;
+bool CleanScreen = false;
+bool IsConnected = true;
+int ScreenDisplay = 1;
+int const NbValHist=5;
+float SaveData[NbValHist];
+int DataTable (0);
+float modulus;
 
 
 
+ //****************************************************************************************************************** 
+ //****************************************************************************************************************** 
+                                        // Begining of the Main function
+ //****************************************************************************************************************** 
+ //****************************************************************************************************************** 
+
+ 
 int main() 
 {
-    float X,Y,Z,modulus,T;
+    float X,Y,Z,T,X2,Y2,Z2,BatLevel;
     int content_buffer[63];
     char read_buffer[11];
-
+    
     init();    
+                                    // Wake up Messages
+ //****************************************************************************************************************** 
+     
     lcd.cls(); // clean screen
-    button.rise(&startZeroing); //link button on the joystick with the start of zeroing
-     
+    lcd.locate(0,12);
+    lcd.printf("Micro Tesla Meter");
+    wait(1.5);
+    lcd.cls();
+                                    // interruption Button for zeroing and choice of display
+ //******************************************************************************************************************     
+ 
+ 
+    button.rise(&startZeroing);                //link button on the joystick with the start of zeroing
+    button2.rise(&changeDisplay);
+    button3.rise(&saveData);
+    
+    lcd.cls();      
+    updateLCD_2(X,Y,Z,modulus);                //Display the opperator mode ( default)
+    
+    
+                                    // Get and display on led the level of the battery 
+ //******************************************************************************************************************      
+    BatLevel = BatteryVolt.read();
+    if (BatLevel> 0.77)
+    {
+        LedRGR = 0;
+        LedRGG = 1;
+    }
+    else 
+    {
+        LedRGR = 1;
+        LedRGG = 0;
+    }
+    
+    
+    
+        
     pc.printf("Microteslameter");
     if(0)//activate deactivate memory dump
     {    
@@ -47,59 +112,362 @@
                         }
      }               
     //sensor.WR(read_buffer, 2, 2016, 0); //put DIF_FILT at max
-       
+  
+  
+                                    
+//******************************************************************************************************************                                    
+//******************************************************************************************************************  
+                                     // Begining of the infinite loop
+//******************************************************************************************************************    
+//******************************************************************************************************************  
+    
     while(1) 
     {
         //X=p1.read()*200; //use potentiometer for debug
         //Y=0;
         //Z=0;    
-        
+ 
+                                    // Get and display on led the level of the battery 
+ //****************************************************************************************************************** 
+        BatLevel = BatteryVolt.read();
+        if (BatLevel> 0.77)
+        {
+            LedRGR = 0;
+            LedRGG = 1;
+        }
+        else 
+        {   
+            LedRGR = 1;
+            LedRGG = 0;
+        }
+ 
+                                     // Get measurement of the tesla sensor 
+ //******************************************************************************************************************        
         MeasureXYZT(&X,&Y,&Z,&T);
         CorrectXYZ(&X,&Y,&Z); // correct for zeroing and convert LSB in uT       
         modulus=calculateModulus(X,Y,Z);
+        IsConnected = isConnected(&X,&Y ,&Z, &X2,&Y2,&Z2);         // verifie if the sensor is or insnt connected
         //--------------- interface----------------
-        setColor(normalize(modulus));       
-        updateLCD(X,Y,Z,modulus);
-        pc.printf("T= %f,X= %f, Y= %f, Z= %f\n",T,X,Y,Z);       
+        setColor(normalize(modulus));                             // display the correct Led value  
+        //lcd.printf("%f ",BatLevel);  // debug purpose
+        if (IsConnected)                       
+        {
+            
+            if ( ScreenDisplay == 0)
+            {
+                updateLCD_1(X,Y,Z,modulus);
+            } 
+            else if (ScreenDisplay == 1) 
+            { 
+                updateLCD_2(X,Y,Z,modulus);
+            }
+            else if (ScreenDisplay == 2) 
+            { 
+                updateLCD_3(SaveData[0],SaveData[1],SaveData[2],modulus);
+                //updateLCD_2(X,Y,Z,modulus);
+            }
+            
+            //lcd.printf("isConnected");
+        }
+        else 
+        {
+            lcd.cls();
+            lcd.locate(0,12);
+            lcd.printf(" the sensor isn't well connected");
+            lcd.cls();
+            //lcd.printf("isntConnected");
+            
+    
+        } 
+        pc.printf("T= %f,X= %f, Y= %f, Z= %f\n",T,X,Y,Z);  
+        
     }
 }
 
 
+
+
+
+
+
+
+
+                                    //Function init the variable
+ //******************************************************************************************************************
 void init(void)
 {    
-    b=1; // RGB led off
-    r=1; // RGB led off
-    g=1; // RGB led off
+    LedG1 =0;
+    LedG2 =0;
+    LedG3 =0;
+    LedG4 =0;
+    LedG5 =0;
+    LedG6 =0;
+    LedG7 =0;
+    LedR1 =0;
+    LedR2 =0;
+    LedR3 =0;
+    
+    
     i2c.frequency(7000);  
 }
 
+                                    //Function detecting if the sensor is or isnt connected 
+ //******************************************************************************************************************
+ 
+bool isConnected (float *X, float *Y,float *Z,float *X2, float *Y2,float *Z2)
+{
+    float DiffX,DiffY, DiffZ;
+    bool isConnected;
+     
+     DiffX = (*X)-(*X2);
+     DiffY = (*Y)-(*Y2);
+     DiffZ = (*Z)-(*Z2);
+     
+     /*lcd.cls();
+     lcd.locate(0,0);
+     lcd.printf("X= %4.1fuT",DiffX);
+     lcd.locate(0,10);
+     lcd.printf("Y= %4.1fuT",DiffY);
+     lcd.locate(0,20);
+     lcd.printf("Z= %4.1fuT",DiffZ);
+     */
+     if (DiffX > 1000 || DiffY > 1000 || DiffZ > 1000)
+     {
+         isConnected = false;
+     }
+     else 
+     {
+        isConnected = true;
+     }
+     *X2 = *X;
+     *Y2 = *Y;
+     *Z2 = *Z; 
+     return isConnected;
+}
+
 
-void setColor(int intensity) // give intensity color from green 0% to red 100%
+void setColor(int intensity) // Display the corresponding Leds
 {
-    float duty_cycle;
-    if(intensity>=100) //if intensity if greater than 100 blink the led in red
+    if (IsConnected)
     {
-      if(Blink_started==false) to1.attach(&blink, 0.2); //attach timer to the blink function to start blinking the led, only if this was not done previously
+        to1.detach();
+        Blink_started=false;
+        if(intensity<=10) //if intensity if greater than 100 blink the led in red
+        {
+            LedG1 =1;
+            LedG2 =0;
+            LedG3 =0;
+            LedG4 =0;
+            LedG5 =0;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;
+        }
+        else if (intensity<=20) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =0;
+            LedG4 =0;
+            LedG5 =0;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=30) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =0;
+            LedG4 =0;
+            LedG5 =0;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=40) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =0;
+            LedG5 =0;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=50) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =0;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=60) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =0;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=70) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =1;
+            LedG7 =0;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=80) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =1;
+            LedG7 =1;
+            LedR1 =0;
+            LedR2 =0;
+            LedR3 =0;      
+       }
+        else if (intensity<=90) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =1;
+            LedG7 =1;
+            LedR1 =1;
+            LedR2 =0;
+            LedR3 =0;      
+        }
+        else if (intensity<=100) 
+        {
+            LedG1 =1;
+            LedG2 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =1;
+            LedG7 =1;
+            LedR1 =1;
+            LedR2 =1;
+            LedR3 =0;      
+        }
+        else     {
+            LedG1 =1;
+            LedG2 =1;
+            LedG3 =1;
+            LedG4 =1;
+            LedG5 =1;
+            LedG6 =1;
+            LedG7 =1;
+            LedR1 =1;
+            LedR2 =1;
+            LedR3 =1;      
+        }
     }
     else 
     {
-        to1.detach(); //detach timer to stop blinking
-        Blink_started=false;    
+        if(Blink_started==false) to1.attach(&blink, 0.2);
     }
-    
-    duty_cycle=100-intensity;
-    duty_cycle=duty_cycle/100;
-    r=duty_cycle; //balance between red and green depending on intensity
-    g=1-duty_cycle;
+        
+
+
 }
 
-void updateLCD(float X,float Y,float Z,float modulus)
+                                                    //LCD Display options
+ //******************************************************************************************************************
+void changeDisplay()
 {
-         if(CleanScreen==true) // clean screen in case the screen displayed something else
+    if ( ScreenDisplay == 0)
+    {
+        ScreenDisplay = 1 ;
+        lcd.cls();
+        lcd.locate(0,14);
+        lcd.printf("Display opperator mode"); 
+        wait(1.5);
+    }
+    else if ( ScreenDisplay == 1 ) 
+    {
+        ScreenDisplay = 2;
+        lcd.cls();
+        lcd.locate(0,14);
+        lcd.printf("Display Historical mode");
+        wait(1.5);
+    }
+    else if ( ScreenDisplay == 2 ) 
+    {
+        ScreenDisplay = 0;
+        lcd.cls();
+        lcd.locate(0,14);
+        lcd.printf("Display debugger mode");
+        wait(1.5);
+    }
+    CleanScreen = true;
+} 
+
+void saveData()
+{
+    SaveData[DataTable] = modulus;
+    
+    switch (DataTable){
+        
+    case 0  : //or 3 or 4: change for bigger screen
+        DataTable++;
+        break;
+        
+    case 1  : //or 3 or 4: change for bigger screen
+        DataTable++;
+        break;
+        
+    case 2  : //or 3 or 4: change for bigger screen
+        DataTable++;
+        break;
+        
+    case 3 : 
+        DataTable = 0;
+        break;
+    }
+}
+
+void updateLCD_1(float X,float Y,float Z,float modulus)                   // display the dubugger screen!
+{
+        if(CleanScreen==true) // clean screen in case the screen displayed something else
         {   
             lcd.cls();
             CleanScreen=false;    
         }
+        
         //lcd.setmode(XOR); 
         lcd.line(lcd.width()/2, 0, lcd.width()/2, lcd.height(), 1); // vertical line to separate field to norm...
         lcd.locate(0,0);
@@ -124,6 +492,67 @@
         }           
         wait(0.010);   // needed?      
     
+}  
+
+void updateLCD_2(float X,float Y,float Z,float modulus)                           // Display the operator screen
+{
+         if(CleanScreen==true) // clean screen in case the screen displayed something else
+        {   
+            lcd.cls();
+            CleanScreen=false;    
+        }
+        //lcd.setmode(XOR); 
+        
+        lcd.locate( 0,(lcd.height()/2));
+        lcd.printf("n= %7.1fuT",modulus);
+        if(modulus>FIELD_LIMIT)
+        {
+            lcd.locate((lcd.width()/2)+20,15);
+            lcd.printf("ALERT");
+            lcd.print_bm(bitmWarning,(lcd.width()/2)+2,15); // print warning sign
+            lcd.copy_to_lcd();           // update lcd    
+        } 
+        else
+        {
+            lcd.locate((lcd.width()/2)+2,15);
+            lcd.printf("           ");  // clean the warning sign             
+        }           
+        wait(0.010);   // needed?      
+    
+}  
+
+void updateLCD_3(float SaveData0, float SaveData1 ,float SaveData2 ,float modulus)                   // display the dubugger screen!
+{
+        if(CleanScreen==true) // clean screen in case the screen displayed something else
+        {   
+            lcd.cls();
+            CleanScreen=false;    
+        }
+        
+        //lcd.setmode(XOR); 
+        lcd.line(lcd.width()/2, 0, lcd.width()/2, lcd.height(), 1); // vertical line to separate field to norm...
+        lcd.locate(0,0);
+        lcd.printf("N1= %5.1fuT",SaveData0);
+        lcd.locate(0,10);
+        lcd.printf("N2= %5.1fuT",SaveData1);
+        lcd.locate(0,20);
+        lcd.printf("N3= %5.1fuT",SaveData2);
+        lcd.locate((lcd.width()/2)+5,0);
+        lcd.printf("n= %5.1fuT",modulus);
+        if(modulus>FIELD_LIMIT)
+        {
+            lcd.locate((lcd.width()/2)+20,15);
+            lcd.printf("ALERT");
+            lcd.print_bm(bitmWarning,(lcd.width()/2)+2,15); // print warning sign
+            lcd.copy_to_lcd();           // update lcd    
+        } 
+        else
+        {
+            lcd.locate((lcd.width()/2)+2,15);
+            lcd.printf("           ");  // clean the warning sign             
+        }           
+        wait(0.010);   // needed?      
+    
 }    
 
 
@@ -143,21 +572,44 @@
     return (field/FIELD_LIMIT )*100;
 }
 
+                                    //Make blink the leds
+ //******************************************************************************************************************
 void blink()
 {
     if(Led_on==false) 
     {
-        r=0;//led on in red
+        LedG1 =0;
+        LedG2 =0;
+        LedG3 =0;
+        LedG4 =0;
+        LedG5 =0;
+        LedG6 =0;
+        LedG7 =0;
+        LedR1 =0;
+        LedR2 =0;
+        LedR3 =0;//led on in red
         Led_on=true;
     }
     else 
     {
-        r=1; //led off
+        LedG1 =1;
+        LedG2 =1;
+        LedG3 =1;
+        LedG4 =1;
+        LedG5 =1;
+        LedG6 =1;
+        LedG7 =1;
+        LedR1 =1;
+        LedR2 =1;
+        LedR3 =1; //led off
         Led_on=false;    
     }
     to1.attach(&blink, 0.2);   
 }
 
+
+                                    //Function Measuring the magnetic field
+ //******************************************************************************************************************
 void MeasureXYZT(float *X,float *Y,float *Z, float *T)
 {
     char read_buffer[11];
@@ -177,12 +629,17 @@
     if(*Z>32768) *Z-=65536;
 }
 
-void startZeroing()
+
+                                    //Make the Zero
+ //******************************************************************************************************************
+void startZeroing() 
 {
+    to1.attach(&blink, 0.2);
     float X,Y,Z,T;
     int iteration=20;// to be defined
     //char read_buffer[11];
     CorrX=0;CorrY=0;CorrZ=0;
+    SaveData[0]=SaveData[1]=SaveData[2]=SaveData[3]=SaveData[4]=0;
     lcd.cls();
     
     for(int i=0;i<iteration;i++)
@@ -194,6 +651,7 @@
         lcd.locate(0,0);
         lcd.printf("ZEROING progress: %i%%",i*100/iteration);
     }
+    to1.detach();
     CorrX/=iteration;
     CorrY/=iteration;
     CorrZ/=iteration;
@@ -212,6 +670,9 @@
     return 4.698; //fix value for the moment...    for RES_XYZ=3 and GAIN_SEL=4
 }
 
+
+                //Function Correct the magnetic field depending on the the zeroing and the corresponding gain
+ //******************************************************************************************************************
 void CorrectXYZ(float *X,float *Y,float *Z) // correct for zeroing and convert LSB in uT
 {
     *X-=CorrX;