Problem with negative unsigned long (millis() related or memory pb, or else?)

Hello,

First, I use Arduino 1.0 RC2 (that may be a big mistake... ). Using an Arduino Uno. Compiled, the sketch is 10544 bytes (of a 32256 byte maximum).

Plus I'm a beginner, that's my first Arduino project (actually, I've done maybe 10 smaller projects, that were in fact sub part of this project: testing sensor input, String use, LCD display, etc... all went well).

Roughly, I want to detect the time elapsed between two events.

Each event is detected thru an electronic detector connected to an Arduino analog input.

for detecting when an event happened, pseudo code is basically:

unsigned long eventDetectionTime = 0;
boolean eventHasNotBeenMeasured = true;

While (eventHasNotBeenMeasured) {

     if (analogRead(firstDetector) > threshold) {
          eventDetectionTime = millis();
          eventHasNotBeenMeasured = false;
     }
}

// then do whatever with eventDetectionTime

Problem is that I often end up with negative values of eventDetectionTime. Otherwise it works ok (it's not finished yet)

I was wondering why. At first I was using int to store millis(), which was bad (out of boundary errors)

So I switched to unsigned long

I still have a problem

Since I use Strings and char arrays for my Serial LCD display, I was wondering if I was not messing something here, either by bad use of pointer, or by overload of memory.

I checked the LCD display code, and it seemed ok. (but I may be wrong).

I checked the available memory at this moment of execution, and I got 1000 something (via the freeMemory function that you can find here:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15

I'm not sure if it's enough or not.

[EDIT: I removed the long part below (code), because it has no relevance with the problem, as it turned out, so without it the solution might be more easy to understand for someone else...]

[EDIT: I removed this post wich was the continuation of the post above (because I reached the size limit of a post), because the code here had no relevance with the problem, as it turned out, so without it the solution might be more easy to understand for someone else...]

Problem is that I often end up with negative values of eventDetectionTime

It isn't possible, if it is declared as "unsigned long".

It is, however, possible to misinterpret an "unsigned long" value as negative.

thanks for your answer,

I do agree with you, that's why I suspected a memory problem.

but: Or could it be that a String cannot be concatenated with an unsigned long?

Got it!!!!

for display I use a function like that:

void affiche(int line, String text, int value)

since I pass an unsigned long to the value parameter which is an int, here go troubles.....

Thanks a lot!!! I was searching for a while this one.