Go Down

Topic: Timing how long a variable is not zero (Read 658 times) previous topic - next topic

acrossell

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?

retrolefty

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


Lefty

acrossell

#2
Jun 20, 2011, 07:19 pm Last Edit: Jun 20, 2011, 07:23 pm by acrossell Reason: 1
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.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

acrossell

#4
Jun 20, 2011, 09:41 pm Last Edit: Jun 20, 2011, 09:44 pm by acrossell Reason: 1
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?

Code: [Select]
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;
 }

acrossell

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

acrossell

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

Go Up