Why does millis() output such large numbers

I am trying to count the time it takes for a mass to fall. I have a switch what is opened when I release and a switch on the floor that closes when the mass hits it. The millis() value that I output for this time is huge, like: 4294966684. I don't even know what this number represents. Can someone please help me determine what is going on with this? I would think the result would be in msec. This is urgent as this assignment is due today.
My code:

unsigned long releaseTime=0;

unsigned long startTime=0;

int Boolean1=0;

const int pin11=11;

int PIN11STATE=0;

unsigned long dropTime=0;

int Boolean2=0;

const int pin9=9;

int PIN9STATE=9;

void setup() {

// put your setup code here, to run once:

pinMode(9,INPUT);

pinMode(11, INPUT);

Serial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

PIN9STATE=digitalRead(pin9);

PIN11STATE=digitalRead(pin11);

if ((PIN11STATE==LOW)&&(Boolean1==0)){

releaseTime=millis();

Boolean1=1;

// Serial.print(releaseTime/1000);

}

if ((PIN9STATE==HIGH)&&(Boolean2==0)){

dropTime=millis();

// Serial.print(dropTime);

Serial.print((releaseTime)-(dropTime));

Boolean2=1;

}

}

You are subtracting two unsigned numbers.

If releaseTime is less than dropTime, the value of (releaseTime)-(dropTime) would be negative.

However, since both operands are unsigned, the result will be unsigned as well. So instead of getting a negative number, you get a very large positive one. See Integer overflow.

Two solutions:

  1. Reverse the order to produce a positive result, ie: dropTime-releaseTime.
  2. Explicitly cast the result to be signed, ie. (signed long)(releaseTime-dropTime).

Ok, thanks for the info.

You could have avoided the confusion if you had given the pins, their states and the millis() values meaningful names

Perhaps startPin, stopPin, startPinState, stopPinState, startTime and stopTime

Then it would be obvious that you needed to print stopTime - startTime

Your question does not sound like an IDE problem and hence your topic has been moved to a more suitable location on the forum.

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