Strange "if" behaviour

I have an "if" statement, but its result is strange.
I cannot detect the cause of this, please help.
I put a bunch of DEBUG print to see what happens.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const byte DEBUG9 = true;	
bool lcd_be;

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

void setup() 
  {
  Wire.begin();
// Start serial communication at 115200 baud
 Serial.begin(115200); // web editoron a monitor szemetel
//  Serial.begin(9600);  // web editorhoz ez alkalmas
// Set clock speed to be the fastest for better communication (fast mode)
  Wire.setClock(400000);
//  Initiate the LCD:
    lcd.init();
    lcd.backlight();
  }  
  // void setup vége

void loop()
{
  lcd.noBacklight(); // LCD kikapcsolása
  LCD_kiiras();
}

void LCD_kiiras()
{
bool lcd_ki;  // a háttérvilágítás kikapcsolásának eleje
bool lcd_be_szamol; // bekapcsolási idő számolása
bool lcd_ki_szamol; // kikapcsolási idő számolása
bool lcd_be_ido_letelt; // letelt a bekapcsolási idő
bool lcd_ki_ido_letelt; // letelt a kikapcsolási idő
unsigned long lcd_be_ido = 2000; // amíg be van kapcsolva az LCD
unsigned long lcd_ki_ido = 5000; // amíg ki van kapcsolva az LCD
unsigned long startTime_lcdbe; // idő mérése BE állapothoz
unsigned long startTime_lcdki; // idő mérése KI állapothoz
unsigned long szamolt_idokulonbseg_be;
unsigned long szamolt_idokulonbseg_ki;

  if (DEBUG9)
    {
Serial.println( "  " ); // üres sor az olvashatóság érdekében
Serial.println( "d1  " ); // 
Serial.println( " if ( lcd_be ) elott " );
Serial.print( " lcd_be: " );
Serial.println( lcd_be );
Serial.print( " lcd_be_szamol: " );
Serial.println( lcd_be_szamol );
    }

  if ( lcd_be ) // BE idő kezdete
    {
    lcd_be = false; 
    lcd_be_szamol = true; // időmérés aktív
    startTime_lcdbe = millis(); // időmérés start a BE állapothoz
    }

  if (DEBUG9)
    {
    Serial.println( "d2  " ); // üres sor az olvashatóság érdekében    
    Serial.println( " BE állapot eleje " );
    Serial.print( " kezdoertek startTime_lcdbe: " );
    Serial.println( startTime_lcdbe );
    Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
    Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
    }

  if ( lcd_be_szamol && (lcd_be_ido_letelt == false) ) // számoljuk a BE állapot idejét
    {
szamolt_idokulonbseg_be = millis() - startTime_lcdbe;
Serial.println( "d2a  " );      
Serial.print( " szamolt_idokulonbseg_be: " );
Serial.println( szamolt_idokulonbseg_be );
Serial.print( " millis(): " );
Serial.println( millis() );
Serial.print( " startTime_lcdbe: " );
Serial.println( startTime_lcdbe );
    }

  if ( szamolt_idokulonbseg_be >= lcd_be_ido ) // eltelt a BE állapotra megadott idő
    {
      lcd_be_ido_letelt = true;
      lcd_be_szamol = false;
      lcd_ki = true;        
    }

  if (DEBUG9)
    {
Serial.println( "d3  " ); // üres sor az olvashatóság érdekében      
Serial.print( " szamolt_idokulonbseg_be: " );
Serial.println( szamolt_idokulonbseg_be );
Serial.print( " millis(): " );
Serial.println( millis() );
Serial.print( " kezdoertek startTime_lcdbe: " );
Serial.println( startTime_lcdbe );
Serial.print( " lcd_be_ido: " );
Serial.println( lcd_be_ido );
Serial.print( " lcd_be_ido_letelt: " );
Serial.println( lcd_be_ido_letelt );
Serial.print( " lcd_be_szamol: " );
Serial.println( lcd_be_szamol );
Serial.print( " lcd_be: " );
Serial.println( lcd_be );
Serial.print( " lcd_ki: " );
Serial.println( lcd_ki );
	  }
}

