const float VOLTAGE = 5.00; //system voltage
#define OFFSET 0 //zero drift voltage
#define LED 13 //operating instructions
double orpValue;
#define ArrayLenth 40 //times of collection
#define orpPin 1 //orp meter output,connect to Arduino controller ADC pin
int orpArray[ArrayLenth];
int orpArrayIndex=0;
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
printf("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
float U; // tension mesurée à l'entrée analogique
float ORP; // sortie potentiel Redox
int counter; int N = 10; // création compteur et nombre d'itération du compteur
void setup() {
Serial.begin(250000);
pinMode(LED,OUTPUT);
lcd.init(); lcd.backlight();
}
void loop() {
static unsigned long orpTimer=millis(); //analog sampling interval
static unsigned long printTime=millis();
if(millis() >= orpTimer)
{
orpTimer=millis()+20;
orpArray[orpArrayIndex++]=analogRead(orpPin); //read an analog value every 20ms
if (orpArrayIndex==ArrayLenth) {
orpArrayIndex=0;
}
orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/1023))/75-OFFSET;
//convert the analog value to orp according the circuit
}
if(millis() >= printTime) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
printTime=millis()+800;
Serial.print("ORP: ");
Serial.print((int)orpValue);
Serial.println("mV");
digitalWrite(LED,1-digitalRead(LED));
}
U = 0; ORP = 0;
for (counter = 0; counter < N; ++counter) {
U += analogRead(A1)*5.0/1023; // 0 si 0V et 1023 si 5V - lecture de la broche A15
ORP += 1e3*(2-analogRead(A1)*5.0/1023)-OFFSET;
}
lcd.clear();
lcd.setCursor(0, 1); lcd.print("U = ") ; lcd.print(U/N,5); lcd.print(" V");
lcd.setCursor(0, 2); lcd.print("ORP = ") ; lcd.print(int(ORP/N)); lcd.print(" mV");
lcd.setCursor(0, 3); lcd.print(millis()%100);
delay(1e3);
}