Dear hard-shovel,
I do not know if you are still around and if you can help me a bit.
I actually put into the soil a watermark at 20cm. I add some water to see the watermark measurement and I used your proposition (code)
I can not understand why I have a WRm of 518 and it retrun me a 200kPa It should be 0
Getting WM1 (SMX):
Frequency at 15: 6896Hz WRM: 518
WRM: 518
WRMc: 518
SWP: 200
Here si the code:
Note: sw.something. The sw is beacuse I use Seesaw Seesaw borad from Adafruit to have more DIO
// getting the swp.
// wm1_power_pin is pin which ppower smx
// wm1_power_pin is the pin read the value
// val is the return value
get_wmsmx(wm1_power_pin, wm1_read_pin, 1000000, val, true);
mesMesures[c_wm1][capteur_wm1_id].valeur = val; // The final SWP is store here
Dont pain attention at 'alternative'. It's alway true and I will clean the code.
void get_wmsmx(int powerPin, int pinRead, unsigned long timeout, int16_t &val, bool alternative)
{
sw.digitalWrite(powerPin, HIGH);
delay(1000); // Need to have the sensor "in equilibre" with the soil.
int highInput, lowInput; // Store high and low time of wave in microseconds
float totalInput; // Temp store of total time of duration for one cycle of high and low pulse
float frequency; // calculated freqency 1/total time of one cycle.
int16_t swp;
int percent=0;
int32_t wrm;
//float Tsoil=24; //temp
highInput = pulseIn(pinRead,HIGH);
lowInput = pulseIn(pinRead,LOW);
totalInput = highInput + lowInput;
frequency = 1000000 / totalInput;
if (highInput >0 && lowInput>0)
{
Si.sprint(F("Frequency at "),2); Si.sprint(pinRead,2); Si.sprint(F(": "),2); Si.sprint((int16_t)frequency,2); Si.sprint(F("Hz "),2);
}
else
{
Si.sprint(F("frequency: "),2);
Si.sprintln(F("Error"),2);
}
delay(500);
sw.digitalWrite(powerPin, LOW);
int16_t Tsoil = mesMesures[c_temperature_soil][capteur_temperature_soil_id].valeur;
if(alternative == false)
{
// That is my old code. I will remove it
wrm = map(frequency,48,13233,27950,550); // Must control the frequency for 440 and 27950
Si.sprint(F("WRM: "),2); Si.sprint(wrm,2); Si.sprintln(F(" Ohm"),2);
Si.sprintln(F("Calculate SWP"),2);
//kPa = (3.213 * kohms + 4.093) / (1 - 0.009733 * kohms - 0.01205 * Celsius)
swp = (int16_t)(3.213 * (wrm/1000) + 4.093) / (1 - 0.009733 * (wrm/1000) - 0.01205 * Tsoil);
Si.sprint(F("SWP: "),2); Si.sprint(swp,2); Si.sprint(F("kPa"),2);
Si.sprint(F(" for a soil temp of "),2); Si.sprintln(Tsoil,2);
//val = (int16_t)percent;
val = swp;
}
else
{
resistanceCalc(frequency, wrm);
Si.sprint(F("WRM: "),2);
Si.sprintln(wrm,2);
kPaCalc(wrm, Tsoil, swp);
val = swp;
}
Si.sprint(F("SWP: "),2); Si.sprintln(swp,2);
}
//---------------------------------------------------------------
void resistanceCalc(float frequencyInput, int32_t &wrm){
wrm=0;
// Convert from freqency to Resistance measurement
// From SMX.pdf datasheet, page 6
// 48 Hz = 10,000,000 Ohms
// 76 Hz = 262,144 Ohms
// 13233 Hz = 0 ohms
// using lookup table held in the array RESISTORarray
//frequencyInput = constrain(frequencyInput,50, 13233);
//float newVal;
if (frequencyInput <= RESISTORarray[0]) { // Minimum value
wrm = RESISTORarray[0+1];
}
if (frequencyInput >= RESISTORarray[74]) { // Maximum value
wrm = RESISTORarray[74+1];
}
for (int i=0; i<74; i=i+2) {
if ((frequencyInput >= RESISTORarray[i]) && (frequencyInput <= RESISTORarray[i+2]))
{
wrm = RESISTORarray[i+1] - ((RESISTORarray[i+1]-RESISTORarray[i+3]) * ((frequencyInput-RESISTORarray[i]) / (RESISTORarray[i+2]-RESISTORarray[i])));
break;
}
}
Si.sprint(F("WRM: "),2); Si.sprintln(wrm,2);
}
void kPaCalc(int32_t ResistanceInput, int16_t FTemperatureInput, int16_t &swp){
// Convert from Resistance to SWP kPa measurement
// From SMX.pdf datasheet, page 7
// 550 Ohms = 0 SWP kPa
// 6000 Ohms = 35 SWP kPa
// 28075 Ohms =200 SWP kPa
// using lookup table held in the array SWPkPAarray
// table valid for temperature of 75F, 24C
// for increase of 1Β°F increase resistance by 1%.
// ** this function accepts temperature in Fahrenheit units **
//float newVal;
// Adjust compensate resistance for temperature and cpnvert celculs to Fahrenheit
// per page 8 of SMX.pdf
swp=0;
float ResistanceCompensated = ResistanceInput *(1 + 0.001*((FTemperatureInput * 1.8 + 32)-75));
Si.sprint(F("WRMc: "),2); Si.sprintln((int16_t)ResistanceCompensated,2);
if (ResistanceCompensated <= SWPkPAarray[0]) { // Minimum value
swp = SWPkPAarray[0+1];
}
if (ResistanceCompensated >= SWPkPAarray[74]) { // Maximum value
swp = SWPkPAarray[16+1];
}
//for (int i=0; i<SWPkPAarray.length-2; i=i+2) {
for (int i=0; i<16; i=i+2) {
if ((ResistanceCompensated >= SWPkPAarray[i]) && (ResistanceCompensated <= SWPkPAarray[i+2])) {
swp = SWPkPAarray[i+1] - ((SWPkPAarray[i+1]-SWPkPAarray[i+3]) * ((ResistanceInput-SWPkPAarray[i]) / (SWPkPAarray[i+2]-SWPkPAarray[i])));
break;
}
}
//return newVal;
}
long RESISTORarray[76] = {
// Watermark Sensor SMX interface Hz to Resistance lookup table per SMX.pdf page 6.
48, 10000000,
76, 262144,
85, 196608,
103, 131072,
122, 98304,
157, 65536,
194, 49152,
264, 32768,
335, 24567,
476, 16384,
612, 12288,
874, 8192,
1135, 6144,
1623, 4096,
2071, 3072,
2862, 2048,
3557, 1536,
4697, 1024,
5596, 768,
6932, 512,
7878, 384,
9104, 256,
9882, 192,
10802, 128,
11312, 96,
11893, 64,
12200, 48,
12526, 32,
12708, 24,
12871, 16,
12962, 12,
13047, 8,
13092, 6,
13139, 4,
13162, 3,
13186, 2,
13209, 1,
13233, 0,
};
long SWPkPAarray[18]{
// Watermark Sensor SMX interface Resistance to SWP kPa lookup table per SMX.pdf page 7.
// this table is valid at temperature of 75F, 24C
550, 0,
1000, 9,
1100, 10,
2000, 15,
6000, 35,
9200, 55,
12200, 75,
15575, 100,
28075, 200,
};
Many thank!!