Help with digital calibration of analogue signal

Hello again. As promised i'm back... ;)

I've inserted and altered the code from the while loop tutorial together with the calibrate function into my code and did some testing. First of all i could start the calibrate function by pressing a button which was configured to digital pin 2, but got some strange results. So i put some Serial.prints in the function to see what the sensorMin and sensorMax values were during calibration. When i put the pH probe in a solution with pH7 i got the value 509 and when i put the probe in a pH4 solution i got the value of 336. So i added two Serial.prints (sensorMax & sensorMin) after the other Serials to se what happened after the calibration and when the calibration was over the values for sensorMax and sensorMin changed to 336 and 340 which is very strange. I also tinkered with the toLow and toHigh values of the mapping and think they should be the values of the pH i'm calibrating to, correct me if i'm wrong, which should be for pH4 --> (4 + 1.889) / 0.0178 = 330.842 and for pH7 --> (7 + 1.889) / 0.0178 = 499.382 Also, i created another int called sensorValueMap to see if the value for sensorValue was different before and after...and it did, so the conversion did happen. I think the problem lies in the values for sensorMax and sensorMin, but i'm not sure anymore...

Also did my best to position the code, so it reads easier ;)

/*
Arduino Mega 2560 Hydroponic Controller V.1
Purpose:
          -Monitor pH level of nutrient tank with pH probe and dose (pH-/pH+) wit two 
            peristaltic pumps.
          -Monitor fluidlevel in the nutrient tank with float switch. In case it gets
            to low a solenoid valve opens up and fils the tank. 
          -Measure temperature and humidity. If both get to high a fan removes hot and
            humid air.
            
Todo's: 
          -Measure amount of light received during the day. (Primairely used for 
           loggin purposes
          -Use second float switch to close off solenoid valve so i don't have to use
           a timerfunction.
          -Monitor and control EC value's. ie, When waterlevel & EC are low the 
            solenoid valve opens up while fertilizer is added.
            
Code found on the WWW. Thanks to everybody who contributed. You know who you are ;)
*/

//Define PIN's
#define dht_dpin 69           //Analog PIN 15
#define pHPin 59              //Analog PIN 5
#define pHPlusPin 10
#define pHMinPin 11
#define ventilatorPin 9
#define floatPin 7
#define solenoidPin 8
#define DHTTYPE DHT11
#define indicatorLedPin 13
#define buttonPin 2

byte compFunc; //for passing error code back from complex functions.
byte dht_dat[4]; //Array to hold the bytes sent from sensor.

int sensorMin = 1023;          //minimum sensor value
int sensorMax = 0;             //maximum sensor value
int sensorValue = 0;           //the sensor value
int sensorValueMap = 0;      //mapped sensor value


void setup(){

pinMode(pHPlusPin, OUTPUT);    //setting PIN's as output
pinMode(pHMinPin, OUTPUT);
pinMode(ventilatorPin, OUTPUT);
pinMode(solenoidPin, OUTPUT);
  
InitDHT();                    //Initializing for reading DHT

Serial.begin(9600);

delay(300);                   //Must wait one second for DHT chip to stabilize. This is
                              //the first part 0.3 seconds.

Serial.println("Luchtvochtigheid, temperatuur & pH");
Serial.println("__________________________________\n\n");

delay(700); //rest of 1 second. Second part 0.7 seconds/

}


