Tutorial.Stopwatch History

Hide minor edits - Show changes to markup

December 10, 2008, at 01:02 PM by David A. Mellis -
Changed lines 3-114 from:

A sketch that demonstrates how to do two (or more) things at once by using millis().


/* StopWatch
 * Paul Badger 2008
 * Demonstrates using millis(), pullup resistors, 
 * making two things happen at once, printing fractions
 *
 * Physical setup: momentary switch connected to pin 4, other side connected to ground
 * LED with series resistor between pin 13 and ground
 */


#define ledPin  13                  // LED connected to digital pin 13
#define buttonPin 4                 // button on pin 4

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
int blinking;                       // condition for blinking - timer is timing
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time



void setup()
{
   Serial.begin(9600);

   pinMode(ledPin, OUTPUT);         // sets the digital pin as output

   pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

}

void loop()
{
    // check for button press
   buttonState = digitalRead(buttonPin);                   // read the button state and store

   if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false){     // check for a high to low transition
      // if true then found a new button press while clock is not running - start the clock

      startTime = millis();                                   // store the start time
      blinking = true;                                     // turn on blinking while timing
      delay(5);                                               // short delay to debounce switch
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

   }

   else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){     // check for a high to low transition
      // if true then found a new button press while clock is running - stop the clock and report

        elapsedTime =   millis() - startTime;              // store elapsed time
        blinking = false;                                  // turn off blinking, all done timing
        lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

       // routine to report elapsed time 
        Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

        Serial.print(".");                             // print decimal point

        // use modulo operator to get fractional part of time 
       fractional = (int)(elapsedTime % 1000L);

       // pad in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this? :)
       if (fractional == 0){
          Serial.print("000");      // add three zero's
       else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00");       // add two zeros
       else if (fractional < 100)
          Serial.print("0");        // add one zero
       }
       Serial.println(fractional);  // print fractional part of time 

   }

   else{
      lastButtonState = buttonState;                         // store buttonState in lastButtonState, to compare next time
   }

   // blink routine - blink the LED while timing
   // check to see if it's time to blink the LED; that is, the difference
   // between the current time and last time we blinked the LED is larger than
   // the interval at which we want to blink the LED.

   if ( (millis() - previousMillis > interval) ) {

      if (blinking == true){
         previousMillis = millis();                         // remember the last time we blinked the LED

         // if the LED is off turn it on and vice-versa.
         if (value == LOW)
            value = HIGH;
         else
            value = LOW;
         digitalWrite(ledPin, value);
      }
      else{
         digitalWrite(ledPin, LOW);                         // turn off LED when not blinking
      }
   }

}

to:

See: http://www.arduino.cc/playground/Code/Stopwatch

October 09, 2008, at 04:44 PM by Paul Badger -
Changed lines 92-93 from:
   // check to see if it's time to blink the LED; that is, is the difference
   // between the current time and last time we blinked the LED bigger than
to:
   // check to see if it's time to blink the LED; that is, the difference
   // between the current time and last time we blinked the LED is larger than
October 09, 2008, at 04:43 PM by Paul Badger -
Changed line 66 from:
      // routine to report elapsed time 
to:
       // routine to report elapsed time 
October 09, 2008, at 04:42 PM by Paul Badger -
Changed lines 69-70 from:
        Serial.print(".");                                       // print decimal point
to:
        Serial.print(".");                             // print decimal point
October 09, 2008, at 04:41 PM by Paul Badger -
Changed lines 47-48 from:
   buttonState = digitalRead(buttonPin);                 // read the button state and store
to:
   buttonState = digitalRead(buttonPin);                   // read the button state and store
Changed lines 62-65 from:
        elapsedTime =   millis() - startTime;            // store elapsed time
        blinking = false;                                // turn off blinking, all done timing
        lastButtonState = buttonState;                   // store buttonState in lastButtonState, to compare next time
to:
        elapsedTime =   millis() - startTime;              // store elapsed time
        blinking = false;                                  // turn off blinking, all done timing
        lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time
Changed lines 69-70 from:
        Serial.print(".");                                  // print decimal point
to:
        Serial.print(".");                                       // print decimal point
October 09, 2008, at 04:40 PM by Paul Badger -
Changed line 62 from:
        elapsedTime =   millis() - startTime;                // store elapsed time
to:
        elapsedTime =   millis() - startTime;            // store elapsed time
Changed line 66 from:
        // routine to report elapsed time 
