// the setup function runs once when you press reset or power the board
int CO2Klep =4;
int LED = 7;
int PhElec = 0; // potentiometer wiper (middle terminal) connected to analog pin 3
int Phwaarde = 0;
int Phscherm = 0;
int Phinstpod = 1;
int Phinst = 0;
int Phinstscherm = 0;
#define SensorPin A2 //pH meter Analog output to Arduino Analog Input 2
#define voltOffset -2.50 //offset to calculate voltage
#define phOffset 7.00 //offset to calculate ph
#define Gain 4.0 //gain compensate: (7-6.68)=0.32, gain = (7-4)/0.32=9,38
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// initialize digital pin 13 as an output.
pinMode(CO2Klep, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("GemetPh");
lcd.setCursor(0, 1);
lcd.print("inPh");
}
// the loop function runs over and over again forever
void loop() {
Phscherm = map(Phwaarde, 0, 1023, 0, 14);
Phwaarde = analogRead(PhElec); // read the input pin
Serial.println(Phscherm); // print as an ASCII-encoded decimal
Phinstscherm = map (Phinst, 0, 1023, 0,14);
Phinst = analogRead (Phinstpod);
lcd.setCursor(9, 1);
lcd.print(Phinstscherm);
Serial.println(Phinst);
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float phReading,voltage;
if(millis()-samplingTime > samplingInterval)
{
int phRead=1024-analogRead(SensorPin); //changed to 1024-
voltage = voltOffset + phRead*5.0/1024; //AnalogRead 0-1024
phReading = Gain*voltage+phOffset;
samplingTime=millis();
}
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("Voltage:");
Serial.print(voltage,2);
Serial.print(" pH value: ");
Serial.println(phReading,2);
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
lcd.setCursor(9, 0);
lcd.print(phReading);
lcd.print(" ");
if(phReading < 100){
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
}
if (phReading > 105){
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
// wait for a second
}
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
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;
}
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<min){
amount+=min; //arr<min
min=arr;
}else {
if(arr>max){
amount+=max; //arr>max
max=arr;
}else{
amount+=arr; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
/*
if(pHValue < 100){
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
}
if (pHValue > 105){
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
// wait for a second
}*/