The output starts like this:

*TMP117 Basic Readings

d1

if ( lcd_be ) elott

lcd_be: 1

lcd_be_szamol: 0

d2

BE állapot eleje

kezdoertek startTime_lcdbe: 5475

lcd_be: 0

lcd_be_szamol: 1

d3

szamolt_idokulonbseg_be: 96

millis(): 5620

kezdoertek startTime_lcdbe: 5475

lcd_be_ido: 2000

lcd_be_ido_letelt: 1

The strange thing happens within this if statement:

if ( szamolt_idokulonbseg_be >= lcd_be_ido )

It gives true:
lcd_be_ido_letelt: 1

But:
szamolt_idokulonbseg_be = 96
lcd_be_ido = 2000

How can be 96 bigger than 2000?

Sorry for the Hungarian text, and because the code is a shortened version. (it has a symmetrical if part for the OFF state, this code is for the ON state)

My main concern is this if logic, which I do not understand.

Both szamolt_idokulonbseg_be and lcd_be_ido are unsigned long variables.

What do I miss?

I would say that your code is "strange" :slight_smile:
You have if (DEBUG9) 3 times, with different Serial.print statements?

Maybe lcd_be_ido_letelt was set to true on a previous loop ? Once set it never gets set back to false.

The code shown does not output the claimed data.

I put these serial.prints to see the values.
Before and after the code does something.

You are right. It was not set back. I inserted now to set it to "false".

[quote="Whandall, post:4, topic:1071149, full:true"]
The code shown does not output the claimed data.

But this is in the code and also in the output. I do not understand what you miss here.

The way to analyse something like this is to completely stop assumings
but instead print out each and every detail
at that place that seems to act strange. In this case this means not printing it anywhere in your code but right at that place where the strange thing happends

  if ( szamolt_idokulonbseg_be >= lcd_be_ido ) // eltelt a BE állapotra megadott idő
  {
    Serial.print("szamolt_idokulonbseg_be=");
    Serial.print(szamolt_idokulonbseg_be);
    Serial.print(" >= ");
    Serial.print("lcd_be_ido=");
    Serial.print(lcd_be_ido);
    Serial.print(" szamolt_idokulonbseg_be >= lcd_be_ido=" );
    Serial.println(szamolt_idokulonbseg_be >= lcd_be_ido);

    Serial.print(szamolt_idokulonbseg_be);
    Serial.print(" >= ");
    Serial.println(lcd_be_ido);

    }
    lcd_be_ido_letelt = true;
    lcd_be_szamol = false;
    lcd_ki = true;
  }

to make readable what values the variables really have

repeating executing your code 10 to 50 times with always again scratching your head" "why does it do this??"
does not help analysing
invest the time in serial debug-output that makes readable all and every detail

best regards Stefan

i think you're concluding that that condition is true because that is the only way "lcd_be_ido_letelt" can be set true. but "lcd_be_ido_letelt" is defined within the function and not initialized.

Very good analysis by just reading the code.
still the second way to analyse such things is to make visible what the code is doing so adding

  if ( szamolt_idokulonbseg_be >= lcd_be_ido ) // eltelt a BE állapotra megadott idő
  {
    Serial.println("szamolt_idokulonbseg_be >= lcd_be_ido  is true");
    Serial.println("setting lcd_be_ido_letelt = true");
    lcd_be_ido_letelt = true;
    
    Serial.println("setting lcd_be_szamol = false");
    lcd_be_szamol = false;
    
    Serial.println("setting lcd_ki = true;");
    lcd_ki = true;
  }

which makes again readable what the code is really doing
if these lines
setting lcd_be_ido_letelt = true;
setting lcd_be_szamol = false;
setting lcd_ki = true;

do not appear you can conclude:
lcd_be_ido_letelt must be true from somewhere else

and maybe to add debug-output

    if (lcd_be_ido_letelt == true) {
      Serial.println("lcd_be_ido_letelt is TRUE!");
    }

