-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHReader.ino
73 lines (61 loc) · 1.55 KB
/
PHReader.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#Credits to https://how2electronics.com/ph-meter-using-ph-sensor-arduino-oled/
#include "LiquidCrystal.h"
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//rs, enable pin, D4, D5,D6 ,D7 ,R/W ,VSS ,Vcc
#define Offset 0.2
void sortLargeToSmall(int);
int average(int);
//float input_voltage;
unsigned long int avgValue; //Store the average value of the sensor feedback
int buf[10];
void setup()
{
//Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("PHTest");
delay(1000);
}
void loop()
{
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
Serial.begin(9600);
buf[i]=analogRead(0);
delay(10);
}
//int analog_value = analogRead(1);
//input_voltage = (analog_value * 5.0) / 1024.0;
sortLargeToSmall(buf);
avgValue = average(buf);
float phValue=(float)avgValue*5.0/1024; //convert the analog into millivolt
phValue=(3.5*phValue) + Offset; //convert the millivolt into pH value
lcd.setCursor(0, 1);
lcd.print("PH:");
lcd.print(phValue);
delay(800);
}
void sortLargeToSmall(int list[])
{
int temp;
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
}
int average(int list[])
{
int av=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
{
av+=list[i];
}
return (av/6);
}