Odd output from if statement

So... been tryin to figure this out for a qouple of days really.
WHY does this give me odd numbers when inside an if satement?
If this is running inside "void loop" whitout an if statement it spews out data, correct also, as soon as i put it in an if statement it gives 3669, 3214, etc etc what do i miss.... ?
this works for other functions like "dht.read" and such with if statement, but not this one....
rainfall globally defined as int, (know i dont need an dtostrf), the plan was to use a float really.... but iam kinda stuck. Uno board.

if ( currentTime - previousTime_4 >= eventTime_4_regn) {
char regn[25];
dtostrf(rainfall, -3, 1, regn);
char text[50];
sprintf(text, "Regn %s \n", regn);
esp8266_transmit_udp(text);
previousTime_4 = currentTime;
}

hard to say without seeing more of the code and knowing what the "correct" value is

the following works as expected, output the same value


const unsigned long eventTime_4_regn = 1000;
unsigned long previousTime_4;
unsigned long currentTime;

float rainfall = 12.34;

void
loop (void)
{
    currentTime = millis ();

    if ( currentTime - previousTime_4 >= eventTime_4_regn) {
        char regn[25];
        dtostrf(rainfall, -3, 1, regn);
        char text[50];
        sprintf(text, "Regn %s \n", regn);

        Serial.print (text);

        previousTime_4 = currentTime;
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

Agree with gcjr

This probably makes no difference, but I have seen odd behaviour too.
Try changing
( currentTime - previousTime_4 >= eventTime_4_regn)
to
( (currentTime - previousTime_4) >= eventTime_4_regn)

Its a left-field thought...

yes, tried that now, works fine... odd, i´ll posting all of the code, maybe not so much my own but this is purly educational fun att the moment, regardless it bugs me not knowing WHY!? :stuck_out_tongue: it gives the "correct" value as specified in the code until rainfall++ then it goes bananas, but..... only when above part is used in if statement. other outpus are fine.

Skärmklipp
this is after one "bucket", one reading of the hall sensor, when placed in this if statement, whitout it prints "1".

#include "DHT.h"
#include <Wire.h>
#include <SFE_BMP180.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3
#define DHTPIN 2 
#define DHTTYPE DHT11
#define I2C_ADDRESS 0x77
#define ALTITUDE 90.0


OneWire oneWire(ONE_WIRE_BUS);
DHT dht(DHTPIN, DHTTYPE);
SFE_BMP180 pressure;
DeviceAddress Temp = {0x28, 0xB2, 0x3C, 0x16, 0xA8, 0x0, 0x3C, 0x2D};
DallasTemperature sensors(&oneWire);

const int Rain = 9;
int rainfall;

const unsigned long eventTime_1_hum = 33000; //in ms
const unsigned long eventTime_2_temp = 32000; //in ms
const unsigned long eventTime_3_press = 31000; //in ms
const unsigned long eventTime_4_regn = 30000; //in ms

unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_4 = 0;

unsigned long currentTime = 0;


#define MAX_COMMAND_TIME  10000 // milliseconds

void setup() {
 pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);

  if(esp8266_test_communication() &&
     esp8266_reset() &&
     esp8266_client_mode() &&
     esp8266_join_ap("WST_AP", "") &&
     esp8266_start_udp("255.255.255.255", 5000)) {

    // success, turn on LED
    digitalWrite(LED_BUILTIN, HIGH);
    
  } else {

    // failure, blink LED
    while(true) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
    }
    
  }
    pinMode(Rain, INPUT);
    dht.begin();
    pressure.begin();
    sensors.begin();
    sensors.setResolution(Temp, 12);

}