at different places inside your code

I have written three macros that make adding serial debug-output more comfortable
as these macros reduce the code to write

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

You type a single line of code that replaces multiple basic lines
In your case the dbgc-macro would be useful
dbgc prints only in case the value of the variable has changed
works for bool and for integer-variables up to long (signed 32bit)

here is your code with the macros added and multiple lines

  dbgc("2:", lcd_be_ido_letelt);

alternatively you could use something like

  dbgi("3:", lcd_be_ido_letelt,1000);

which prints to the serial monitor only once per second (once every 1000 milliseconds)
"3:", lcd_be_ido_letelt=1
or
"3:", lcd_be_ido_letelt=0

1 if lcd_be_ido_letelt is true
0 if lcd_be_ido_letelt is false

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const byte DEBUG9 = true;
bool lcd_be;

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

void setup()
{
  Wire.begin();
  // Start serial communication at 115200 baud
  Serial.begin(115200); // web editoron a monitor szemetel
  //  Serial.begin(9600);  // web editorhoz ez alkalmas
  // Set clock speed to be the fastest for better communication (fast mode)
  Wire.setClock(400000);
  //  Initiate the LCD:
  lcd.init();
  lcd.backlight();
}
// void setup vége

void loop()
{
  lcd.noBacklight(); // LCD kikapcsolása
  LCD_kiiras();
}

void LCD_kiiras()
{
  bool lcd_ki;  // a háttérvilágítás kikapcsolásának eleje
  bool lcd_be_szamol; // bekapcsolási idő számolása
  bool lcd_ki_szamol; // kikapcsolási idő számolása
  bool lcd_be_ido_letelt; // letelt a bekapcsolási idő
  bool lcd_ki_ido_letelt; // letelt a kikapcsolási idő
  unsigned long lcd_be_ido = 2000; // amíg be van kapcsolva az LCD
  unsigned long lcd_ki_ido = 5000; // amíg ki van kapcsolva az LCD
  unsigned long startTime_lcdbe; // idő mérése BE állapothoz
  unsigned long startTime_lcdki; // idő mérése KI állapothoz
  unsigned long szamolt_idokulonbseg_be;
  unsigned long szamolt_idokulonbseg_ki;
  dbgc("top of LCD_kiiras()", lcd_be_ido_letelt);

  if (DEBUG9)
  {
    Serial.println( "  " ); // üres sor az olvashatóság érdekében
    Serial.println( "d1  " ); //
    Serial.println( " if ( lcd_be ) elott " );
    Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
    Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
  }

  if ( lcd_be ) // BE idő kezdete
  {
    lcd_be = false;
    lcd_be_szamol = true; // időmérés aktív
    startTime_lcdbe = millis(); // időmérés start a BE állapothoz
  }

  if (DEBUG9)
  {
    Serial.println( "d2  " ); // üres sor az olvashatóság érdekében
    Serial.println( " BE állapot eleje " );
    Serial.print( " kezdoertek startTime_lcdbe: " );
    Serial.println( startTime_lcdbe );
    Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
    Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
  }

  dbgc("2:", lcd_be_ido_letelt);

  if ( lcd_be_szamol && (lcd_be_ido_letelt == false) ) // számoljuk a BE állapot idejét
  {
    szamolt_idokulonbseg_be = millis() - startTime_lcdbe;
    Serial.println( "d2a  " );
    Serial.print( " szamolt_idokulonbseg_be: " );
    Serial.println( szamolt_idokulonbseg_be );
    Serial.print( " millis(): " );
    Serial.println( millis() );
    Serial.print( " startTime_lcdbe: " );
    Serial.println( startTime_lcdbe );
  }

  dbgc("3:", lcd_be_ido_letelt);
  if ( szamolt_idokulonbseg_be >= lcd_be_ido ) // eltelt a BE állapotra megadott idő
  {
    lcd_be_ido_letelt = true;
    lcd_be_szamol = false;
    lcd_ki = true;
  }

  dbgc("4:", lcd_be_ido_letelt);
  if (DEBUG9)
  {
    Serial.println( "d3  " ); // üres sor az olvashatóság érdekében
    Serial.print( " szamolt_idokulonbseg_be: " );
    Serial.println( szamolt_idokulonbseg_be );
    Serial.print( " millis(): " );
    Serial.println( millis() );
    Serial.print( " kezdoertek startTime_lcdbe: " );
    Serial.println( startTime_lcdbe );
    Serial.print( " lcd_be_ido: " );
    Serial.println( lcd_be_ido );
    Serial.print( " lcd_be_ido_letelt: " );
    Serial.println( lcd_be_ido_letelt );
    Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
    Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
    Serial.print( " lcd_ki: " );
    Serial.println( lcd_ki );
  }
  dbgc("5:", lcd_be_ido_letelt);
}