to:
      // routine to report elapsed time 
October 09, 2008, at 04:39 PM by Paul Badger -
Changed lines 63-65 from:
        blinking = false;                                       // turn off blinking, all done timing
        lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time
to:
        blinking = false;                                // turn off blinking, all done timing
        lastButtonState = buttonState;                   // store buttonState in lastButtonState, to compare next time
October 09, 2008, at 04:36 PM by Paul Badger -
Changed lines 62-72 from:
      elapsedTime =   millis() - startTime;                // store elapsed time
      blinking = false;                                       // turn off blinking, all done timing
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

       // routine to report elapsed time 
       Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

       Serial.print(".");                                  // print decimal point
to:
        elapsedTime =   millis() - startTime;                // store elapsed time
        blinking = false;                                       // turn off blinking, all done timing
        lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

        // routine to report elapsed time 
        Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

        Serial.print(".");                                  // print decimal point
October 09, 2008, at 04:35 PM by Paul Badger -
Changed lines 70-72 from:
      Serial.print(".");                                  // print decimal point
to:
       Serial.print(".");                                  // print decimal point
Added line 75:
October 09, 2008, at 04:34 PM by Paul Badger -
Deleted lines 70-71:
      fractional = (int)(elapsedTime % 1000L);  
October 09, 2008, at 04:31 PM by Paul Badger -
Changed lines 75-76 from:
       // padd in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this :)
to:
       // pad in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this? :)
Changed lines 78-81 from:
          Serial.print("000"); // add three zero's

       else if (fractional < 10)        // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00"); // add zero
to:
          Serial.print("000");      // add three zero's
       else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00");       // add two zeros
Changed line 82 from:
          Serial.print("0"); // add another one
to:
          Serial.print("0");        // add one zero
October 09, 2008, at 04:28 PM by Paul Badger -
Changed line 70 from:
      Serial.print(".");                                     // print decimal point
to:
      Serial.print(".");                                  // print decimal point
Changed lines 74-80 from:
       fractional = (int)(elapsedTime % 1000L); 
       if (fractional < 100)        // if fractional < 100 the 0 is ignored giving a wrong time, so add the zero
       Serial.print("0"); // add zero
       if (fractional < 10){
          Serial.print("0"); // and another one
       else if (fractional == 0)
          Serial.print("00"); // and two for three total
to:
       fractional = (int)(elapsedTime % 1000L);
       // padd in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this :)
       if (fractional == 0){
          Serial.print("000"); // add three zero's

       else if (fractional < 10)        // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00"); // add zero
       else if (fractional < 100)
          Serial.print("0"); // add another one
October 08, 2008, at 08:17 AM by Paul Badger -
Changed line 53 from:
      blinking = true;                                        // turn on blinking while timing
to:
      blinking = true;                                     // turn on blinking while timing
October 08, 2008, at 08:16 AM by Paul Badger -
Changed lines 68-70 from:
         // routine to report elapsed time - this breaks when delays are in single or double digits. Fix this as a coding exercise.

         Serial.print( (int)(elapsedTime / 1000L) );         // divide by 1000 to convert to seconds - then cast to an int to print
to:
       // routine to report elapsed time 
       Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print
Changed line 73 from:
         // use modulo operator to get fractional part of time 
to:
        // use modulo operator to get fractional part of time 
Changed lines 82-83 from:

Serial.println(fractional); // print fractional part of time

to:
       Serial.println(fractional);  // print fractional part of time 
October 08, 2008, at 08:13 AM by Paul Badger -
Changed lines 74-83 from:
             // use modulo operator to get fractional part of time 

fractional = (int)(elapsedTime % 1000L); if (fractional < 100) // if fractional < 100 the 0 is ignored giving a wrong time, so add the zero {

  Serial.print("0"); // add zero
  if (fractional < 10)
    Serial.print("0"); // and another one
  else if (fractional == 0)
    Serial.print("00"); // and two for three total

}

to:
         // use modulo operator to get fractional part of time 
       fractional = (int)(elapsedTime % 1000L); 
       if (fractional < 100)        // if fractional < 100 the 0 is ignored giving a wrong time, so add the zero
       Serial.print("0"); // add zero
       if (fractional < 10){
          Serial.print("0"); // and another one
       else if (fractional == 0)
          Serial.print("00"); // and two for three total
       }
October 08, 2008, at 08:09 AM by Paul Badger -
Changed lines 72-74 from:
      fractional = (int)(elapsedTime % 1000L);               // use modulo operator to get fractional part of time
      Serial.println(fractional);                            // print fractional part of time
to:
      fractional = (int)(elapsedTime % 1000L);  

             // use modulo operator to get fractional part of time 

fractional = (int)(elapsedTime % 1000L); if (fractional < 100) // if fractional < 100 the 0 is ignored giving a wrong time, so add the zero {

  Serial.print("0"); // add zero
  if (fractional < 10)
    Serial.print("0"); // and another one
  else if (fractional == 0)
    Serial.print("00"); // and two for three total

} Serial.println(fractional); // print fractional part of time

