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?