Negative values

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
tjansteman
Posts: 9
Joined: 26 Jan 2021 00:20

Negative values

Post by tjansteman »

I'm building an irrigation system with a water flow meter. My problem is that I keep getting negative values for the l/s. ill post the code and I know it isn't pretty but I have no coding experience so it is trial and error.

I´ve tried with low numbers for the waterflow and it works, but when they get higher it turns into a negative number. 

Code: Select all

[code]

#define LedPin1 13
#define LedPin2 14
#define LedPin3 15
#define LedPin4 16
#define LedPin5 11
#define LedPin6 12
#define LedPin7 5
#define LedPin8 6

#include <math.h>



ZUNO_SETUP_ISR_INT1(int1_handler); // Davis Regensensor
ZUNO_SETUP_ISR_INT0(int0_handler);
ZUNO_SETUP_ISR_GPTIMER(gpt_handler);

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_ALWAYS_AWAKE);

unsigned int flow_frequency = 0;
unsigned int flow_frequency_naring = 0;

byte switchValue1 = 1;
byte switchValue2 = 1;
byte switchValue3 = 1;
byte switchValue4 = 1;
byte switchValue5 = 1;
byte switchValue6 = 1;
byte switchValue7 = 1;
byte switchValue8 = 1;

long water_rate = 0;

long water_factor_naring = 0;
long water_naring = 0;


unsigned int sendstopper = 0;

byte data_updated = FALSE;
byte GPT_1sec = 0;
byte count_1sec = 0;
unsigned int water_factor = 350;
unsigned int naring_factor = 450;
unsigned int resetterwatertotal;

ZUNO_SETUP_CHANNELS(// set up channels
  ZUNO_SENSOR_MULTILEVEL(0x38 /* Water flow */, 0x00 /* l/h */, 2 /* two bytes size */, 2 /* one decimal */, getterWaterRate),
  ZUNO_SENSOR_MULTILEVEL(0x38 /* Water flow */, 0x00 /* l/h */, 2 /* two bytes size */, 2 /* one decimal */, getterNaringRate),
  ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_GENERAL_PURPOSE_VALUE, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterPercent),
  ZUNO_SWITCH_BINARY(getterSwitch1, setterSwitch1),
  ZUNO_SWITCH_BINARY(getterSwitch2, setterSwitch2),
  ZUNO_SWITCH_BINARY(getterSwitch3, setterSwitch3),
  ZUNO_SWITCH_BINARY(getterSwitch4, setterSwitch4),
  ZUNO_SWITCH_BINARY(getterSwitch5, setterSwitch5),
  ZUNO_SWITCH_BINARY(getterSwitch6, setterSwitch6),
  ZUNO_SWITCH_BINARY(getterSwitch7, setterSwitch7),
  ZUNO_SWITCH_BINARY(getterSwitch8, setterSwitch8)
);

void setup() {
  zunoExtIntMode(ZUNO_EXT_INT1, RISING);
  zunoExtIntMode(ZUNO_EXT_INT0, RISING);
  zunoGPTInit(ZUNO_GPT_SCALE1024 | ZUNO_GPT_CYCLIC);
  zunoGPTSet(31250); // 1 Second Timer
  zunoGPTEnable(1);
  Serial.begin();
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  pinMode(LedPin3, OUTPUT);
  pinMode(LedPin4, OUTPUT);
  pinMode(LedPin5, OUTPUT);
  pinMode(LedPin6, OUTPUT);
  pinMode(LedPin7, OUTPUT);
  pinMode(LedPin8, OUTPUT);
}



void int1_handler() // PIN18-water sensor interupt
{
  flow_frequency++;
}

void int0_handler() // PIN17-water sensor interupt
{
  flow_frequency_naring++;
}

void gpt_handler() // 1" Timer
{
  GPT_1sec++;
}

