-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoil.h
65 lines (55 loc) · 1.28 KB
/
soil.h
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
/*
Soil humidity
*/
class soilSensor{
public:
float medianRead;
void loopSensors(){
lcd.clear();
int arraySum = 0;
for (int thisPin = 0; thisPin < sensorCount; thisPin++) {
pinMode(thisPin, INPUT);
int r = convertRawValue(analogRead(thisPin)); // get percetage
displayValues(thisPin,r); // show number
arraySum += r; //sum values
}
medianRead = arraySum / sensorCount; // store average
displayAverage(medianRead);
lcd.backlight();
delay(5000);
lcd.noBacklight();
lcd.clear();
}
int getAverage(){
return (int)medianRead;
}
private:
int sensorCount = 4;
String sPreStr = "S#";
String sInStr = ": ";
String sPostStr = "%";
String aPreStr = "AvH: ";
String aPostStr = "%";
void displayValues(int i, float r){
String buf;
buf += sPreStr;
buf += String(i);
buf += sInStr;
buf += String(r,0);
buf += sPostStr;
lcd.setCursor(0,i);
lcd.print(buf);
}
void displayAverage(float r){
String buf;
buf += aPreStr;
buf += String(r,0);
buf += aPostStr;
lcd.setCursor(10,0);
lcd.print(buf);
}
int convertRawValue(int read) {
return map(read,0,1023,100,0);
}
};
soilSensor sl;