/*---------------------------------------------------------------------------------------------------------------------------------------------------
  Author:       Ryan Depace
                
  Date:         9/26/13
   
  Program:      Power and Temp Monitor

  Description:  This Program takes four measurements and dispalys them on a 20x4 LCD - voltmeter, Current, Temp and Power
  
  Hardware:     Arduino Uno, Voltage Dividers, AD623 Instrumentation Amplifier, Shunt
 
  REV:          4/30/14
------------------------------------------------------------------------------------------------------------------------------------------------------*/                


#define NUM_SAMPLES 10               // number of analog samples to take per reading, per channel

#define DIV_1         3.126            // calibration values
#define DIV_2         82.162
#define SHUNT_OHMs    2.3

#define V_REF    4.991               // ADC reference voltage / calibration value

    double Thermister(int RawADC) {
    double Temp;
    Temp = log(((10240000/RawADC) - 10000));
    Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
    Temp = Temp - 275.0;             // Convert Kelvin to Celcius
    return Temp;
}

int sum[2] = {0};                    //  sums of samples taken
unsigned char sample_count = 0;      //  sample number
float Measurement[2] = {0.0};        //  calculated measurements
char l_cnt = 0;                      //  Counter used in 'for' loops

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,10,5,4,3,2);

void setup()
{
  lcd.begin(20,4);                     //  Colums, rows. 20x4 LCD
  lcd.clear();                         //  starts with a blank screen
  lcd.setCursor(0,0);                  //  sets cursor to column 0, row 0 (the first row)
  lcd.print("RJD PROJECT");     
  lcd.setCursor(0,1);                
  lcd.print("THREMAL ELECTRIC");       
  lcd.setCursor(0,2); 
  lcd.print("USB CHARGER, WELCOME");  
  lcd.setCursor(0,3);  
  lcd.print("PLS PLUG IN DEVICE");
  delay(7000);
  lcd.clear();   
}

void loop()
{
   
    while (sample_count < NUM_SAMPLES) {             // take a number of analog samples and add them up
      
        for (l_cnt = 0; l_cnt < 4; l_cnt++) {        // sample each channel A0 to A1
            sum[l_cnt] += analogRead(A0 + l_cnt);
        }
        sample_count++;
        delay(100);
    }
   
    for (l_cnt = 0; l_cnt < 4; l_cnt++) {
        Measurement[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;  // calculate the measurements for each Analog channel
    }
    double Current = 0;                        //Calibrating Current Measurment
    Current = Measurement[3] * DIV_2;
    
    double Voltage = 0;                        //Calibrating Voltage Measurment
    Voltage = Measurement[0] * DIV_1;
//----------------------------------------------------------------
 double Overheat = 90;                         // 90 Degrees C, MAX TEMP = 138 Degrees C
 double fTemp;
    double temp = Thermister(analogRead(2));   // Read sensor
    fTemp = (temp * 1.8) + 32.0;               // Convert to USA
    
  if(temp < Overheat){                        // If temprature exceeds 90 Degrees C warn the user If below display measuremnets
    
    lcd.clear(); 
    lcd.setCursor(0, 0);
    lcd.print("----MEASUREMENTS----");
    
    lcd.setCursor(0, 1);                       //Display Voltage
    lcd.print(" V:"); 
    lcd.print(Voltage, 1);
    
    lcd.setCursor(0, 2);                       //Display Current
    lcd.print("mA:");
    lcd.print(Current, 1);
    
    lcd.setCursor(0, 3);                       //Display Power
    lcd.print("mW:");
    lcd.print(Current * Current * SHUNT_OHMs , 1);

    lcd.setCursor(11, 1);                      //Display Temp C and F
    lcd.print("TEMP:"); 
    lcd.setCursor(11, 2);    
    lcd.print(fTemp);
    lcd.setCursor(17, 2); 
    lcd.print(" F");
    lcd.setCursor(11, 3); 
    lcd.print(temp);
    lcd.setCursor(17, 3); 
    lcd.print(" C");
    
    sample_count = 0;
    
    for (l_cnt = 0; l_cnt < 2; l_cnt++) {
       sum[l_cnt] = 0;
   }
  }
 else{                                        //Warns the user that they are over 90 Degrees C
    lcd.clear(); 
    delay(550);
    lcd.setCursor(0,0);
    lcd.print("WARNING !!");
    lcd.setCursor(13, 0);                     //Display Temp
    lcd.print("TEMP:"); 
    lcd.setCursor(13, 1);    
    lcd.print(fTemp);
    lcd.print(" F");
    lcd.setCursor(13, 2);
    lcd.print(temp);
    lcd.print(" C");
    lcd.setCursor(0, 1);
    lcd.print("PLS SHTDWN");
    lcd.setCursor(0, 2);
    lcd.print("OVERHEATING");
    delay(2000);
   }
//----------------------------------------------------------------    

}