void loop(){
  
// while the button is pressed, take calibration readings:
  
while (digitalRead(buttonPin) == HIGH) {    
  calibrate(); 
  }
  
  digitalWrite(indicatorLedPin, LOW);                                // signal the end of the calibration period 
  sensorValue = analogRead(pHPin);                                   // read the sensor:
  sensorValueMap = map(sensorValue, sensorMin, sensorMax, 330, 499);   // apply the calibration to the sensor reading
  

//measuring pH level...
  
  float pH(0.0178 * sensorValueMap - 1.889);                          //Conversionformula for pH

  if (pH >= 6.00)                                                  //If pH gets below 5 or above 6 a 
    {                                                              // corresponding pump gets activated.
    digitalWrite(pHMinPin, HIGH);
    digitalWrite(pHPlusPin, LOW);
    }
      else if (pH <= 5.00)
        {
        digitalWrite(pHPlusPin, HIGH);
        digitalWrite(pHMinPin, LOW);
        }
      

//measuring temperature & humidity.
  
ReadDHT();                                                         //Read from DHT chip
  switch (compFunc){
    case 0:
Serial.print("Luchtvochtigheid = ");
Serial.print(dht_dat[0], DEC);
Serial.println("% ");
Serial.print("Temperatuur = ");
Serial.print(dht_dat[2], DEC);
Serial.println(" *C ");
        break;
     case 1:
        Serial.println("Error 1: DHT start condition 1 not met.");
        break;
     case 2:
        Serial.println("Error 2: DHT start condition 2 not met.");
        break;
     case 3:
        Serial.println("Error 3: DHT checksum error.");
        break;
     default:
        Serial.println("Error: Unrecognized code encountered.");
        break;
      }                                                          //End of switch
        Serial.print("pH = : ");                                 //adding pH to screen
        Serial.println(pH);
        Serial.println(sensorValue);
        Serial.println(sensorValueMap);
        Serial.println(sensorMin);
        Serial.println(sensorMax);

if(dht_dat[0] >= 80 && dht_dat[2] >= 35)
  {
  digitalWrite(ventilatorPin, HIGH);
  Serial.println("Ventilator geactiveerd");
  }
  else
    {
    digitalWrite(ventilatorPin, LOW);
    }
//measuring waterlevel...

int levelValue = LOW;
levelValue = digitalRead(floatPin);
if (levelValue == HIGH)
  {
  digitalWrite(solenoidPin, HIGH);
  Serial.println("Solenoid klep geactiveerd");
  }
  else
    {
    digitalWrite(solenoidPin, LOW);
    }
    
Serial.println(" \n\n"); 
delay(2000);                                                     //Delay can be adjusted to 800ms but better to
                                                                 //set it to 2000ms for more stable reading. 

}


void InitDHT(){
  pinMode(dht_dpin,OUTPUT);
  digitalWrite(dht_dpin,HIGH);
}


void ReadDHT(){
  
          /*Uses global variables dht_dat[0-4], and compFunc to pass
          "answer" back. compFunc=0 if read went okay.
          Depends on global dht_dpin for where to look for sensor.*/
          
compFunc=0;
byte dht_in;
byte i;
                                                                 // Send "start read and report" command to sensor....
                                                                 // First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(50);                                                       //standard is 23

           /*Next line: Brings line high again,
           second step in giving "start read..." command*/
           
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);                                           //DHT22 datasheet says host should
                                                                 //keep line high 20-40us, then watch for sensor taking
                                                                 //line low. That low should last 80us. Acknowledges 
                                                                 //"start read and report" command.

                                                                 //Next: Change Arduino pin to an input, to
                                                                 //watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);

//dht_in=digitalRead(dht_dpin);  //mischien terug opzetten

if(dht_in){
   compFunc=1;                                                   //dht start condition 1 not met
   return;
   }                                                             //end if
delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);

if(!dht_in){
   compFunc=2;                                                   //dht start condition 2 not met
   return;
   }                                                             //end if

            /*After 80us low, the line should be taken high for 80us by the
            sensor. The low following that high is the start of the first
            bit of the forty to come. The routine "read_dht_dat()"
            expects to be called with the system already into this low.*/
            
delayMicroseconds(80);
                                                                 
for (i=0; i<5; i++)                                              //now ready for data reception... pick up the 5 bytes coming from the sensor
   dht_dat[i] = read_dht_dat();