April 22, 2008, at 04:49 AM by Paul Badger -
Changed lines 47-48 from:
   buttonState = digitalRead(buttonPin);                         // read the button state and store
to:
   buttonState = digitalRead(buttonPin);                 // read the button state and store
April 22, 2008, at 04:48 AM by Paul Badger -
Changed lines 46-48 from:
   // here is where you'd put code that needs to be running all the time.

   // check for button press
to:
    // check for button press
April 22, 2008, at 04:47 AM by Paul Badger -
Changed line 64 from:
      elapsedTime =   (millis() - startTime) - 5;             // store elapsed time (-5 to make up for switch debounce time)
to:
      elapsedTime =   millis() - startTime;                // store elapsed time
April 22, 2008, at 04:45 AM by Paul Badger -
Changed lines 70-71 from:
         // routine to report elapsed time
to:
         // routine to report elapsed time - this breaks when delays are in single or double digits. Fix this as a coding exercise.
April 18, 2008, at 02:25 PM by Paul Badger -
Changed lines 10-11 from:
 * Demonstrates using millis(), pullup resistors, making two things happen at once, printing fractions
to:
 * Demonstrates using millis(), pullup resistors, 
 * making two things happen at once, printing fractions
April 18, 2008, at 02:24 PM by Paul Badger -
Added lines 1-105:

Stopwatch

A sketch that demonstrates how to do two (or more) things at once by using millis().


/* StopWatch
 * Paul Badger 2008
 * Demonstrates using millis(), pullup resistors, making two things happen at once, printing fractions
 *
 * Physical setup: momentary switch connected to pin 4, other side connected to ground
 * LED with series resistor between pin 13 and ground
 */


#define ledPin  13                  // LED connected to digital pin 13
#define buttonPin 4                 // button on pin 4

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
int blinking;                       // condition for blinking - timer is timing
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time



void setup()
{
   Serial.begin(9600);

   pinMode(ledPin, OUTPUT);         // sets the digital pin as output

   pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.

}

void loop()
{
   // here is where you'd put code that needs to be running all the time.

   // check for button press
   buttonState = digitalRead(buttonPin);                         // read the button state and store

   if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false){     // check for a high to low transition
      // if true then found a new button press while clock is not running - start the clock

      startTime = millis();                                   // store the start time
      blinking = true;                                        // turn on blinking while timing
      delay(5);                                               // short delay to debounce switch
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

   }

   else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){     // check for a high to low transition
      // if true then found a new button press while clock is running - stop the clock and report

      elapsedTime =   (millis() - startTime) - 5;             // store elapsed time (-5 to make up for switch debounce time)
      blinking = false;                                       // turn off blinking, all done timing
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time



         // routine to report elapsed time

         Serial.print( (int)(elapsedTime / 1000L) );         // divide by 1000 to convert to seconds - then cast to an int to print
      Serial.print(".");                                     // print decimal point
      fractional = (int)(elapsedTime % 1000L);               // use modulo operator to get fractional part of time
      Serial.println(fractional);                            // print fractional part of time

   }

   else{
      lastButtonState = buttonState;                         // store buttonState in lastButtonState, to compare next time
   }

   // blink routine - blink the LED while timing
   // check to see if it's time to blink the LED; that is, is the difference
   // between the current time and last time we blinked the LED bigger than
   // the interval at which we want to blink the LED.

   if ( (millis() - previousMillis > interval) ) {

      if (blinking == true){
         previousMillis = millis();                         // remember the last time we blinked the LED

         // if the LED is off turn it on and vice-versa.
         if (value == LOW)
            value = HIGH;
         else
            value = LOW;
         digitalWrite(ledPin, value);
      }
      else{
         digitalWrite(ledPin, LOW);                         // turn off LED when not blinking
      }
   }

}

Share