#include <Wire.h>
#define adc 0x68

void setup() {
//Setup Comms
    Serial.begin(9600);           // PC to Arduino comms  
    Serial.println("Read Hitachi F-1050");

    Wire.begin();
    Wire.beginTransmission(adc);
    Wire.write(0x1C);  
        /*AD Converter Configuration Notes: 
        8 bit configuration 
        bit 7; 1 initiates a new conversion in 1 shot mode - see bit 4
        bit 6,5; channel select  00 = 1, 11 = 4
        bit 4;   1= continuous conversion, 0= 1 shot
        bit 3,2; 00 = 240 sps 12 bit, 01 60 sps 14 bit, 10 = 15 sps 16 bit, 11 = 3.75 sps 18 bit
        bit 1,0; gain 00=x1, 01=x2, 10=x4, 11= x8
        so 1C (00011100) is chanel 1, continuous, 18 bit, 1x gain
           1D (00011101) is chanel 1, continuous, 18 bit, 2x gain
           1E (00011110) is chanel 1, continuous, 18 bit, 4x gain
        default ADC range is +/1 2.048 V.
        */
    Wire.endTransmission();
    delay(500);    
}


void loop() {
//  Serial.println("Get Volt");
//  for (int i=1;i<=10;i++) {
    Get_Cond();
    delay(500);
//    Serial.println();
// }
}

        //Get Cond - Calculate Conductivity from Detector Board
    float Get_Cond()
        {
        // in 18 bit mode the adc returns 3 bytes  
//        uint8_t clockvar[8];                            // Variable  is 8 byte array with clock info
        byte a,b,c,d;
        long count;
        float volt;
        Wire.requestFrom(adc,4);                            // 18 bit request sends 4 bytes, others send 3
            a= (Wire.read());                            // bits 17, 16
            b= (Wire.read());                            // bits 15-8
            c= (Wire.read());                            // bits 7-0
            d= (Wire.read());                            // status register
//            Serial.print(int(a));
//            Serial.print(' ');
//            Serial.print(int(b));
//            Serial.print(' ');
//            Serial.print(int(c));
//            Serial.print(' ');
//            Serial.println(d,BIN);
            // 2's complement base 17 math to take bytes to counts
        if(a<2)
            {
            count = long(a)*256L*256L+long(b)*256L+long(c);
            }
        else if (a==255) 
            {
            // a = 255 so bit 17 is set
            count = -pow(2,17)+ 256L*256L+long(b)*256L+long(c);    // generate negative values
            }
        else
            {
            // a== 254 and bit 17 not set
            count = -pow(2,17)+ long(b)*256L+long(c);       // generate negative values
            }
            //Serial.println(count);
            // counts * 15.625 uV/count in 18 bit mode - see table 4-1, page 15  
            // counts * 62.5 uV/count in 16 bit mode
            // counts * 250 uV/count in 14 bit mode
            // counts * 1000 uV/count in 12 bit mode

        //Factor in Gain from AD Converter
        volt = (count*15.625)/1e6;                   
//        Read_Clock(clockvar);                          // Get the time
        //return volt;                                   // PG added after file commands below commented out.
        //PrintDateTime();                    // print date and time to serial port
        //Serial.println();
//        Serial.print("Count is:  ");
//        Serial.println(count);
//        Serial.print("Volt is:  ");
        Serial.println(volt,5);  
        }
 
