Valeur non prise en compte dans librairie

Bonjour,

Je dispose d'une sonde pour mesurer le pH.
Pour mes besoins, j'ai retravaillé le fichier cpp pour étalonner différemment mon capteur. Actuellement, j'ai un souci d'étalonnage (quand je lance la procédure, celle-ci échoue et sont donc pris en compte les paramètres initiaux)
En attendant de chercher plus loin d'où vient le problème, je voulais "tricher" en modifiant les paramètres _acidVoltage et _neutralVoltage du fichier cpp pour que l'affichage du pH soit correct.
Mon problème est que, même après avoir modifié dans le fichier ces valeurs (respectivement 2011.5 et 1575), elles n'ont pas l'air d'être prises en compte car lorsque je demande à les faire afficher, les valeurs initiales sont affichées (2032.444 et 1500).
Avez-vous une idée d'où peut provenir cette erreur d'affichage ?

/*
 * file DFRobot_PH.cpp * @ https://github.com/DFRobot/DFRobot_PH
 *
 * Arduino library for Gravity: Analog pH Sensor / Meter Kit V2, SKU: SEN0161-V2
 *
 * Copyright   [DFRobot](http://www.dfrobot.com), 2018
 * Copyright   GNU Lesser General Public License
 *
 * version  V1.0
 * date  2018-04
 */


#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include "DFRobot_PH.h"
#include <EEPROM.h>



#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p)  {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}

#define PHVALUEADDR 0x00    //the start address of the pH calibration parameters stored in the EEPROM

DFRobot_PH::DFRobot_PH()
{
    this->_temperature    = 25.0;
    this->_phValue        = 7.0;
    this->_acidVoltage    = 2011.5;    //buffer solution 4.0 at 25C
    this->_neutralVoltage = 1575.0;     //buffer solution 7.0 at 25C
    this->_voltage        = 1500.0;
}

DFRobot_PH::~DFRobot_PH()
{

}

void DFRobot_PH::begin()
{
    EEPROM_read(PHVALUEADDR, this->_neutralVoltage);  //load the neutral (pH = 7.0)voltage of the pH board from the EEPROM
	Serial.print("_neutralVoltage:");
    Serial.println(this->_neutralVoltage);
    if(EEPROM.read(PHVALUEADDR)==0xFF && EEPROM.read(PHVALUEADDR+1)==0xFF && EEPROM.read(PHVALUEADDR+2)==0xFF && EEPROM.read(PHVALUEADDR+3)==0xFF){
        this->_neutralVoltage = 1575.0;  // new EEPROM, write typical voltage
        EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
    }
    EEPROM_read(PHVALUEADDR+4, this->_acidVoltage);//load the acid (pH = 4.0) voltage of the pH board from the EEPROM
	Serial.print("_acidVoltage:");
    Serial.println(this->_acidVoltage);
    if(EEPROM.read(PHVALUEADDR+4)==0xFF && EEPROM.read(PHVALUEADDR+5)==0xFF && EEPROM.read(PHVALUEADDR+6)==0xFF && EEPROM.read(PHVALUEADDR+7)==0xFF){
        this->_acidVoltage = 2011.5;  // new EEPROM, write typical voltage
        EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
    }

}

float DFRobot_PH::readPH(float voltage, float temperature)
{
    float slope = (7.0-4.0)/((this->_neutralVoltage-1500.0)/3.0 - (this->_acidVoltage-1500.0)/3.0);  // two point: (_neutralVoltage,7.0),(_acidVoltage,4.0)
    float intercept =  7.0 - slope*(this->_neutralVoltage-1500.0)/3.0;
    Serial.print("pente:");
    Serial.print(slope);
    Serial.print(",intercept:");
    Serial.println(intercept);
    this->_phValue = slope*(voltage-1500.0)/3.0+intercept;  //y = k*x + b
    return _phValue;
}


void DFRobot_PH::calibration(float voltage, float temperature,char* cmd)
{
    this->_voltage = voltage;
    this->_temperature = temperature;
    strupr(cmd);
    phCalibration(cmdParse(cmd));  // if received Serial CMD from the serial monitor, enter into the calibration mode
}

void DFRobot_PH::calibration(float voltage, float temperature)
{
    this->_voltage = voltage;
    this->_temperature = temperature;
    if(cmdSerialDataAvailable() > 0){
        phCalibration(cmdParse());  // if received Serial CMD from the serial monitor, enter into the calibration mode
    }
}

