Created
February 12, 2018 02:47
-
-
Save HMKRL/0bfef009dc989c1a71752bda0973bbf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define VOLTAGE 5.00 //system voltage | |
#define OFFSET 12 //orp zero drift voltage | |
#define LED 13 //operating instructions | |
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0 | |
#define orpPin A5 //orp meter output,connect to Arduino controller ADC pin | |
#define Offset 0.00 //deviation compensate | |
#define LED 13 | |
#define samplingInterval 20 | |
#define printInterval 30000 | |
#define ArrayLenth 40 //times of collection | |
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback | |
int pHArrayIndex=0; | |
int orpArray[ArrayLenth]; | |
int orpArrayIndex = 0; | |
double orpValue; | |
unsigned long time_cnt = 0; | |
void setup(void) | |
{ | |
pinMode(LED,OUTPUT); | |
Serial.begin(9600); | |
Serial.println("Time, pH, ORP"); | |
} | |
void loop(void) | |
{ | |
static unsigned long samplingTime = millis(); | |
static unsigned long orpTimer = millis(); //analog sampling interval | |
static unsigned long printTime = millis(); | |
static float pHValue,voltage; | |
if(millis()-samplingTime > samplingInterval) | |
{ | |
pHArray[pHArrayIndex++]=analogRead(SensorPin); | |
if(pHArrayIndex==ArrayLenth) pHArrayIndex=0; | |
voltage = avergearray(pHArray, ArrayLenth)*5.0/1024; | |
pHValue = 3.5*voltage+Offset; | |
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 / 1024)) / 75 - OFFSET; //convert the analog value to orp according the circuit | |
samplingTime=millis(); | |
} | |
if(millis() - printTime > printInterval) | |
{ | |
Serial.print(time_cnt++); | |
Serial.print(", "); | |
Serial.print(pHValue,2); | |
Serial.print(", "); | |
Serial.println(orpValue,2); | |
digitalWrite(LED,digitalRead(LED)^1); | |
printTime=millis(); | |
} | |
} | |
double avergearray(int* arr, int number){ | |
int i; | |
int max,min; | |
double avg=0; | |
long amount=0; | |
if(number<=0){ | |
Serial.println("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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment