/*#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 64    // OLED display height, in pixels
#define OLED_RESET -1       // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

byte values[11];
SoftwareSerial mod(2, 3);

*/
// Nitrogen
int cornN  []    = {98, 54, 55, 82,  105, 43, 76, 42, 46, 42, 27, 24, 42, 68, 65, 56, 90, 27, 31, 44};
int sugarcanN [] = {31, 31, 27, 54,  59, 19, 53, 47, 53, 27, 43, 51, 33, 33, 54, 32, 59, 43, 46, 50};
int eggplantN [] = {90 , 58, 59, 45,  20, 28, 36, 20, 14, 19, 12, 44, 31, 16, 41, 54, 28, 44, 47, 54};
int tomatoN [] =   {72, 108, 36, 14,   15, 37, 32, 95,  21, 75, 69, 8,  17, 19, 18, 20, 26, 37, 18, 14};

// P
int cornP []     = { 35, 19, 19, 29, 37, 15, 27, 15,  16, 15, 10, 8, 15, 24, 23, 20, 32, 9, 11, 15};
int sugarcanP [] = { 11, 11, 9, 19,  27, 6, 19, 17,   17, 9, 15, 14, 21, 23, 25, 28, 32, 12, 15, 23 };
int eggplantP [] = { 32, 20, 21, 16, 7, 10, 13, 7,    5, 6, 4, 15,   11, 5, 14, 19,  10, 16, 23, 23 };
int tomatoP []   = { 25, 38, 12, 5,  8, 13, 11, 33,   7, 26, 24, 2,  6, 7, 7, 11,    9, 16, 6, 5};

// K
int cornK []     = {49, 27, 27, 41, 52, 21, 38, 21, 23, 21, 14, 12,  21, 34, 32, 28, 46, 13, 15, 22};
int sugarcanK [] = {15, 15, 13, 27, 29, 9, 27, 24,  25, 13, 9, 22,   16, 18, 11, 16, 9, 10, 21, 18};
int eggplantK [] = {45, 20, 29, 22, 10, 14, 18, 10, 7, 9, 6, 22,     15, 8, 20, 27,  14, 22, 27, 17};
int tomatoK []   = {36, 54, 18, 7,  8, 18, 16, 47,  10, 37, 34, 4,   8, 10, 11, 11,  13, 23, 8, 7};

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};


void setup() {
  Serial.begin(9600);
  /*
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  delay(500);
  display.clearDisplay();
  display.setCursor(25, 15);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println(" NPK Sensor");
  display.setCursor(25, 35);
  display.setTextSize(1);
  display.print("Initializing");
  display.display();
  delay(3000);
  */
}

bool samevalueN1= false;
bool samevalueN2= false;
bool samevalueN3= false;
bool samevalueN4= false;

int matchindex1=-1;
int matchindex2=-1; 
int matchindex3=-1; 
int matchindex4=-1; 

void loop()
{int valueN1= analogRead(A0);
valueN1= map(valueN1, 0, 1024, 0, 120);
Serial.print ("valueN= ");
Serial.print (valueN1);
delay(100);
int valueP1= analogRead(A1);
valueP1= map(valueP1, 0, 1024, 0, 100);
Serial.print ("valueP= ");
Serial.print (valueP1);
int valueK1= analogRead(A2);
valueK1= map(valueK1, 0, 1024, 0, 100);
Serial.print ("valueK= ");
Serial.println (valueK1);
for (int i=0; i<20; i++)
{
 if (valueN1 == cornN[i])
 { if ((valueP1 == cornP[i]))
 {
 if ((valueK1 == cornK[i]))
 {
  samevalueN1= true; 
   matchindex1=i;  
 }
 }
 }
 if (valueN1 == sugarcanN[i])
 { if ((valueP1 == sugarcanP[i]))
 {
 if ((valueK1 == sugarcanK[i]))
 {
  samevalueN2= true; 
   matchindex2=i;  
 }
 }
 }

  if (valueN1 == eggplantN[i])
 { if ((valueP1 == eggplantP[i]))
 {
 if ((valueK1 == eggplantK[i]))
 {
  samevalueN3= true; 
   matchindex3=i;  
 }
 }
 }

   if (valueN1 == tomatoN[i])
 { if ((valueP1 == tomatoP[i]))
 {
 if ((valueK1 == tomatoK[i]))
 {
  samevalueN4= true; 
   matchindex4=i;  
 }
 }
 }
 }

if ((samevalueN1) && (samevalueN2) && (samevalueN3))
{
  Serial.println("corn, sugarcan or eggplant");
}

else if ((samevalueN2) && (samevalueN3))
{
  Serial.println("sugarcan, eggplant" );
}

else if (samevalueN1)
{
  Serial.println("corn");
}
else if (samevalueN2)
{
  Serial.println("sugarcan");
}
else if (samevalueN3)
{
  Serial.println("eggplant");
}

else if (samevalueN4)
{
  Serial.println("tomato");
}
else
{
Serial.println("no match");
}

resetvalues();
}

void resetvalues()
{
samevalueN1=false;
samevalueN2=false;
samevalueN3=false;
samevalueN4 =false;
matchindex1=-1;
matchindex2=-1; 
matchindex3=-1; 
matchindex4=-1;
}

void printing ()
{
Serial.print(samevalueN1);
Serial.print(samevalueN2);
Serial.print(samevalueN3);
Serial.println(samevalueN4);
Serial.print(matchindex1);
Serial.print(",");
Serial.print(matchindex2);
Serial.print(",");
Serial.print(matchindex3);
Serial.print(",");
Serial.print(matchindex4);
}