best regards Stefan

That is simple, the code

Serial.println( "d2a  " );      
Serial.print( " szamolt_idokulonbseg_be: " );
Serial.println( szamolt_idokulonbseg_be );

can not print the claimed

output.

@StefanL38

Thanks for your debug-code, I saved it for later use!

@StefanL38

I changed my code, and now it works as I intended!

Thank you guys! :+1:

Actual and working part of my code is below.

Previously I had to use and insert codes from others.
Which either worked or not as I needed. In both cases I did not know: why? "How can I change something in the code?"
I realized that I have to create my own code after my logic.
(maybe it is a spagetti-code now, not elegant or short - but I know what is what for and why I put it there).

Useful advice was that I have to check the settings of the "bool" variables:
"Do they have both states?"
And also the numerical values (I forgot to zero the sum variable
szamolt_idokulonbseg_be , for example).

Only the changed part (I created 6 DEBUG bool from 1)

  if (DEBUG91)
    {
    Serial.println( "  " ); // empty line for easier read
    Serial.println( "d1  " ); // 
    Serial.println( " if ( lcd_be ) elott " );
	  Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
	  Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
    }

  if ( lcd_be ) // ON time start
    {
    lcd_be = false;
	  lcd_be_ido_letelt = false;	
    lcd_be_szamol = true; // time measure active
    startTime_lcdbe = millis(); // time measure start for ON state
    }

  if (DEBUG92)
    {
    Serial.println( "d2  " ); // empty line
    Serial.println( " BE állapot eleje " );
    Serial.print( " kezdoertek startTime_lcdbe: " );
    Serial.println( startTime_lcdbe );
    Serial.print( " lcd_be: " );
    Serial.println( lcd_be );
    Serial.print( " lcd_be_szamol: " );
    Serial.println( lcd_be_szamol );
    }

  if ( lcd_be_szamol && (lcd_be_ido_letelt == false) ) // calculating ON time
    {
      szamolt_idokulonbseg_be = millis() - startTime_lcdbe;
      
      Serial.println( "d2a  " );      
 		  Serial.print( " szamolt_idokulonbseg_be: " );
      Serial.println( szamolt_idokulonbseg_be );
	    Serial.print( " millis(): " );
      Serial.println( millis() );
      Serial.print( " startTime_lcdbe: " );
      Serial.println( startTime_lcdbe );
    }

  if ( szamolt_idokulonbseg_be >= lcd_be_ido ) // ON time is over the given value
    {
      lcd_be_ido_letelt = true;
      lcd_be_szamol = false;
      lcd_ki = true;
      szamolt_idokulonbseg_be = 0;      
    }

  if (DEBUG93)
    {
      Serial.println( "d3  " ); // 
 		  Serial.print( " szamolt_idokulonbseg_be: " );
      Serial.println( szamolt_idokulonbseg_be );
	    Serial.print( " millis(): " );
      Serial.println( millis() );
      Serial.print( " kezdoertek startTime_lcdbe: " );
      Serial.println( startTime_lcdbe );
	    Serial.print( " lcd_be_ido: " );
      Serial.println( lcd_be_ido );
      Serial.print( " lcd_be_ido_letelt: " );
      Serial.println( lcd_be_ido_letelt );
	    Serial.print( " lcd_be_szamol: " );
      Serial.println( lcd_be_szamol );
	    Serial.print( " lcd_be: " );
      Serial.println( lcd_be );
	    Serial.print( " lcd_ki: " );
      Serial.println( lcd_ki );
	  }
    // if (DEBUG93) end