pinMode(dht_dpin,OUTPUT);                                        //Next: restore pin to output duties
digitalWrite(dht_dpin,HIGH);                                     //Next: Make data line high again, as output from Arduino

byte dht_check_sum =                                             //Next see if data received consistent with checksum received
       dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];          
            /*Condition in following "if" says "if fifth byte from sensor
            not the same as the sum of the first four..."*/
            
if(dht_dat[4]!= dht_check_sum)
   {compFunc=3;}                                                 //DHT checksum error
}

void calibrate() {
  

  digitalWrite(indicatorLedPin, HIGH);                           // turn on the indicator LED to indicate that calibration is happening
  sensorValue = analogRead(pHPin);                               // read the sensor
  if (sensorValue > sensorMax) {                                 // record the maximum sensor value
    sensorMax = sensorValue;
    Serial.println(sensorMax);
  
  }

  if (sensorValue < sensorMin) {                                 // record the minimum sensor value
    sensorMin = sensorValue;
    Serial.println(sensorMin);
  }
}

byte read_dht_dat()
{
            /*Collect 8 bits from datastream, return them interpreted
            as a byte. I.e. if 0000.0101 is sent, return decimal 5.
            Code expects the system to have recently entered the
            dataline low condition at the start of every data bit's
            transmission BEFORE this function is called.*/

  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){                                           /*We enter this during the first start bit (low for 50uS) of the byte
                                                                   Next: wait until pin goes high*/
      while(digitalRead(dht_dpin)==LOW);                         /*signalling end of start of bit's transmission.
                                                                   Dataline will now stay high for 27 or 70 uS, depending on
                                                                   whether a 0 or a 1 is being sent, respectively.*/
      delayMicroseconds(45);                                     /*AFTER pin is high, wait further period, to be
                                                                   into the part of the timing diagram where a 0 or a 1 denotes
                                                                   the datum being send. The "further period" was 30uS in the software
                                                                   that this has been created from. I believe that a higher number
                                                                   (45?) might be more appropriate.*/

                                                                  //Next: Wait while pin still high
      if (digitalRead(dht_dpin)==HIGH)
       result |=(1<<(7-i));                                         // "add" (not just addition) the 1 to the growing byte.
    
    
      while (digitalRead(dht_dpin)==HIGH);                          /*Next wait until pin goes low again, which signals the START
                                                                    of the NEXT bit's transmission.*/
   }                                                               //end for
  return result;
}

Oh, i have another question, does a map function have the same function than a pot meter? ie, tone down the signal received?

void calibrate() {
   digitalWrite(indicatorLedPin, HIGH);                           // turn on the indicator LED to indicate that calibration is happening
  sensorValue = analogRead(pHPin);                               // read the sensor
  if (sensorValue > sensorMax) {                                 // record the maximum sensor value
    sensorMax = sensorValue;
    Serial.println(sensorMax);
  
  }

  if (sensorValue < sensorMin) {                                 // record the minimum sensor value
    sensorMin = sensorValue;
    Serial.println(sensorMin);
  }
}

Your calibrate routine only adjusts sensorMin & sensorMax values if they fall outside currently defined range so are not properly calibrating. Also you do not do a two step calibration allowing the probe to be swapped between the low/high pH solutions. To quote myself

pseudo code to do the mapping way would be something like
Serial.println(“Place probe in 4.5pH solution, wait for reading to stabilize then press button”)
While (buttonNotPressed){}
fromLow = analogRead(pHPin)
Serial.println(“Place probe in 8.0pH solution, wait for reading to stabilize then press button”)
While (buttonNotPressed){}
fromHigh = analogRead(pHPin)

You could also do with some form of software/hardware button debounce else program could rip through all the wait for button presses in a single press.

Hey Riva,

Thanks again for your 'pseudo code' :) I've altered my code and will be testing it tonight ;)

