Comparing mills()

Hi guys,

I don’t have a full understanding of variable types and usage, and I’m trying to figure out how to address a repeated error in my code. It compiles and works, but I’d like to eliminate the errors.

The error I get is:

comparison of integer expressions of different signedness: ‘system_tick_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]

In this line of code:

currentMillis = millis();

I declare the variable here:

int currentMillis;

I have tried to redefine the variable as unsigned int, unsigned long int, etc with no change.
how can I compare millis() or recast them so the error clears?
thx

int (aka int32_t) is a signed 32 bit integer but millis() returns an unsigned int (aka uint32_t).
The former has only half the possible positive numbers compared to the latter. Hence you may run into troubles when dealing with numbers > 2^31

Can you show the exact code where this warning pops up?
You show an assignment but then talk about a comparison, so I’d suspect the issue is not where you are looking at (or you define the same variable in multiple places).

@ScruffR

Well, it happens in my loop, for example, as below:

void loop() {
currentMillis = millis();
checkIfNotificationNeeded();
LEDHandler();
renewUDP();
}

The word compare came from the error message:

comparison of integer expressions of different signedness: ‘system_tick_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]

Though a strange thing is that the error says that it occurs in the line where the closing ‘}’ is…

I also get the error here as a comparison - on the line that closes ‘void interruptCode()’

void interruptCode() {
    if (millis() > (interruptTime + 25)) {
        active = true;
        //inactiveSent = FALSE;
        interruptTime = millis();
        count ++;
        if (count5 < 5) {
            if (count5 == 1) {
                rpm5start = interruptTime;
            }
            count5 ++;
        }
        else {
            rpm5time = (millis() - rpm5start);
            rpm5 = (60000 / rpm5time * 5);
            count5 = 0;
        }
    }
}

Actually it may be in this:

void renewUDP() {

   if ((UDPStartSecs + 3600) < (millis()/1000)) {
        Udp.stop();
        Udp.begin(UDP_PORT);
        Udp.joinMulticast(remoteIP);
        UDPStartSecs = (millis()/1000);
    }

}

For a unsigned compare all involved variables should be unsigned.

I’d rewrite your comparisons like this (when posting code snippets always provide the relevant variable definitions too)

const uint32_t msUdpReconnect = 60*60*1000; // renew UDP "connection" every hour
const uint32_t msIsrDebounce  = 25;         // milliseconds to ignore subsequent interrupts
uint32_t msIsrTime;                         // timestamp of last interrupt acceptance
uint32_t msUdpLastStart;                    // timestamp of last UDP "reconnect"
...
  if (millis() - msIsrTime > msIsrDebounce) {
    ...
  }
...
  if (millis() - msUdpLastStart > msUdpReconnect) {
    ...
    msUdpLastStart = millis();
  }

BTW

Which closing curly brace?
I suspect the one that closes the if() { ... } block as that is the end of the statement. The compiler cannot be certain about the validity of a statement before it has fully parsed it and once it has it only gives you the place of where/when it found the error.

1 Like

@ScruffR, Thanks! You always seem to have the answer. :slight_smile: Your assistance has helped me thru dozens of hiccups over the past few years, you’re a huge asset to the community.

I’ll spend some time reworking the variables later today.

And yes, it was the Curly brace that closed the If block…

J

2 Likes