// LCD OFF state
  if (DEBUG94)
    {
    Serial.println( "d4  " ); // 
	  Serial.println( " if ( lcd_ki ) elott " );
	  Serial.print( " lcd_ki: " );
    Serial.println( lcd_ki );
	  Serial.print( " lcd_ki_szamol: " );
    Serial.println( lcd_ki_szamol );
    }

  if ( lcd_ki ) // OFF state start
    {
    lcd_ki = false;
  	lcd_ki_ido_letelt = false;	
    lcd_ki_szamol = true; // time measure active for OFF
    startTime_lcdki = millis(); // time measure start
  
    Serial.print( " startTime_lcdki: " );
    Serial.println( startTime_lcdki );
    }

  if (DEBUG95)
    {
      Serial.println( "d5  " ); // 
	    Serial.println( " KI állapot eleje " );
	    Serial.print( " lcd_ki: " );
      Serial.println( lcd_ki );
	    Serial.print( " lcd_ki_szamol: " );
      Serial.println( lcd_ki_szamol );
    }

  if ( lcd_ki_szamol && (lcd_ki_ido_letelt == false) ) // calculating time for OFF
    {
      szamolt_idokulonbseg_ki = millis() - startTime_lcdki;
    }

  if ( szamolt_idokulonbseg_ki >= lcd_ki_ido ) // LCD can be OFF
    {
      lcd_ki_ido_letelt = true;
      lcd_ki_szamol = false;
      lcd_be = true;
      szamolt_idokulonbseg_ki = 0;
    }

  if (DEBUG96)
    {
      Serial.println( "d6  " ); // 
 		  Serial.print( " szamolt_idokulonbseg_ki: " );
      Serial.println( szamolt_idokulonbseg_ki );
	    Serial.print( " millis(): " );
      Serial.println( millis() );
	    Serial.print( " lcd_ki_ido: " );
      Serial.println( lcd_ki_ido );
      Serial.print( " lcd_ki_ido_letelt: " );
      Serial.println( lcd_ki_ido_letelt );
	    Serial.print( " lcd_ki_szamol: " );
      Serial.println( lcd_ki_szamol );
	    Serial.print( " lcd_be: " );
      Serial.println( lcd_be );
	    Serial.print( " lcd_ki: " );
      Serial.println( lcd_ki );
	  }
    // if (DEBUG96) end

Additional info for later use:

The purpose of the code was to create a state machine with 2 states.
The states come after each other with different length time periods.
No button or any other outside effect was used, the code must run if power is on.
(this caused me a problem, because the code must be started somehow, but only once - so I put the starting variable into the beginning of the code to run only once).

I put many DEBUG bool variables into the code which contain serial.prints to see what happens at different phases. This technique was useful to follow what the code does actually (and not just what I assumed it does).

I develop the code mostly in Wokwi site, which was easy and fast, I recommend it. It was easy to stop the code running and copy the serial output into a text file and search the code with the debug and serial.print info.

