Pages: [1]   Go Down
Author Topic: Why am I getting this warning?  (Read 422 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   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:
#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.
« Last Edit: May 29, 2009, 08:35:12 pm by ajb » Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if( status != lastStatus && millis() - lastChangeTime > debounceDelay ) {

The reference for millis describes it as:

Code:
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. smiley

--Phil.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? smiley

Thanks!
« Last Edit: May 29, 2009, 11:36:01 pm by ajb » Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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? smiley
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-grin

--Phil.
Logged

Pages: [1]   Go Up
Jump to: