Why am I getting this warning?

Hopefully someone can point out what I'm missing here. I just transitioned a class into its own library (which was a bit of a learning experience for me), and now I'm getting this error

warning: comparison between signed and unsigned integer expressions

from this function in the library .cpp. I've indicated which line the warning is indicated on, and I can't for the life of my understand why that would cause this warning:

    void DebouncedInput::refresh() {
      status = digitalRead(pin);
      if( status != lastStatus && millis() - lastChangeTime > debounceDelay ) {
        changed=true; /*warning indicated on this line*/
        pressed = inverted ? !status : status;
        released = !pressed;
        lastStatus = status;
        prevChangeTime = lastChangeTime;
        lastChangeTime = millis();
      }
      else {
        changed=false;
        lastReadTime=millis();
      }
    }

Here's the library .h file in its entirety:

#ifndef SmartInput_h
#define SmartInput_h

class DebouncedInput {
  public:
    DebouncedInput( int inPin, int dbTime);
    DebouncedInput (int inPin, int dbTime, boolean inv );
    
    void refresh();  // Read current status
    long sinceLast(); //time since last status change
    long lastInterval();
    void pullup(boolean pullup);

    int pin;
    boolean status;
    boolean inverted;  //True if a HIGH==released && LOW==pressed at the input, False otherwise.
    long lastReadTime; //time of latest read
    long lastChangeTime;  //time of most recent change
    long prevChangeTime;//time of previous change
    long debounceDelay;
    boolean lastStatus;  //status in last iteration 
    boolean pressed;
    boolean released;
    boolean changed;
    
};

#endif

Thanks in advance.

The fact the error message mentions a comparison issue makes me suspect the line with the error is actually this one (also the fact the IDE can get confused about exactly which line has the error):

if( status != lastStatus && millis() - lastChangeTime > debounceDelay ) {

The reference for millis describes it as:

unsigned long millis()

Your declarations of lastChangeTime and debounceDelay make them of type (signed) long so I suggest changing them to unsigned long.

I haven't tested this thought though, so it could be wrong. :slight_smile:

--Phil.

Aha, that did it!

I had suspected it was actually in that if() statement but I didn't think to check what millis() would be returning. Although it makes perfect sense for it to return an unsigned long once you think about it, doesn't it? :slight_smile:

Thanks!

I didn't think to check what millis() would be returning. Although it makes perfect sense for it to return an unsigned long once you think about it, doesn't it? :slight_smile:

In some ways it's an unfortunate choice to make millis() return an unsigned type--it's really held back people wanting to build time machines with the Arduino. :smiley:

--Phil.