boolean DFRobot_PH::cmdSerialDataAvailable()
{
    char cmdReceivedChar;
    static unsigned long cmdReceivedTimeOut = millis();
    while(Serial.available()>0){
        if(millis() - cmdReceivedTimeOut > 500U){
            this->_cmdReceivedBufferIndex = 0;
            memset(this->_cmdReceivedBuffer,0,(ReceivedBufferLength));
        }
        cmdReceivedTimeOut = millis();
        cmdReceivedChar = Serial.read();
        if (cmdReceivedChar == '\n' || this->_cmdReceivedBufferIndex==ReceivedBufferLength-1){
            this->_cmdReceivedBufferIndex = 0;
            strupr(this->_cmdReceivedBuffer);
            return true;
        }else{
            this->_cmdReceivedBuffer[this->_cmdReceivedBufferIndex] = cmdReceivedChar;
            this->_cmdReceivedBufferIndex++;
        }
    }
    return false;
}

byte DFRobot_PH::cmdParse(const char* cmd)
{
    byte modeIndex = 0;
    if(strstr(cmd, "ENTERPH")      != NULL){
        modeIndex = 1;
    }else if(strstr(cmd, "EXITPH") != NULL){
        modeIndex = 3;
    }else if(strstr(cmd, "CALPH")  != NULL){
        modeIndex = 2;
    }
    return modeIndex;
}

byte DFRobot_PH::cmdParse()
{
    byte modeIndex = 0;
    if(strstr(this->_cmdReceivedBuffer, "ENTERPH")      != NULL){
        modeIndex = 1;
    }else if(strstr(this->_cmdReceivedBuffer, "EXITPH") != NULL){
        modeIndex = 3;
    }else if(strstr(this->_cmdReceivedBuffer, "CALPH")  != NULL){
        modeIndex = 2;
    }
    return modeIndex;
}

void DFRobot_PH::phCalibration(byte mode)
{
    char *receivedBufferPtr;
    static boolean phCalibrationFinish  = 0;
    static boolean enterCalibrationFlag = 0;
    switch(mode){
        case 0:
        if(enterCalibrationFlag){
            Serial.println(F(">>>Command Error<<<"));
        }
        break;

        case 1:
        enterCalibrationFlag = 1;
        phCalibrationFinish  = 0;
		Serial.println();
        Serial.println(F(">>>Enter PH Calibration Mode<<<"));
        Serial.println(F(">>>Please put the probe into the 4.0 or 7.0 standard buffer solution<<<"));
        Serial.println();
		break;

        case 2:
		Serial.println(_voltage);
        if(enterCalibrationFlag){
            if((this->_voltage>1322)&&(this->_voltage<1678)){        // buffer solution:7.0{
				Serial.println();
                Serial.print(F(">>>Buffer Solution:7.0"));
                this->_neutralVoltage =  this->_voltage;
				Serial.println(F(",Send EXITPH to Save and Exit<<<"));
                Serial.println();
                phCalibrationFinish = 1;
            }else if((this->_voltage>1854)&&(this->_voltage<2210)){  //buffer solution:4.0
				Serial.println();
                Serial.print(F(">>>Buffer Solution:4.0"));

                this->_acidVoltage =  this->_voltage;
				Serial.println(F(",Send EXITPH to Save and Exit<<<")); 
                Serial.println();

                phCalibrationFinish = 1;
            }else{
                                   // not buffer solution or faulty operation
                Serial.println();
                Serial.print(F(">>>Buffer Solution Error Try Again<<<"));
                Serial.println();                                    
				phCalibrationFinish = 0;
            }
        }
        break;

        case 3:
        if(enterCalibrationFlag){
            Serial.println();
            if(phCalibrationFinish){
                if((this->_voltage>1322)&&(this->_voltage<1678)){
                    EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
                }else if((this->_voltage>1854)&&(this->_voltage<2210)){
                    EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
                }
                Serial.print(F(">>>Calibration Successful"));
            }else{
                Serial.print(F(">>>Calibration Failed"));
            }
            Serial.println(F(",Exit PH Calibration Mode<<<"));
            Serial.println();
            phCalibrationFinish  = 0;
            enterCalibrationFlag = 0;
        }
        break;
    }
}


Merci pour l'aide

Tu parles de cette partie de code?

Les valeurs sont lues en EEPROM.
Elles ne sont re-écrites que s'il y a 0xFF dans tous les emplacements mémoire de l'EEPROM.

Le plus simple serait peut-être d'écrire un petit bout de code pour remettre à 0xFF les adresses EEPROM allant de PHVALUEADDR à PHVALUEADDR + 7.

Le télecharger et l'exécuter une fois puis recharger ton application. Cette fos comme l'EEPROM sera vierge il écrira tes valeurs dans l'EEPROM.

Bonjour,

Merci pour ta réponse.
Je n'ai jamais programmé de telles choses (j'ai pris le code de base livré avec la sonde, que j'ai retravaillé sur les parties que je comprenais pour l'adapter à mes besoins).
Le bout de code dont tu parles, il doit se placer où ?

Je pense que @fdufnews te propose d'écrire un autre code qui stocke la valeur FF dans le premières cases de ton EEPROM
Un truc du genre :

