#include <Arduino.h>
#include <LiquidCrystal.h>
//select the pin for the green fragment of the LED
int greenpin = 11;
//select the pin for the red fragment of the LED
int redpin = 6;
// select the pin for the blue fragment of the LED
int bluepin = 3;
//Define an integer variable val
int val;
int zaehler = 0;
int Shock = 2;
LiquidCrystal lcd(8, 9, 10, 5, 12, 13);

//                 0    1    2    3    4    5    6    7    8    9   10   11   12   13   14
float rot[] =   {213, 255, 255, 255, 255, 255, 255, 169,  85,   0,   0,   0,   0,   0,   0};
float gruen[] = {  0,  43,  85, 128, 169, 213, 255, 255, 255, 213, 170, 128,  85,   0,   0};
float blau[] =  {  0,   0,   0,   0,   0,   0,   0,   0,  85, 128, 213, 255, 254, 254, 169};

void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  Serial.begin(9600);
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(Shock, INPUT);
}

// select the pin for the red LED

//Set redpin, bluepin, greenpin output type

int modus = 1;
float mischen(float phWert, float *liste) 
{
  float u = liste[(int)phWert];
  float o = liste[(int)phWert + 1];
  float frac = phWert - (int)phWert;
  float ergebnis = o * frac + u * (1 - frac);
  return ergebnis;
}

#define SensorPin 0 //pH meter Analog output to Arduino Analog Input 0
#define calibration 0.03 //deviation compensate
unsigned long int avgValue; //Store the average value of the sensor feedback

float kalibr = 0.0;
float ph(float voltage) 
{
  return -5.70 * (voltage - kalibr) + 21.56; //(Kalibriert mit Poinger Leitungswasser auf 7,46 -> 2.47 V)
}

void loop()
{

  zaehler++;
  if (zaehler == 4) {
    if (modus == 1) {
      modus = 2;
    }
    else {
      modus = 1;
    }
    zaehler = 0;
  }

  int buf[10]; //buffer for read analog
  for (int i = 0; i < 10; i++) //Get 10 sample value fromthe sensor for smooth the value
  {
    buf[i] = analogRead(SensorPin);
    delay(10);
  }
  for (int i = 0; i < 9; i++) //sort the analog fromsmall to large
  {
    for (int j = i + 1; j < 10; j++)
    { if (buf[i] > buf[j])
      {
        int temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }
  avgValue = 0;
  for (int i = 2; i < 8; i++) //take the average value of 6 center sample
    avgValue += buf[i];

  float voltage = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into Volt

  if(digitalRead(Shock) == LOW)
  {
    kalibr = voltage - 2.47;
    modus = 3;
  }

  float phWert = ph(voltage);
  int dimmer = 13;

  analogWrite(redpin, (int)mischen(phWert, rot) / dimmer);
  analogWrite(bluepin, (int)mischen(phWert, blau) / dimmer);
  analogWrite(greenpin, (int)mischen(phWert, gruen) / dimmer);
  Serial.println(mischen(phWert, rot));

  lcd.setCursor(0, 0);
  lcd.print("pH=");
  lcd.print(phWert);
  lcd.print("  ");

  if (modus == 1) {
    if (phWert < 10.2 && phWert > 9.50) {
      lcd.setCursor(0, 1);
      lcd.print("Waschpulver!          ");
    }
    else if (phWert < 7.10 && phWert > 6.50) {
      lcd.setCursor(0, 1);
      lcd.print("Kaffee!            ");
    }
    else if (phWert < 7.70 && phWert > 7.20) {
      lcd.setCursor(0, 1);
      lcd.print("Wasser!        ");
    }
    else if (phWert < 7.15 && phWert > 7.10) {
      lcd.setCursor(0, 1);
      lcd.print("Kaliumchlorid!       ");
    }
    else if (phWert < 5.44 && phWert > 4.80) {
      lcd.setCursor(0, 1);
      lcd.print("Sprudelwasser!       ");
    }
    else if (phWert < 7.27 && phWert > 6.90) {
      lcd.setCursor(0, 1);
      lcd.print("Salzwasser!    ");
    }
    else if (phWert < 6.90 && phWert > 6.60) {
      lcd.setCursor(0, 1);
      lcd.print("Milch!           ");
    }
    else if (phWert < 3.20 && phWert > 2.50) {
      lcd.setCursor(0, 1);
      lcd.print("Essig!          ");
    }
    else if (phWert < 14.0 && phWert > 13.00) {
      lcd.setCursor(0, 1);
      lcd.print("Natriumhydroxid!          ");
    }
    else {
      lcd.setCursor(0, 1);
      lcd.print("Unbekannt!           ");
    }
  }


  if (modus == 2) {
    if (phWert < 2.80 && phWert > 0.00) {
      lcd.setCursor(0, 1);
      lcd.print("Sehr Sauer!            ");
    }
    else {
      if (phWert < 5.60 && phWert > 2.80) {
        lcd.setCursor(0, 1);
        lcd.print("Sauer!           ");
      }
      else {
        if (phWert < 7.60 && phWert > 6.50) {
          lcd.setCursor(0, 1);
          lcd.print("Neutral!             ");
        }
        else {
          if (phWert < 10.20 && phWert > 8.00) {
            lcd.setCursor(0, 1);
            lcd.print("Alkalisch!                ");
          }
          else {
            if (phWert < 14.00 && phWert > 10.20) {
              lcd.setCursor(0, 1);
              lcd.print("Sehr Alkalisch!             ");
            }
            else {
              lcd.setCursor(0, 1);
              lcd.print("Unbekannt!            ");
            }
          }
        }
      }
    }
  }
Serial.println(modus);

  if (modus == 3) {
    lcd.setCursor(0, 1);
    lcd.print("Kalibriert!     ");
  }

  delay(600);
}