void loop() {
       
  if (count_1sec != GPT_1sec) {
    count_1sec = GPT_1sec;
    if (flow_frequency > 0) {
      water_rate = flow_frequency;
      //water_rate = 23;
      water_factor_naring = flow_frequency_naring;
      //water_factor_naring = 24;
      water_naring = water_factor_naring / water_rate * 100;
      flow_frequency_naring = 0;
      flow_frequency = 0;
      GPT_1sec = 0;
      sendstopper = 0;
      /*  
         Serial.print("Water rate  ");
         Serial.print(water_rate);
         Serial.print("\n");
         Serial.print("näring rate  ");
         Serial.print(water_factor_naring);
         Serial.print("\n");
         Serial.print("rate  ");
         Serial.print(flow_frequency_naring);
         Serial.print(flow_frequency);
         Serial.print("\n");
         Serial.print("procent  ");
 */
      delay(100);
      zunoSendReport(1);
      zunoSendReport(2);
      zunoSendReport(3);
    }
    if (sendstopper == 0) {
      if (flow_frequency == 0) {
        water_rate = flow_frequency;
        water_factor_naring = 0;
        water_naring = 0;
        //  my_sensor.water_totalsend = water_total / 35000;
        //        Serial.print(my_sensor.water_totalsend);
        //        Serial.print("water total");
        //   water_total = 0;
        delay(100);
        sendstopper = 1;
        zunoSendReport(1);
        zunoSendReport(2);
        zunoSendReport(3);
      }
    }

  }
}



void setterSwitch1(byte value) {
  digitalWrite(LedPin1, (value > 0) ? HIGH : LOW);
  switchValue1 = value;

}

byte getterSwitch1() {
  return switchValue1;

}

void setterSwitch2(byte value) {
  digitalWrite(LedPin2, (value > 0) ? HIGH : LOW);
  switchValue2 = value;
}

byte getterSwitch2() {
  return switchValue2;
}

void setterSwitch3(byte value) {
  digitalWrite(LedPin3, (value > 0) ? HIGH : LOW);
  switchValue3 = value;
}

byte getterSwitch3() {
  return switchValue3;
}

void setterSwitch4(byte value) {
  digitalWrite(LedPin4, (value > 0) ? HIGH : LOW);
  switchValue4 = value;
}

byte getterSwitch4() {
  return switchValue4;
}

void setterSwitch5(byte value) {
  digitalWrite(LedPin5, (value > 0) ? LOW : HIGH);
  switchValue5 = value;
}

byte getterSwitch5() {
  return switchValue5;
}

void setterSwitch6(byte value) {
  digitalWrite(LedPin6, (value > 0) ? LOW : HIGH);
  switchValue6 = value;
}

byte getterSwitch6() {
  return switchValue6;
}

void setterSwitch7(byte value) {
  digitalWrite(LedPin7, (value > 0) ? HIGH : LOW);
  switchValue7 = value;
}

byte getterSwitch7() {
  return switchValue7;
}

void setterSwitch8(byte value) {
  digitalWrite(LedPin8, (value > 0) ? HIGH : LOW);
  switchValue8 = value;
}

byte getterSwitch8() {
  return switchValue8;
}
/* Water flow
  dword getterWatertotal(void) {
  return my_sensor.water_totalsend;
  Serial.print(my_sensor.water_totalsend);
  Serial.print("return");
  }
*/

unsigned long getterPercent(void) {
  return water_naring;

}
unsigned long getterNaringRate(void) {
  return water_factor_naring * 3600 / 350 * 100;
  // return my_sensor.water_factor_naring * 360000 / 350;

}
unsigned long getterWaterRate(void) {
  return water_rate * 3600 / 450 * 100;


}
[/code]
User avatar
PoltoS
Posts: 6350
Joined: 26 Jan 2011 19:36

Re: Negative values

Post by PoltoS »

You defined 2 bytes, but return type of getterWaterRate is long (4 bytes). This is wrong
Post Reply