#include <EEPROM.h>

void setup(){
EEPROM.write( 0, 0xFF );
EEPROM.write( 1, 0xFF );
EEPROM.write( 2, 0xFF );
EEPROM.write( 3, 0xFF );
EEPROM.write( 4, 0xFF );
EEPROM.write( 5, 0xFF );
EEPROM.write( 6, 0xFF );
}

void loop(){

}

On ne peut faire plus simple...

Ah d’accord : le code n’est pas à mettre dans le fichier .cpp mais dans le programme lui-même ?
Et donc à chaque fois que je lancerai le programme, les adresses seront réinitialisées ?

Non, c'est juste pour effacer ces cases de l'EEPROM, tu ne le feras qu'une seule fois et lorsque tu relanceras ton autre code, il fonctionnera comme prévu et prendra en compte les valeurs que tu veux.

Il faut faire un #include "DFRobot_PH.h" pour pouvoir utiliser PHVALUEADDR à PHVALUEADDR+7 à la place des adresses en dur dans ce petit morceau de code.

Si je comprends, je téléverse le programme suivant :

#include <EEPROM.h>
#include "DFRobot_PH.h"

void setup(){
EEPROM.write( PHVALUEADDR, 0xFF );
EEPROM.write( PHVALUEADDR+1, 0xFF );
EEPROM.write( PHVALUEADDR+2, 0xFF );
EEPROM.write( PHVALUEADDR+3, 0xFF );
EEPROM.write( PHVALUEADDR+4, 0xFF );
EEPROM.write( PHVALUEADDR+5, 0xFF );
EEPROM.write( PHVALUEADDR+6, 0xFF );
EEPROM.write( PHVALUEADDR+7, 0xFF );
}

void loop(){

}

Une fois téléversé, je téléverse ensuite mon programme lié au capteur et normalement les valeurs initiales que je modifie seront (normalement) prises en compte ?

Exactement

Bonjour,

je viens de tester mais j'ai une erreur à la compilation du programme de mon post précédent :

'PHVALUEADDR' was not declared in this scope

Est-ce parce qu'il faut déclarer les constantes de type PHVALUEADDR ? Je pensais que l'appel #include "DFRobot_PH.h" suffisait

Qu'est ce qu'il a dans le fichier DRRobot_PH.h ?
Que dit la documentation de la bibliothèque ?
Que disent les exemples d'utilisation qui sont joints à la bibliothèque ?
Ces exemples sont fonctionnels il faut les réaliser en premier pous s'assurer que l'on a bien compris comment il fallait utiliser le matériel.

/*
 * file DFRobot_PH.h * @ https://github.com/DFRobot/DFRobot_PH
 *
 * Arduino library for Gravity: Analog pH Sensor / Meter Kit V2, SKU: SEN0161-V2
 *
 * Copyright   [DFRobot](http://www.dfrobot.com), 2018
 * Copyright   GNU Lesser General Public License
 *
 * version  V1.0
 * date  2018-04
 */

#ifndef _DFROBOT_PH_H_
#define _DFROBOT_PH_H_

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define ReceivedBufferLength 10  //length of the Serial CMD buffer

class DFRobot_PH
{
public:
    DFRobot_PH();
    ~DFRobot_PH();
    void    calibration(float voltage, float temperature,char* cmd);  //calibration by Serial CMD
    void    calibration(float voltage, float temperature);
    float   readPH(float voltage, float temperature); // voltage to pH value, with temperature compensation
    void    begin();   //initialization

private:
    float  _phValue;
    float  _acidVoltage;
    float  _neutralVoltage;
    float  _voltage;
    float  _temperature;

    char   _cmdReceivedBuffer[ReceivedBufferLength];  //store the Serial CMD
    byte   _cmdReceivedBufferIndex;

private:
    boolean cmdSerialDataAvailable();
    void    phCalibration(byte mode); // calibration process, wirte key parameters to EEPROM
    byte    cmdParse(const char* cmd);
    byte    cmdParse();
};

#endif

Dans l'exemple de la bibliothèque, il n'apparaît pas PHVALUEADDR mais uniquement dans le fichier cpp (donné dans le premier post)

Il est défini dans ton code tout en haut (vu que ça commence par PH, c'est forcément lié à ton application, pas à la bibliothèque). Tu peux ajouter cette ligne dans le nouveau code ou bien (puisqu'il vaut 0) ôter toutes les occurrences.

Je n'ai pas touché à cette partie, elle figurait dans le fichier de base.
Avec l'ajout de la ligne, ça fonctionne : super merci beaucoup

Au temps pour moi, j'avais raté la ligne dans le code original.

Quand j'ai posé cette question, c'était pour t'inciter à vérifier le contenu dudit fichier.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.