Timing how long a variable is not zero

I have created a sketch which contain an array of 6 values that change based upon serial input, 0 is the default value for all the slices in the array. When a value in the array is not 0 I need to be able to count how long this is true and reset the value to 0 after a predetermined amount of time.

Does anyone know of a good jumping off point for me? I'm looking at the debounce tutorial to help me but I'm not quite there yet, any pointers you might be able to offer me?

I’m looking at the debounce tutorial

Debouncing has to do with dealing with physical switch contacts that ‘bounce’ and the problems that they can cause. What would that have to do with incoming serial characters and timing there values in your storage array?


Im using debounce to get a grip on how how to compare times as this is the first time I have dealt with time based functions in arduino. Thanks for the pointers ill see what i can do.

create an array of stopwatches ... - http://www.arduino.cc/playground/Code/StopWatchClass -

thanks rob ill have a look at that.

Ive tried this but dont have my board to hand to test it. Im only trying to change one of the index’s at the minute but am i steering in the right direction?

while (Serial.available() == 0)  {
    array1[0] = 0;
    array1[1] = 0;
    array1[2] = 0;
    array1[3] = 0;
    array1[4] = 0;
    array1[5] = 0; //all motors off if serial not available, prevents startup errors
  if (Serial.available() >= 6)  {
   for ( int i = 0; i < 6; i++)
   array1[i]= Serial.read() ;   //send serial values to array 1
  while (array1[0] != 0); {
   start = millis(); //take the start time of motor on array1[0]
  if ((millis() - start) > maxtime); {
   array1[0] = 0;

thanks thats great, but just for my own learning would my very basic attempt work? If not, why not?

Thanks for your help. Ive looked over your suggested code and I can see how that works. Thanks :)