Output number changes from positive to negative

Hi all,
A while ago I created a script to run a timer to control up to 28 relays in an infinite loop. I made the script with menus and to use a keypad to change the delays between each relay, as well as the time the individual relays will stay on. There is also a screen in the system to show what you're doing.

To start with I had the delays in seconds, but since the timings need to be more accurate I changed it to milliseconds.

I have just discovered that for some reason if I input a delay time of larger than 36767 it changes the output to a negative number and it makes the timer inoperative. (I might have that number incorrect by a few digits, forgot my paperwork at work, but it's close to that number. The first negative number I think is -36768)

The code for the Default delay (offTime) and onTime is

int onTime = 10 ; // seconds on
int offTime = 3000 ; // milliseconds off

And the part where it uses these times for the relays is, (The relays control blower motors, which is why I called it Blower)

void ManageBlowers() {
if (millis() - BlowerStartTime > offTime * 1UL)
    {
    StartBlower(CurrentBlower);
    CurrentBlower++;
    if (CurrentBlower >= BlowerCount)
      CurrentBlower=0;
    }
for (int i = 0; i < BlowerCount;i++)
  {
  if(millis()-Blowers[i].StartedTime > onTime * 1000UL)
    {
    StopBlower(i);
    }
  }
}

Can anyone help me with a reason why this is happening? My apologies if I use the incorrect terminology, but I'm very new to the Arduino coding world, and this was my first script.

Hope someone can help

Could you read the next link and then explain it yourself ?
https://www.arduino.cc/reference/en/language/variables/data-types/int/

Arduino data types.

Thanks for the replies.
So if I understand it correctly I need to make the integer an unsigned data type? I will give this a shot in the morning when I get back to the office.
Thanks again!

No !

If you do something with millis(), then use 'unsigned long'. That will go up to 50 days.

1 Like

Sorry, was just getting to that part in the data types link sent by groundFungus. I will go through this very carefully now and try the different types for testing.
Thanks a lot

Look at the data types. An unsigned in will hold numbers 0 through 65535.

The millis() function returns unsigned long data type. For that reason, any variables used for timing are best declared as unsigned long.

2 Likes

Starting to get clear on this now, thanks for the help.

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