void loop() {

  currentTime = millis();

if ( currentTime - previousTime_4 >= eventTime_4_regn) {
  
          char text[50];
          sprintf(text, "Regn %d \n", rainfall);
          esp8266_transmit_udp(text);
          
          previousTime_4 = currentTime;
  }

if ( digitalRead(Rain) == LOW){ //hall sensor on "tipping bucket" (clean enough without deboounce)
    rainfall++;
   
  }
 

  if ( currentTime - previousTime_1 >= eventTime_1_hum) {
    char hum[25];
    dtostrf(dht.readHumidity(), -2, 0, hum);
    char text[50];
    sprintf(text, " Hum: %s\n", (hum));
    esp8266_transmit_udp(text);
    
   previousTime_1 = currentTime;
 }

 if ( currentTime - previousTime_2 >= eventTime_2_temp) {
    sensors.requestTemperatures();
    char temp[25];
    dtostrf(sensors.getTempC(Temp), -4, 2, temp);
    char text[50];
    sprintf(text, " Temp: %s\n", (temp));
    esp8266_transmit_udp(text);

   previousTime_2 = currentTime;
 }


  if ( currentTime - previousTime_3 >= eventTime_3_press) {

      char status;
      double T,P,p0,a;
 

  status = pressure.startTemperature();
  if (status != 0)
  {

    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

       status = pressure.startPressure(3);
      if (status != 0)
      {
        
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
        

          p0 = pressure.sealevel(P,ALTITUDE);
          a = pressure.altitude(P,p0);
          
              char pres[25];
              dtostrf((P) + 4, -4, 2, pres);      
              char presu[25];
              dtostrf((p0 + 4), -4, 2, presu);
              
              char text[50];
              sprintf(text, " Press: %s sea: %s\n", (pres), (presu));
              esp8266_transmit_udp(text);
              
            previousTime_3 = currentTime;

        }
        
      }
      
    }
    
  }


  }


    







}




bool esp8266_test_communication(void) {
  delay(500); // wait for module to boot up
  Serial.print("AT\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("OK"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

bool esp8266_reset(void) {
  Serial.print("AT+RST\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("ready"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

bool esp8266_client_mode(void) {
  Serial.print("AT+CWMODE=1\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("OK"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

bool esp8266_join_ap(String ssid, String password) {
  Serial.print("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("WIFI CONNECTED"))
      break;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
  while(true) {
    if(Serial.find("WIFI GOT IP"))
      break;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
  while(true) {
    if(Serial.find("OK"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

bool esp8266_start_udp(String ip_address, int port_number) {
  Serial.print("AT+CIPSTART=\"UDP\",\"" + ip_address + "\"," + port_number + "\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("CONNECT"))
      break;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
  while(true) {
    if(Serial.find("OK"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

bool esp8266_transmit_udp(String text) {
  Serial.print("AT+CIPSEND=" + String(text.length()) + "\r\n");
  unsigned long startTime = millis();
  while(true) {
    if(Serial.find("OK"))
      break;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
  while(true) {
    if(Serial.find(">"))
      break;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
  Serial.print(text);
  while(true) {
    if(Serial.find("SEND OK"))
      return true;
    if(millis() > startTime + MAX_COMMAND_TIME)
      return false;
  }
}

I'll try that later, need to run. Thanks.

looks like eventTime_4_regn is 30 sec. rainfall may be incremented millions of times in that period

Yes, it is. No, rainfall only increments when low, in reality max 4-5times over 30sec. The thing is that if i set "eventtime_4" to 1ms it still gives me this wierd output. Digital hall sensor, high = no input.

how long does esp8266_transmit_udp() take to complete

I do not have an exact figure on that, and i know it stalls the code a bit, even so the dht read, and pressue sensor reading takes a bit of time, but it still gives a good output. But worst case would then be that digitalread on pin 9 "misses" one read, right? No way near optimal code, i know, still i want to know whats going on.

Hi,
You have got the IDE monitor set to 9600 baud?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

it's good to thoroughly understand the code.

suggest simplifying the code until you get reliable and understandable results, perhaps for than just the cases you're interested in.

then start adding code back in until things become flaky

perhaps the Rain pin should be configured as INPUT_PULLUP

Yes, it feels kind of mandatory if one want to really learn something.

Yeah, will spend my night with this, hopefully i will find a solution.

It does have a 10k external pullup, but i will try it and se what happens.

So.... i was stupid (yeah, stupid...) enough to belive that my hallsensor signal was "clean", no! it was not. LOADS of chattering, this was also the problem. A simple debounce routine proves it, as it works as aspected, float to char and send in an udp packet. Thanks everyone for kind and helpful input! //J

we all make mistakes. don't beat yourself up
some of us learn from them