void calibrate() {
  
  digitalWrite(indicatorLedPin, HIGH);                           // turn on the indicator LED to indicate that calibration is happening
  sensorValue = analogRead(pHPin);                               // read the sensor
  Serial.println("Place probe in pH 4.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 4.0: ");
  Serial.println(sensorValue);
  while (buttonTwo == LOW){
    sensorMin = analogRead(pHPin);
  }
  Serial.println("Place probe in pH 7.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 7.0: ");
  Serial.println(sensorValue);
  while (buttonTwo == LOW){
    sensorMax = analogRead(pHPin);
  }
}

i'm still working on getting the debounce working... though it is something simple i can't seem to get my head around it :blush:

Your code read and displayed the pHPin before user had pressed calibrate button and displays this value for both pH solutions so I have tweaked it to read & display value after button pressed. Also included is possible code example of how to overcome button debounce (I assume this is what you was referring to). I simply read the button state and if it’s pressed then wait a bit to allow switch bounce to settle and read it again.

const long debounceDelay = 20;                          // Button debounce time

void calibrate() {
  digitalWrite(indicatorLedPin, HIGH);                  // turn on the indicator LED to indicate that calibration is happening
//  sensorValue = analogRead(pHPin);                    // read the sensor
  Serial.println("Place probe in pH 4.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 4.0: ");
  while (checkButton(buttonTwo)){}
  sensorMin = analogRead(pHPin);
  Serial.println(sensorMin);

  Serial.println("Place probe in pH 7.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 7.0: ");
  while (checkButton(buttonTwo)){}
  sensorMax = analogRead(pHPin);
  Serial.println(sensorMax);
}

// Button = pin number to read
boolean checkButton(int Button){
    if (digitalRead(Button) == HIGH) {                  // Is button pressed?
        long Time = millis();                           // Yes
        while ((millis() - Time) < debounceDelay) {     // Wait a bit  to allow for debounce
        }
        if (digitalRead(Button) == HIGH) {              // Read button again
            return true;                                // Still pressed so return true
        }
        else {
            return false;                               // Not pressed so return false
        }
    }
    else {                                              // Button not pressed so no need to wait for debounce
        return false;                                   // Return false
    }
}

Riva,

I would like to start with thanking you (again) helping me with my project! :) I tried your code, but what i don't understand is what to do with 'Button'. So just for testing purposes i removed the debouncing, and tried your code after a bit of alteration. The sensorMin & sensorMax didn't get filled by the mapping until i created another variable called sensorValue2 (sorry, no inspiration :blush:) and then the values got to sensorMin & sensorMax. So the mapping works now, but somehow the new calculation doesn't work like i hoped. When i put the probe in a pH 7.0 solution the value before mapping is 513 and after the mapping it turns to +-1300... :(

/*
Arduino Mega 2560 Hydroponic Controller V.1
Purpose:
          -Monitor pH level of nutrient tank with pH probe and dose (pH-/pH+) wit two 
            peristaltic pumps.
          -Monitor fluidlevel in the nutrient tank with float switch. In case it gets
            to low a solenoid valve opens up and fils the tank. 
          -Measure temperature and humidity. If both get to high a fan removes hot and
            humid air.
            
Todo's: 
          -Measure amount of light received during the day. (Primairely used for 
           loggin purposes
          -Use second float switch to close off solenoid valve so i don't have to use
           a timerfunction.
          -Monitor and control EC value's. ie, When waterlevel & EC are low the 
            solenoid valve opens up while fertilizer is added.
            
Code found on the WWW. Thanks to everybody who contributed. You know who you are ;)
*/

//Define PIN's
#define dht_dpin 69           //Analog PIN 15
#define pHPin 59              //Analog PIN 5
#define pHPlusPin 10
#define pHMinPin 11
#define ventilatorPin 9
#define floatPin 7
#define solenoidPin 8
#define DHTTYPE DHT11
#define indicatorLedPin 13
#define buttonPin 2
#define buttonTwo 3

