Error in simple multiplication if done in one line

In one part of the code I tried doing this :

delay_time_ms=delay_time*60*1000;

delay_time variable stores a positive integer value. It is usually within 1-20 depending on what the user selects through a touch interface. delay_time_ms is simply a convertion of that value to milliseconds. And the answer always yields to something as big as 4xxxxxxx which I later found out by using a serial print of that variable.
If I split up the line and do this, then the result is calculated just fine:

delay_time_ms=delay_time*60;
delay_time_ms=delay_time_ms*1000;

I can't figure out why this strange behaviour is happening.
Entire code is also attached.
Required libraries are here due to file size restrictions: 7.2 MB file on MEGA

sequence_controller_3_beta_GSLC.h (16.7 KB)

sketch_jan11a.ino (25.2 KB)

Hello,

Because 60 and 1000 are seen by the compiler as int (16 bits, signed), so the result of 601000 will also be an int, even if variable delay_time_ms is unsigned long. 60000 doesn't fit in an int so you will see the result of the overflow. If you change to 601000UL you tell the compiler to treat the numbers as unsigned long and it will give the correct result :slight_smile:

also delay_time looks rather small for all those big numbers:

uint8_t delay_time= 1;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.