 /*
 Sketch for Arduino Uno included in the Open ISEmeter project (v:3.2)
 
 Authors J.D. Mozo and C. Salvador from the www.uhu.es/oswh group
 
 Released unther the Creative Commons Atribution-NonCommectial-ShareAlike 3.0 license (CC BY-NC-SA 3.0)
 
 Arduino Uno is supplied with a home-made shield to read the Open Circuit voltage from an Ion Selective Electrode
 or any chemical battery avoiding the consum of current by a high-impedande differential reading system
 It also includes a set of low-pass filters you can selected by code towards the USB port and
 a programable-gain differential amplifier to read de fem of cell.
 Arduino paces the adquisition by controlling time between readings and send data to PC by USB 
 */
 
 // variable declaration
 int gain = 0;                        // set gain to x1
 char serialData = 0;                 // stores data read from USB (one byte 0 to 255)
 unsigned long previousTime = 0;      // stores previous read time in microseconds
 unsigned long currentTime = 0;       // stores actual time in microseconds
 unsigned long interval = 333333;     // stores interval-between-reads in microseconds 
 boolean Adquire = LOW;               // stores if acquisition is enabled
 int volt = 0;                        // stores the voltage value from analog channel
 
 // pin assignation
 const int unflt = 0;                 // analog channel for reading unfiltered voltage (positive only)
 const int fltp = 1;                  // analog channel for reading positive filtered voltage
 const int fltn = 2;                  // analog channel for reading negative filtered voltage
 const int G0 = 7;                    // TTL line for LSB gain setting
 const int G1 = 6;                    // TTL line for MSB gain setting

 
 /* USB command set
 gn     == Gain (n = 0 to 3  )        gain setting for PGA204
 r      == GO Run                     start analog adquisition
 s      == STOP run                   stop adquisition
 va;b,c == Volt (a,b,c = 0 to 1024)   send the readed voltage values to USB (0 to 5 volts)
                                      (a unfiltered, b filtered(+), c filtered(-))
 */
 
 
 void setup() {
   // init digital pins as output for setting PGA204's gain 
   pinMode(G0,OUTPUT);
   pinMode(G1,OUTPUT);
   digitalWrite(G0,LOW);
   digitalWrite(G1,LOW);

   // init serial port (USB)
   Serial.begin(9600);
 }
 
 void loop() {
   // first read the actual time 
   currentTime = micros();
   
   // check if data is waiting on serial port
   while (Serial.available() > 0) {
     serialData = Serial.read();
     switch (serialData) {
       case 'g':
         gain = Serial.parseInt();         // read numerical value for gain 
         gain = constrain(gain, 0, 3);
         Serial.print("Ok, g");            // handshaking "Ok, gn"
         Serial.println(gain,DEC);   
         // change gain setting
         switch (gain) {
          case 0:
            digitalWrite(G0,LOW);
            digitalWrite(G1,LOW);
            break;
          case 1:
            digitalWrite(G0,HIGH);
            digitalWrite(G1,LOW);
            break;
          case 2:
            digitalWrite(G0,LOW);
            digitalWrite(G1,HIGH);
            break;
          case 3:
            digitalWrite(G0,HIGH);
            digitalWrite(G1,HIGH);
            break;
         }
         break;
      case 'r':
         Adquire = HIGH;
         Serial.print("Ok ,");
         Serial.println(serialData);
         break;
       case 's':
         Adquire = LOW;
         Serial.print("Ok ,");
         Serial.println(serialData);
         break;
     }
     int serialDataTmp = Serial.read();    // empty serial port (CR)
   }
   
   // check if reading time is ellapsed
   if (currentTime - previousTime >= interval && Adquire == HIGH) {
     previousTime = currentTime;           // stores actual time (RESET time)
     Serial.print("v");
     volt = analogRead(unflt);             // read not filtered voltage
     Serial.print(volt,DEC);
     Serial.print(";");
     volt = analogRead(fltp);              // read positive filtered voltage
     Serial.print(volt,DEC);
     Serial.print(",");
     volt = analogRead(fltn);              // read negative filtered voltage
     Serial.println(volt,DEC);
   }
 }
  