byte compFunc; //for passing error code back from complex functions.
byte dht_dat[4]; //Array to hold the bytes sent from sensor.

int sensorMin = 1023;          //minimum sensor value
int sensorMax = 0;             //maximum sensor value
int sensorValue = 0;           //the sensor value
int SensoBefore = 0;
int sensorValue2 = 0;

void setup(){

pinMode(pHPlusPin, OUTPUT);    //setting PIN's as output
pinMode(pHMinPin, OUTPUT);
pinMode(ventilatorPin, OUTPUT);
pinMode(solenoidPin, OUTPUT);
  
InitDHT();                    //Initializing for reading DHT

Serial.begin(9600);

delay(300);                   //Must wait one second for DHT chip to stabilize. This is
                              //the first part 0.3 seconds.

Serial.println("Luchtvochtigheid, temperatuur & pH");
Serial.println("__________________________________\n\n");

delay(700); //rest of 1 second. Second part 0.7 seconds/

}


void loop(){
  
// while the button is pressed, take calibration readings:
  
while (digitalRead(buttonPin) == HIGH) {    
  calibrate(); 
  }
  
  digitalWrite(indicatorLedPin, LOW);                              // signal the end of the calibration period 
  SensoBefore = analogRead(pHPin);
  sensorValue = analogRead(pHPin);                                 // read the sensor:
  sensorValue2 = map(sensorValue, sensorMin, sensorMax, 330, 500);  // apply the calibration to the sensor reading
  //sensorValue = constrain(sensorValue, 0, 1023);                   // in case the sensor value is outside the range seen during calibration


//measuring pH level...
  
  sensorValue = analogRead(pHPin);                                 //Read analog pin
  float pH(0.0178 * sensorValue2 - 1.889);                          //Conversionformula for pH

  if (pH >= 6.00)                                                  //If pH gets below 5 or above 6 a 
    {                                                              // corresponding pump gets activated.
    digitalWrite(pHMinPin, HIGH);
    digitalWrite(pHPlusPin, LOW);
    }
      else if (pH <= 5.00)
        {
        digitalWrite(pHPlusPin, HIGH);
        digitalWrite(pHMinPin, LOW);
        }
      

//measuring temperature & humidity.
  
ReadDHT();                                                         //Read from DHT chip
  switch (compFunc){
    case 0:
Serial.print("Luchtvochtigheid = ");
Serial.print(dht_dat[0], DEC);
Serial.println("% ");
Serial.print("Temperatuur = ");
Serial.print(dht_dat[2], DEC);
Serial.println(" *C ");
        break;
     case 1:
        Serial.println("Error 1: DHT start condition 1 not met.");
        break;
     case 2:
        Serial.println("Error 2: DHT start condition 2 not met.");
        break;
     case 3:
        Serial.println("Error 3: DHT checksum error.");
        break;
     default:
        Serial.println("Error: Unrecognized code encountered.");
        break;
      }                                                          //End of switch
        Serial.print("pH = : ");                                 //adding pH to screen
        Serial.println(pH);
        Serial.print("sensor before map :");
        Serial.println(SensoBefore);
        Serial.print("sensor after map :");
        Serial.println(sensorValue2);
        Serial.print("sensor Min :");
        Serial.println(sensorMin);
        Serial.print("sensor Max :");
        Serial.println(sensorMax);

if(dht_dat[0] >= 80 && dht_dat[2] >= 35)
  {
  digitalWrite(ventilatorPin, HIGH);
  Serial.println("Ventilator geactiveerd");
  }
  else
    {
    digitalWrite(ventilatorPin, LOW);
    }
//measuring waterlevel...

int levelValue = LOW;
levelValue = digitalRead(floatPin);
if (levelValue == HIGH)
  {
  digitalWrite(solenoidPin, HIGH);
  Serial.println("Solenoid klep geactiveerd");
  }
  else
    {
    digitalWrite(solenoidPin, LOW);
    }
    
Serial.println(" \n\n"); 
delay(2000);                                                     //Delay can be adjusted to 800ms but better to
                                                                 //set it to 2000ms for more stable reading. 

}


