Go Down

Topic: Why am I getting this warning? (Read 428 times) previous topic - next topic

ajb

May 30, 2009, 03:34 am Last Edit: May 30, 2009, 03:35 am by ajb Reason: 1
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


Quote
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:

Code: [Select]
   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:

Code: [Select]
#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.

follower

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):

Code: [Select]
if( status != lastStatus && millis() - lastChangeTime > debounceDelay ) {

The reference for millis describes it as:

Code: [Select]
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. :)

--Phil.

ajb

#2
May 30, 2009, 06:34 am Last Edit: May 30, 2009, 06:36 am by ajb Reason: 1
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? :)

Thanks!

follower

Quote
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? :)

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. :D

--Phil.

Go Up