If statement fires wrong

I’m looking at my code for a couple of hours now and i can’t find the problem.
I’m making a solar logger based on puls counting.

This is part of my program where it checks every 15 min if our home ventilator still needs to be on.
Also after 5 o clock it has to sync the time.

I have problems with the sync time.
Every 15 min it syncs the time but on the serial print syncDone is already 1.

syncDone will change to false if my controler didn’t do anything for 10 min.

  //Slow loop
  if((millis() - slowLoop) > 900000UL || slowLoop == 0)
  {
    //time sync
    getDateDs1307();
    if (hour2 >= 6 && hour2 <= 23)
    {
      //centrale ventilator aan
      ventilator = true;
      Serial.println("aan");
      scherm (); 
    }
    else
    {
      //centrale ventilator uit
      ventilator = false;
      Serial.println("uit");
      scherm (); 
    }

    //later dan 5 uur time goed zetten
    if (hour2 > 5 && SyncDone == false);
    {
      Serial.println(SyncDone); 
      SyncDone = true;
      SyncTime();
      scherm (); 
    }

    slowLoop = millis();
  }

An other error i can’t solve is this code.
It is called every time my screen updates. It checks if it is still the same day or else it schould reset some values.
the value dayOfMonth2 is set every time i ask the time from my RTC (every puls i get)

When i reset the controller it sets day2 perfect (day2 was 0). But the next day it will not reset.
Both day2 and dayOfMonth2 are byte values.

  if (day2 != dayOfMonth2)
  {
    Serial.println("reset");
    maxvermogen = 0;
    kwhDag = 0;
    day2 = (dayOfMonth2);
  }

My whole code is around 1000 lines so i will not post it all.

You can attach the. ino(or.pde) to the post, it would help to see the entire code

I added it.

I’m using an EtherMega.

This code also runs from time to time while it schould not.

 // scherm slaap mode and upd eneable 1 sec loop
  if ((millis() - Milisec) > 600000UL and (millis() - fastLoop) > 1000)
  {
    if (lcdDisplay == true)
    {
      Serial.println(Tijd);
      Serial.println(Milisec);
      Serial.println(millis());
      Serial.println("lcd off");    
      //lcd.noDisplay();
      lcdDisplay = false;
      starttijdtussenpuls2 = 0;
      starttijdtussenpuls = 0;
      countNr = 1 ;
      UDP = true;
      vermogen = 0;
    }

    getDateDs1307();
    Network();
    fastLoop = millis();
    scherm (); 
  }

When it does it gives to following values:
MiliSec ;
Millis() ;
7254670
7254670

MiliSec ;
Millis() ;
7273494
7273494

MiliSec ;
Millis() ;
7392316
7392317

solar_prog.ino (17.4 KB)

Eggybert:

  if((millis() - slowLoop) > 900000UL || slowLoop == 0)

if( ((millis() - slowLoop) > 900000UL) || (slowLoop == 0) )





if (hour2 >= 6 && hour2 <= 23)
    if ( (hour2 >= 6) && (hour2 <= 23)





if (hour2 > 5 && SyncDone == false); <<< note the spurious trailing semicolon
    if ( (hour2 > 5) && (SyncDone == false) )

When you have compound expressions like this, it’s best to use parentheses to show your intended order of evaluation. I haven’t reviewed all your code to see whether this is your problem, but at the very least it will enable you to rule that out. And check for spurious semicolons elsewhere. IF statements control the execution of the immediately following statement - and the semicolon constitutes a statement. This means that whatever follows the semicolon is not controlled by the IF statement and will execute regardless - causing just the sort of problem you have.

That solved one problem, stupid me :stuck_out_tongue:

if (hour2 > 5 && SyncDone == false); <<< note the spurious trailing semicolon
    if ( (hour2 > 5) && (SyncDone == false) )

Found the problem
in the interrupt routine the value Milisec is set

void Pulse ()
{

  //Filter
  if( Milisec == 0 || millis() - Milisec > 1000)
  {
    readkwhpuls = true;
    Milisec = millis();
  }

}

But because the happens in an interrupt routine it sometimes gives wrong values (dunno why).
And causes this to fire sometimes:

  if ((millis() - Milisec) > 600000UL and (millis() - fastLoop) > 1000)

i changed the code so I set the Milisec later in the normal code and it is working fine now.

New code is added

solar_prog_GLCD.ino (21.7 KB)