#include <Arduino.h>
#include <U8g2lib.h>

//****************************************************************************************//
//           Constante de temps et broches de connexion des capteurs                      //
//****************************************************************************************//

const int pause_affichage_ms = 100;      // temps de pause d'affichage en milliseconde

#define TDS_Sensor_Pin_Num A2           // TDS sensor on Analog pin A2
#define Turbidity_Sensor_Pin_Num A3     // Turbidity sensor on Analog pin A3
#define pH_Sensor_Pin_Num A4            // pH sensor on Analog pin A4
#define DS18S20_Pin_Num_v2 D2           // DS18B20 sensor on digital pin D2                      <======= Informations des connexions
#define DHT_Sensor_Pin_Num D6           // DHT on Digital pin D6
#define LED_Pin_Num D7                  // LED on Digital pin D7
//****************************************************************************************//

// Données pour le capteur pH meter          ==> site DFRobot
const int numMeasure = 10;
char pH_str[8];

float offset_pH = 0;                   // valeur standard = -1.8 avec la carte pH alimentée en 3.3V et -2.5 avec carte alimentée en 5V

// Configuration de l'afficheur SSD1315
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(/* rotation=*/ U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);    //Low spped I2C
/*
  U8g2lib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.   
*/


void get_pH()
{
  int buf[10],temp;
  
  for(int i=0;i<numMeasure;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(pH_Sensor_Pin_Num);
    delay(10);
  }
  
  for(int i=0;i<numMeasure-1;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<numMeasure;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  
  unsigned long int sumValue = 0;  //Store the average value of the sensor feedback

  for(int i=2;i<numMeasure-2;i++)                      //take the average value of 6 center sample
    sumValue+=buf[i];
  
  float phValue=(float)sumValue*5.0/4096/(numMeasure-4); //convert the analog into millivolt
//  phValue=3.5*phValue + offset_pH2;                      //convert the millivolt into pH value avec carte pH alimentée en 5v
  phValue=5.3*phValue + offset_pH;                      //convert the millivolt into pH value avec carte pH alimentée en 3.3v
  
  dtostrf(phValue, 5, 1, pH_str);
 
  Serial.print("pH_2 value: ");  
  Serial.println(phValue,2);

}



void setup()
{
  // put your setup code here, to run once:
  
  Serial.begin(115200);
  
  Serial.println("**********************************************");
  Serial.println("Setup Serial monitor OK");

  u8g2.begin();
  Serial.println("Setup écran OLED OK");

  pinMode(pH_Sensor_Pin_Num,INPUT);             // Set pH sensor pin to input mode
 
  pinMode(LED_Pin_Num, OUTPUT);                 // Set LED Pin to output mode

  Serial.println("End of setup");
  Serial.println("**********************************************");
}

void loop()
{
  // put your main code here, to run repeatedly:   
 
  get_pH();

  // affichage des données

  u8g2.setFont(u8g2_font_ncenB08_tr);   // choose a suitable font

//****************************************************************************************//
// écran OLED SSD1315 :
// fonction U8g2.drawStr(n°colonne,n°ligne,string)

  u8g2.clearBuffer();                     // clear the internal memory
  u8g2.drawStr(0,10,"Station eau");       // write something to the internal memory  
  u8g2.drawStr(0,40,"pH :");              // affichage du texte "pH :"
  u8g2.drawStr(20,40,pH_str);               // affichage de la valeur du pH
  u8g2.sendBuffer();                      // transfer internal memory to the display

  Serial.println();

}
