Hi,
when you do computations with constant values some precautions are needed.
A function to compute a date number from a date record initially got false results on an Arduino Nano:
unsigned long datenum(int ee_adr) {
return( EEPROM[ee_adr+9] + EEPROM[ee_adr+8]*10 + // Minuten
(EEPROM[ee_adr+7] + EEPROM[ee_adr+6]*10)*60 + // Stunden
(EEPROM[ee_adr+5] + EEPROM[ee_adr+4]*10)*1440 + // Tage
(EEPROM[ee_adr+3] + EEPROM[ee_adr+2]*10)*44640 + // Monate
(EEPROM[ee_adr+1] + EEPROM[ee_adr]*10)*535680 ); // Jahre
}
The date is stored in EEPROM with format yymmddHHMM (y:year, m:month, d:day, H:hour, M:minute).
ee_adr is pointing to the first byte of yy.
I expected that the compiler generates code to do all computations with 32bit, because the result is defined as unsigned long.
The multiplications for month and year have to be changed like the following code:
unsigned long datenum(int ee_adr) {
return( EEPROM[ee_adr+9] + EEPROM[ee_adr+8]*10 + // Minuten
(EEPROM[ee_adr+7] + EEPROM[ee_adr+6]*10)*60 + // Stunden
(EEPROM[ee_adr+5] + EEPROM[ee_adr+4]*10)*1440 + // Tage
(EEPROM[ee_adr+3] + EEPROM[ee_adr+2]*10)*long(44640) + // Monate
(EEPROM[ee_adr+1] + EEPROM[ee_adr]*10)*long(535680) ); // Jahre
}
When a constant, here 44640, can be represented by 16 bits then the computation is performed 16 bits wide only even if the result shall be unsigned long.
This is worth to know.