Ints to long conversion

Hello

Stupid question I am sure.

I have a timer value of between 0 and 300 minutes.
This is triggered using a millis() timer (timertrigMillis).

So I set up these variables:

unsigned long timertrigMillis;

byte timerlowByte;
byte timerhighByte;
int timerlength = 300; // Timer length in seconds (This variable can change)

I store the 300 variable in EEPROM with:

timerlowByte = ((timerlength >> 0) & 0xFF);          
timerhighByte = ((timerlength >> 8) & 0xFF);
EEPROM.write(7, timerlowByte);
EEPROM.write(8, timerhighByte);

I retrieve them with

timerlowByte = EEPROM.read(7);
timerhighByte = EEPROM.read(8);
timerlength = ((timerlowByte << 0) & 0xFF) + ((timerhighByte << 8) & 0xFF00);

During the code, this timerlength variable can change from the default 300 and is therefore re-saved in EEPROM accordingly.

All this works fine.

But, I then need to convert that stored timerlength variable into the unsigned long timertrigMillis, so that I can use it with my millis() timer routine.

But this doesn't work and I assume it's something to with the conversion between an int and a long.
timertrigMillis = (timerlength * 1000);

Can someone advise?

OK... I just added an 'L to the 1000:

timertrigMillis = (timerlength * 1000L);

Is that correct?

Yes. that will make the 1000 be represented as a long

but reality is that you should use unsigned long for timerlength instead of int (and for timertrigMillis). This way the formula

timertrigMillis = (timerlength * 1000);

would be conducted using unsigned long and if you want to make it very obvious you know what you are doing, you would do

timertrigMillis = timerlength * 1000ul; // ul for unsigned long

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