The DEBUG parts can be switched ON / OFF at the beginning of the code, so later they can be easily switched off, when they are not necessary. (previously I inserted serial.prints then I put comment signs into the lines: //) If you have many serial.print lines, commenting them is time consuming and not effective.

that sound like a partial explanation of how it is implemented, not a description of the purpose of the code

and with the Hungarian words, its even hard to reverse engineer

very unclear what the purpose of the code is.

looks like it repeatedly executes some code for 2 seconds and then stops. the numerous boolean flags are confusing

the following code assumes that some event needs to happen every 2 seconds, producing the following outpu

d2a   szamolt_idokulonbseg_be: 0 millis(): 2000 startTime_lcdbe: 2000
d2a   szamolt_idokulonbseg_be: 0 millis(): 4000 startTime_lcdbe: 4000
d2a   szamolt_idokulonbseg_be: 0 millis(): 6000 startTime_lcdbe: 6000
void LCD_kiiras()
{
    unsigned long msec = millis ();

    // számoljuk a BE állapot idejét
    if (msec - startTime_lcdbe  >= Lcd_be_ido) {
        startTime_lcdbe = msec;
        Serial.print  ( "d2a  " );
        Serial.print  ( " szamolt_idokulonbseg_be: " );
        Serial.print  ( szamolt_idokulonbseg_be );
        Serial.print  ( " millis(): " );
        Serial.print  ( millis() );
        Serial.print  ( " startTime_lcdbe: " );
        Serial.println( startTime_lcdbe );
    }
}

several unused variables were removed, including unsigned long lcd_ki_ido = 5000; which suggest that something may need to occur after 2 seconds and the other thing after 5 seconds, to complete a 7 second cycle.

if that the case, consider

action1: 2000
action2: 7000
action1: 9000
action2: 14000
action1: 16000
action2: 21000
action1: 23000
action2: 28000
action1: 30000
action2: 35000
bool flag;

const unsigned long Period1 = 2000;
const unsigned long Period2 = 5000;

unsigned long msecPeriod = Period1;
unsigned long msecLst;
unsigned long msec;

// -----------------------------------------------------------------------------
void
action1 (void)
{
    Serial.print ("action1: ");
    Serial.println (msec);
    msecPeriod = Period2;
}

// -------------------------------------
void
action2 (void)
{
    Serial.print ("action2: ");
    Serial.println (msec);
    msecPeriod = Period1;
}

// -------------------------------------
void loop()
{
    msec = millis ();

    if (msec - msecLst >= msecPeriod)  {
        msecLst = msec;

        flag = ! flag;
        if (flag)
            action1 ();
        else
            action2 ();
    }
}

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

What do you suggest with the variables?
It won't be easy to translate all the variables text into English.
I am sorry if the logic is not visible now in the code.

For me, the English variables are sometimes confusing, as I don't know if they are "official" names (like " isAlphaNumeric() ") or just user-created variables or functions like waitForButtonPressed or currentMillis. For me (as I am not an experienced C++ programmer) they have similar naming convention.

Previously I copied these codes and variables and hoped they will work somehow, but it was not clear how they work.

I did not tell that the above code is streamlined or compacted. It is sometimes confusing even for me.
In the development process sometimes I created new variables or renamed old ones. I am not sure which one is better programming practice.
I do not know how to find unused variables (for example).

For me it is better to look into others code, and if I catch the point "OK I understand what it does", then I try to write my own code according to the ideas of others.

I know what I expect from my code to perform, so I have to check if it does what I expect. It also was not easy to correct until I reach that stage.

I see your code, @gcjr , and more or less I undestand how it works, thanks for the ideas!

again
what is the purpose of your code?

Please post your complete sketch. That will do the most good for the people who stumble on to the problem you solved.

TIA

a7

@gcjr This part of my code makes a choice between two states: a). LCD is ON, and b). LCD is OFF. It has to be performed with millis() instructions.
The states have two different time intervals and come after each other.
The code runs as power is on, without a "start signal" or "pressing a button".

The whole code uses a TMP117 sensor to measure and display precise room temperature.
I apply a Kalman-filter to smooth the measured value.
I did the built-in LED blinking at the actual value and the LCD shows the same value. (the LED blinking use delay functions as these are short time periods)

I thought it is unnecessary to light the LCD all the time, so I decided to switch it ON/OFF periodically. (the temperature does not change fast in a living room).
(maybe later I will use the Arduino with a battery)

@alto777 Will the code be useful with Hungarian texts?? (all variables, explanations, comments, ...)

To run the code you should have a TMP117 temp sensor attached to your Arduino (or have to comment out many lines - it is also not an easy task, I could not figure out an easier way to comment out certain lines then back). (The test values are commented out now).