millis starts counting in negative numbers

Hey guys i am using the millis() functiuon to create a timestamp, then keep track of time using "time = millis - timestamp" logic. Here is my code:

void loop(){

Serial.println("#S|CPBLTST|[]#");
Serial.println("#S|THRMLOG|[Time(ms);Vin0;Vin1;Vin2;Vin3;Vin4;Vin5;Vin6;Vin7;Vin8]#");
** tStart = millis();**
while(true){

Vin0 = analogRead(v0Pin); Vin1 = analogRead(v1Pin); Vin2 = analogRead(v2Pin);
Vin3 = analogRead(v3Pin); Vin4 = analogRead(v4Pin); Vin5 = analogRead(v5Pin);
Vin6 = analogRead(v6Pin); Vin7 = analogRead(v7Pin); Vin8 = analogRead(v8Pin);

char buffer[10];

Serial.print("#S|THRMLOG|[");
tTest = millis() - tStart;
Serial.print(itoa((tTest), buffer, 10));
Serial.print(";");
Serial.print(itoa((Vin0), buffer, 10));
Serial.print(";");
Serial.print(itoa((Vin1), buffer, 10));
Serial.print(";");
Serial.print(itoa((Vin2), buffer, 10));
Serial.print(";");
Serial.print(itoa((Vin3), buffer, 10));
Serial.print(";");
Serial.print(itoa((Vin4), buffer, 10));
Serial.print(";");
Serial.println("]#");

delay(1000);
}
}

Here are the consecutive readouts approximately where things start to go wrong:

30255;901;898;903;901;902;
31257;901;898;903;902;902;
32258;901;898;903;903;902;
-32276;901;897;903;903;903;
-31274;902;898;903;901;902;
-30273;901;898;903;902;902;

It will loop all the way back to 31258 then go to -32276. Anyone know the reason this may be happening?

EDIT: I solved this by declaring tTest as a long rather than int. However, that is not the only thing I had to do, I also had to eliminate the itoa function, and I got lucky because for some reasonb gobetwino doesnt require the number to be converted to a string, although it says to in the user manual.

Let me guess... you've declared tStart and tTest as ints?

They need to be "unsigned long" or you'll just get (as you are) rubbish.

An int can only store between -32768 and 32767.

Here is my code:

sp. "Here is a fragment of my code from which I have omitted much useful information"

My money is on "int"s too, but posting all the code in the first place would have saved the guess work.

AWOL:

Here is my code:

sp. "Here is a fragment of my code from which I have omitted much useful information"

My money is on "int"s too, but posting all the code in the first place would have saved the guess work.

My bad for omitting the code I just didnt want to put a whole bunch of stuff for no reason and look like a newb, but obviosly that backfired haha. You are partially correct on the ints. But the itoa function maxes out at 16 bits as well.

But the itoa function maxes out at 16 bits as well

int to ASCII.
An int being a signed 16 bit number on the arduino.

But the itoa function maxes out at 16 bits as well.

There are, of course, equivalent functions for long to ASCII and ASCII to long. I'll bet you can even guess what they are.

michaud85:
My bad for omitting the code I just didnt want to put a whole bunch of stuff for no reason and look like a newb,

When posting code with a question, it is most helpful to post the minimum code that meets two criteria:

  1. Compiles as-is
  2. Demonstrates the problem

Funny enough, when you scale down to that amount, many times you find the problem before having to ask...