void InitDHT(){
  pinMode(dht_dpin,OUTPUT);
  digitalWrite(dht_dpin,HIGH);
}


void ReadDHT(){
  
          /*Uses global variables dht_dat[0-4], and compFunc to pass
          "answer" back. compFunc=0 if read went okay.
          Depends on global dht_dpin for where to look for sensor.*/
          
compFunc=0;
byte dht_in;
byte i;
                                                                 // Send "start read and report" command to sensor....
                                                                 // First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(50);                                                       //standard is 23

           /*Next line: Brings line high again,
           second step in giving "start read..." command*/
           
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);                                           //DHT22 datasheet says host should
                                                                 //keep line high 20-40us, then watch for sensor taking
                                                                 //line low. That low should last 80us. Acknowledges 
                                                                 //"start read and report" command.

                                                                 //Next: Change Arduino pin to an input, to
                                                                 //watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);

//dht_in=digitalRead(dht_dpin);  //mischien terug opzetten

if(dht_in){
   compFunc=1;                                                   //dht start condition 1 not met
   return;
   }                                                             //end if
delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);

if(!dht_in){
   compFunc=2;                                                   //dht start condition 2 not met
   return;
   }                                                             //end if

            /*After 80us low, the line should be taken high for 80us by the
            sensor. The low following that high is the start of the first
            bit of the forty to come. The routine "read_dht_dat()"
            expects to be called with the system already into this low.*/
            
delayMicroseconds(80);
                                                                 
for (i=0; i<5; i++)                                              //now ready for data reception... pick up the 5 bytes coming from the sensor
   dht_dat[i] = read_dht_dat();

pinMode(dht_dpin,OUTPUT);                                        //Next: restore pin to output duties
digitalWrite(dht_dpin,HIGH);                                     //Next: Make data line high again, as output from Arduino

byte dht_check_sum =                                             //Next see if data received consistent with checksum received
       dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];          
            /*Condition in following "if" says "if fifth byte from sensor
            not the same as the sum of the first four..."*/
            
if(dht_dat[4]!= dht_check_sum)
   {compFunc=3;}                                                 //DHT checksum error
}


const long debounceDelay = 20;                          // Button debounce time

void calibrate() {
  digitalWrite(indicatorLedPin, HIGH);                  // turn on the indicator LED to indicate that calibration is happening
  Serial.println("Place probe in pH 4.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 4.0: ");
  while (digitalRead(buttonTwo) == LOW) {}
  sensorMin = analogRead(pHPin);
  Serial.println(sensorMin);

  Serial.println("Place probe in pH 7.0 solution, wait for reading to stabilize then press button two");
  Serial.print("Sensorvalue for pH 7.0: ");
  while (digitalRead(buttonTwo) == LOW){}
  sensorMax = analogRead(pHPin);
  Serial.println(sensorMax);
  
  
}

////Button = pin number to read
//boolean checkButton(int Button){
//    if (digitalRead(Button) == HIGH) {                  // Is button pressed?
//        long Time = millis();                           // Yes
//        while ((millis() - Time) < debounceDelay) {     // Wait a bit  to allow for debounce
//        }
//        if (digitalRead(Button) == HIGH) {              // Read button again
//            return true;                                // Still pressed so return true
//        }
//        else {
//            return false;                               // Not pressed so return false
//        }
//    }
//    else {                                              // Button not pressed so no need to wait for debounce
//        return false;                                   // Return false
//    }
//}

