long float_to_long(float in) {
return in * 1000L;
}
float vpd_gap_function(long new_, long old_, long UL, long LL, long GAP_ALLOWED, long STEP_SIZE, uint8_t SMOOTH_NUM, uint8_t i) {
static uint8_t smooth_count[18] = {0};
long temp = constrain(new_, LL, UL);
long signed_gap = temp - old_;
long abs_gap = abs(signed_gap);
if (abs_gap > GAP_ALLOWED) {
if (smooth_count[i] >= SMOOTH_NUM) {
smooth_count[i] = 0;
float temp_float = float(new_) / 1000.0;
return temp_float;
}
if (signed_gap < 0) {
smooth_count[i]++;
temp = old_ - STEP_SIZE;
float temp_float = float(temp) / 1000.0;
return temp_float;
}
if (signed_gap > 0) {
smooth_count[i]++;
temp = old_ + STEP_SIZE;
float temp_float = float(temp) / 1000.0;
return temp_float;
}
}
else {
smooth_count[i] = 0;
float temp_float = float(new_) / 1000.0;
return temp_float;
}
return 0;
}
void vpd_message(char *strings[], uint8_t tokens) {
if (tokens > 18)
{
return;
}
long float_to_long_arr[18] = {0};
long current_vpd_long_arr[18] = {0};
//convert everything to long
for (int i = 0; i < 18; i++) {
char *buf[20] = {NULL};
*buf = strings[i + 1];
float temp = atof(*buf);
float_to_long_arr[i] = float_to_long(temp);
//Serial.println(float_to_long_arr[i]);
}
const long vpd_UL = 2500;
const long vpd_LL = 500;
const long vpd_STEP_SIZE = 5;
const long vpd_SMOOTH_NUM = 15;
const long vpd_GAP_ALLOWED = 20;
const long temp_UL = 30000;
const long temp_LL = 15000;
const long temp_STEP_SIZE = 5;
const long temp_SMOOTH_NUM = 15;
const long temp_GAP_ALLOWED = 20;
current_vpd_long_arr[8] = float_to_long(AreaOne.vapor_pressure_deficit);
current_vpd_long_arr[13] = float_to_long(AreaTwo.vapor_pressure_deficit);
for (int i = 0; i < 4; i++) {
current_vpd_long_arr[i] = float_to_long(AreaOne.leaf_temperature[i]);
current_vpd_long_arr[i + 4] = float_to_long(AreaTwo.leaf_temperature[i]);
current_vpd_long_arr[i + 9] = float_to_long(AreaOne.leaf_vapor_pressure_deficit[i]);
current_vpd_long_arr[i + 14] = float_to_long(AreaTwo.leaf_vapor_pressure_deficit[i]);
}
AreaOne.vapor_pressure_deficit = vpd_gap_function(float_to_long_arr[8], current_vpd_long_arr[8], vpd_UL, vpd_LL, vpd_GAP_ALLOWED, vpd_STEP_SIZE, vpd_SMOOTH_NUM, 8);
AreaTwo.vapor_pressure_deficit = vpd_gap_function(float_to_long_arr[13], current_vpd_long_arr[13], vpd_UL, vpd_LL, vpd_GAP_ALLOWED, vpd_STEP_SIZE, vpd_SMOOTH_NUM, 13);
for (int i = 0; i < 4; i++) {
AreaOne.leaf_temperature[i] = vpd_gap_function(float_to_long_arr[i], current_vpd_long_arr[i], temp_UL, temp_LL, temp_GAP_ALLOWED, temp_STEP_SIZE, temp_SMOOTH_NUM, i);
AreaTwo.leaf_temperature[i + 4] = vpd_gap_function(float_to_long_arr[i + 4], current_vpd_long_arr[i + 4], temp_UL, temp_LL, temp_GAP_ALLOWED, temp_STEP_SIZE, temp_SMOOTH_NUM, (i + 4));
AreaOne.leaf_vapor_pressure_deficit[i + 9] = vpd_gap_function(float_to_long_arr[i + 9], current_vpd_long_arr[i + 9], vpd_UL, vpd_LL, vpd_GAP_ALLOWED, vpd_STEP_SIZE, vpd_SMOOTH_NUM, (i + 9));
AreaTwo.leaf_vapor_pressure_deficit[i + 14] = vpd_gap_function(float_to_long_arr[i + 14], current_vpd_long_arr[i + 14], vpd_UL, vpd_LL, vpd_GAP_ALLOWED, vpd_STEP_SIZE, vpd_SMOOTH_NUM, (i + 14));
}
}