A Simple, Practical Arduino Stopwatch

Published

Introduction: A Simple, Practical Arduino Stopwatch

Search the web for Arduino stopwatch. You probably just did it, if you're here. From personal experience, I can tell you that any stopwatch on the internet is either way too complex (in code, for beginners), or is way too simplified, and not practical, like the ones that just keep counting seconds without resetting.

You think so too, eh? Well guess what. I did it, and what's more, I directly take my input from the millis() function- an endless line of numbers (until 49 days, or whatever). The best part? It's only one line.

Resetting, is the actual hard part of the stopwatch. For intermediate, programmers, if you haven't already, go ahead and try to do this yourself, with seconds, minutes, and hours together. You'll get what I'm talking about.

For beginners, read on, and revel in the genius of my mind.

Just kidding, but the solution I came up with is pretty neat. Here's what it is:

Step 1: The Solution

So the problem is resetting. What we want is a continuous, repeating cycle of numbers, from an endless output of sequential numbers (millis() - it basically counts milliseconds, or actually 1.024 milliseconds, but whatever).

The first thing that comes to mind is using difference, like time1 -time2 and a delay. Forget that. It takes at least six lines of code, and an if statement to boot.

So here's how to do it. We have a limit (59). We want everything from 0 to 59 to repeat over and over. How?

What if... we took the remainder... of the millis function divided by 59.... Bingo!

So, it should be like so:

(60[as the initial value because the limit is 59]+ millis() / 1000[1000 milliseconds is a second] ) % 60

Ok, lemme explain. (%) or modulus, is basically an operator like (+) that finds the remainder. Like 9 % 2 = 1.

So:

  • remainder of (60 + 0) % 60 = 0
  • remainder of (60 + 1) % 60 =1

  • remainder of (60 + 58) % 60 = 58

  • remainder of (60 + 59) % 60 = 59

  • remainder of (60 + 60) % 60 = 60

  • remainder of (60 + 61) % 60 = 1

See!

Now, for the parts.

Step 2: The Parts

Not much, at all.

  1. Any arduino
  2. Any display (I used the 0.96" OLED, but you can use whatever you have, just make sure to put the right variables into the display functions of your display.)

That's it.

Step 3: The Code

Here ya go. It's pretty clearly commented, so there should be no problems. The libraries and init for 0.96" OLED is in the code. Replace it with your own display's if it it's different.

// 0.96" OLED libraries

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>


// 0.96" OLED Init

#define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET);

int seconds;

int minutes;

int hours;

void setup() {

// Some more 0.96" OLED Init

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); }

void loop() {

seconds = (60 + millis() / 1000) % 60; minutes = (60 + millis() / 60000) % 60; hours = (25 + millis() / 3600000) % 25;

display.clearDisplay(); display.setCursor(0, 22); // print the number of hours since reset display.print(hours); display.print("h:");

// print the number of miutes since reset

display.print(minutes);

display.print("m:");

// print the number of seconds since reset display.print(seconds); display.print("s");

display.setCursor(0, 0); display.print("Stopwatch"); display.drawLine(0,18,128,18, WHITE); display.display();

}

Step 4: En Finalment...

So that's it! Now go do what you want with it. Time some eggs, or take over your neighborhood.

Cheers, Aarush

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Casting Contest

      Casting Contest
    • Woodworking Contest

      Woodworking Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments