-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_PLS_KIT3.0
153 lines (126 loc) · 3.93 KB
/
sketch_PLS_KIT3.0
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <SDI12.h>
#define ANALOG_PIN A2
#define RANGE 5000 // Depth measuring range 5000mm (for water)
#define VREF 5000 // ADC's reference voltage on your Arduino,typical value:5000mV
#define CURRENT_INIT 4.00 // Current @ 0mm (uint: mA)
#define DENSITY_WATER 1 // Pure water density normalized to 1
#define PRINT_INTERVAL 1000
int16_t dataVoltage;
float dataCurrent, depth; //unit:mA
unsigned long timepoint_measure;
#define SERIAL_BAUD 115200
#define DATA_PIN 12
SDI12 mySDI12(DATA_PIN);
//SDI 12 commands
#define myComAdress "?!" //ask the adress of the probe
#define myComId "0I!" //ask the identification
#define myComMeasure "0M!" //ask to start a measurement
#define myComSend "0D0!" //ask to send the measure
#define myComSetUnitCM "0OSU+1!" //set the unit
#define myComUnit "0OSU!" //ask for the unit of the measure, +0=m, +1=cm, +2=ft, +3=mbar, +4=psi
String readsensor(){
String sdiResponse = "";
delay(30);
while(mySDI12.available()){
char c = mySDI12.read();
if ((c != '\n') && (c != '\r')) { sdiResponse += c; delay(5); }
}
return sdiResponse;
}
void measREFsensor(){
delay(300);
mySDI12.sendCommand(myComMeasure);
while(mySDI12.available()){ mySDI12.read();}
delay(1000);
mySDI12.clearBuffer();
delay(500);
mySDI12.sendCommand(myComSend);
delay(50);
String rawdata = readsensor();
mySDI12.clearBuffer();
delay(50);
//Decoding string sent from probe
int p=0;
int pos[] = {0,0};
for (int z=0 ; z<rawdata.length() ; z++) {
char u = rawdata.charAt(z);
if (u == '+' || u == '-') {
pos[p] = z ;
p++;
}
delay (50);
}
float RealRefLevel = (rawdata.substring(pos[0],pos[1])).toFloat()-1; //Water level from the reference sensor (without correction)
String Temperature = rawdata.substring(pos[1],rawdata.length()); //Water temperature from the reference sensor
Serial.print("PLS water level:");
Serial.println(RealRefLevel);
delay(50);
Serial.println("Temperature:" +Temperature);
delay(50);
}
void StartRefSensor(){
mySDI12.begin();
delay(2000);
Serial.print(F("'**** Identification of the sensor: "));
mySDI12.sendCommand(myComId);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Adress of the sensor: "));
mySDI12.sendCommand(myComAdress);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Set the unit of the sensor to be cm: "));
mySDI12.sendCommand(myComSetUnitCM);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Unit 0+(0=m, 1=cm, 2=ft, 3=mbar, 4=psi): "));
mySDI12.sendCommand(myComUnit);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
}
void measKitSensor(){
if (millis() - timepoint_measure > PRINT_INTERVAL) {
timepoint_measure = millis();
dataVoltage = analogRead(ANALOG_PIN)/ 1024.0 * VREF;
dataCurrent = dataVoltage / 120.0; //Sense Resistor:120ohm
depth = (dataCurrent - CURRENT_INIT) * (RANGE/ DENSITY_WATER / 16.0)/10; //Calculate depth from current readings
if (depth < 0)
{
depth = 0.0;
}
}
//Serial print results
Serial.print("Kit_depth:");
Serial.println(depth);
delay(50);
}
void StartKitSensor(){
pinMode(ANALOG_PIN, INPUT);
timepoint_measure = millis();
delay(300);
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
StartRefSensor();
StartKitSensor();
}
void loop() {
for (int i = 0; i <= 20; i++){
measREFsensor();
delay(100);
measKitSensor();
delay(100);
}
while(1){}
}