byte read_dht_dat()
{
            /*Collect 8 bits from datastream, return them interpreted
            as a byte. I.e. if 0000.0101 is sent, return decimal 5.
            Code expects the system to have recently entered the
            dataline low condition at the start of every data bit's
            transmission BEFORE this function is called.*/

  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){                                           /*We enter this during the first start bit (low for 50uS) of the byte
                                                                   Next: wait until pin goes high*/
      while(digitalRead(dht_dpin)==LOW);                         /*signalling end of start of bit's transmission.
                                                                   Dataline will now stay high for 27 or 70 uS, depending on
                                                                   whether a 0 or a 1 is being sent, respectively.*/
      delayMicroseconds(45);                                     /*AFTER pin is high, wait further period, to be
                                                                   into the part of the timing diagram where a 0 or a 1 denotes
                                                                   the datum being send. The "further period" was 30uS in the software
                                                                   that this has been created from. I believe that a higher number
                                                                   (45?) might be more appropriate.*/

                                                                  //Next: Wait while pin still high
      if (digitalRead(dht_dpin)==HIGH)
       result |=(1<<(7-i));                                         // "add" (not just addition) the 1 to the growing byte.
    
    
      while (digitalRead(dht_dpin)==HIGH);                          /*Next wait until pin goes low again, which signals the START
                                                                    of the NEXT bit's transmission.*/
   }                                                               //end for
  return result;
}

Hi BillieBricks,

I'm not near any hardware to test this but maybe while (checkButton(buttonTwo)){} should have been while (checkButton(buttonTwo) = false){} Button is just used internally to the checkButton function, it should = the value passed in the brackets after checkButton() Sorry I can see no reason you have to create another variable called sensorValue2 to get it to work. As to the mapping problem the only thing I can think is due to having no button debounce in the calibrate (while (digitalRead(buttonTwo) == LOW) {}) that when you press it the first time to do a low calibration the reading is taken and the next button test for high calibration is still reading the same button press so sensorMax & sensorMin end up being the same value. A quick and dirty fix for this might be to put a delay(1000) after the while (digitalRead(buttonTwo) == LOW) {} line to allow time for person to release button and then after delay another while (digitalRead(buttonTwo) == HIGH) {} to stop calibration continuing until button is released.

Something else I thought of was you need to add and extra step in the calibrate() routine to allow operator to place pH probe back in it's normal medium as after calibrate() you immediately read pH level and the probe will still be in the 7.0pH solution.

  Serial.println("Place probe back in operating position, wait for reading to stabilize then press button two");
  while (digitalRead(buttonTwo) == LOW){}

Riva: Hi BillieBricks,

I'm not near any hardware to test this but maybe while (checkButton(buttonTwo)){} should have been while (checkButton(buttonTwo) = false){}

Riva,

If i change it i get the following error while compiling --> err: lvalue required as left operand of assignment So i looked it up and i think it should be while (checkButton(buttonTwo) == false){} --> then i don't get an error during compiling. But the mapping still doesn't work, because i get the same pH values after the mapping as i did before the mapping. The debounce on the other hand, works. Yes!! :D

should have been while (checkButton(buttonTwo) = false){}

No, that = should've been ==, which is why you get the assignment error message

But the mapping still doesn't work, because i get the same pH values after the mapping as i did before the mapping.

The constrain() function is remmed out (//sensorValue =constrain(sensorValue, 0, 1023);) and should probably be sensorValue = constrain(sensorValue, sensorMin, sensorMax); because you can will get unexpected results from map() if sensorValue falls outside of sensorMin/sensorMax range. It should also be placed before the map() line. I did say in another message the calibrate routine should add to the end a button press delay to allow operator to place pH probe back in it normal medium. Currently after calibrate() you read the pH level but the probe would still be in the 7.0pH solution and not reflect the condition your trying to test for.

Hello,

I've tried all sorts of different things. Finally got the mapping to work, but it still doesn't work as expected. The pH value of the last callibration solution is acurate, but the further away i go from that value the more screwed up the results get... For now i'm working with a trim potmeter (100KOhm) and this gives more then acceptable results. As i'm currently buisy with getting the 3.2" tft working so the digital calibration part has been set asside... It's still on the todo list though.