# Mindfulness Clock OF DOOM

## Overview

https://www.youtube.com/watch?v=FHrBcGeEwwQ

![](https://cdn-learn.adafruit.com/assets/assets/000/034/554/medium800/arduino_graveYard-faceplate-red.jpg?1470245448)

It’s not pleasant&nbsp;thinking about one’s mortality, but that’s the point of this clock design: perspective. To make one&nbsp;aware of the passage of time and **how precious each minute is**.

Use an **online calculator** (some links below) to gauge&nbsp;your **life expectancy** , then plug that date&nbsp;into our code for this clock project. You’ll get a running count of how many minutes you have left. Or you can pick&nbsp;another event with a date&nbsp;of your choosing…the return of Halley’s comet (28 July 2061), a predicted&nbsp;date for the [technological singularity](https://en.wikipedia.org/wiki/Technological_singularity)&nbsp;(2045 according to Ray Kurzweil), a favorite future moment from science fiction, you name it.

As&nbsp;you look at the clock, remember&nbsp;that&nbsp;you&nbsp;are _never_ getting those minutes back. Stop watching internet cat videos and&nbsp; **make the most of them!**

Here’s a few links to life expectancy calculators&nbsp;that can provide a final date. If&nbsp;one’s not working (or you don’t like their prediction), try the next:

- [**The Death Clock**](https://www.death-clock.org)
- [**Death Timer**](http://www.deathtimer.com)
- [**Find Your Fate Death Clock**](http://www.findyourfate.com/deathmeter/deathmtr.html)

![](https://cdn-learn.adafruit.com/assets/assets/000/034/198/medium800/arduino_cicruit-parts.jpg?1469399333)

# Parts and Tools

- **[Adafruit Pro Trinket](https://www.adafruit.com/products/2000)**, either the **[5V](https://www.adafruit.com/products/2000)**&nbsp;or **[3V](https://www.adafruit.com/products/2010)**&nbsp;version will work, whichever is&nbsp;in stock&nbsp;(with your own enclosure, the code can also work with most ATmega 328P-based microcontrollers such as an Arduino Uno…but NOT&nbsp;32u4-based boards like the Arduino Leonardo or Adafruit Feather 32u4, nor ATtiny-based boards. The code relies on hardware-specific features.)
- [**DS3231 Precision RTC&nbsp;Breakout**](https://www.adafruit.com/products/3013)&nbsp;(or other **DS3231** realtime clock breakout board, such as&nbsp;the&nbsp;[MaceTech ChronoDot](https://www.adafruit.com/products/255)&nbsp;— again, will need your own enclosure)
- [**CR1220 Lithium coin&nbsp;cell battery**](https://www.adafruit.com/products/380) for RTC. If you can pick this up locally at a nearby drugstore or Radio Shack, do that! This may&nbsp;slow down your shipment otherwise, due to safety regulations with batteries.
- **2** (two) [**0.56" 4-digit 7-segment LED&nbsp;displays with I2C backpack**](https://www.adafruit.com/products/878) (any color — mix or match!)
- [**USB power supply**](https://www.adafruit.com/products/1995) (or use an old MicroUSB phone charger if you’ve got one)
- **Optional: [Perma-Proto board](https://www.adafruit.com/product/1608)**&nbsp;for splitting&nbsp;connections.
- **3D printer** and **filament** …or **design your own** enclosure with materials and tools that best suit your abilities.
- [M2 x .4 x 4mm Phillips Flat Head Machine Screws](http://www.albanycountyfasteners.com/2MM-x-4-Phillips-Flat-Head-Machine-Screw-p/1011-1000.htm?1=1&CartID=0)

You will also need a **soldering iron** and related&nbsp;paraphernalia, some wire, etc. **Read through the whole guide** before planning any acquisitions.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/559/medium800thumb/arduino_timeLoopGY-RED.jpg?1470246229)

# Realistic Expectations
This is an **art piece** and should not be taken too seriously.&nbsp;Any longevity calculation is loosely based on statistical probabilities; reality will vary, and _we really hope you’ll long outlive your clock!_

Of course, unforseen events could happen too. Giant meteor, Cthulu may call, or Drillcat…no clock can predict these surprises.

![arduino_drillcat.jpg](https://cdn-learn.adafruit.com/assets/assets/000/033/687/medium640/arduino_drillcat.jpg?1468028828)

# Mindfulness Clock OF DOOM

## 3D Printing

![](https://cdn-learn.adafruit.com/assets/assets/000/034/312/medium800/arduino_3d-parts-screws.jpg?1469628400)

Let’s start with the printing first. You can work on the electronics and software&nbsp;while the printer runs the job.

If doing your own custom build — for example, if using different components&nbsp;— skip ahead to the next page.&nbsp; **The 3D-printed parts are specifically designed around the Pro Trinket&nbsp;board&nbsp;and components listed&nbsp;previously.**

[Download 3D files from Thingiverse](http://www.thingiverse.com/thing:1699265)
## Customize The Design

The&nbsp;parts where modeled&nbsp;in Autodesk Fusion 360, so it's easy to modify the design. The design is&nbsp;public and available to download in different formats. If&nbsp;you'd like to use a different CAD software package, you are free to import the files and remix them.

[Edit Design](http://a360.co/2auBmBO)
## Slicer&nbsp;Settings

To slice the parts, we used Simplify3D. We recommend using&nbsp;the settings below or use them as reference. We 3D printed the&nbsp;part on a BCN3D Sigma&nbsp;with a 6.mm nozzle. If you have Simplify3D, you can download our profiles below.

[Printer Profiles](https://github.com/adafruit/Printer-Profiles)
## Filament Materials

We recommend using PLA material to reduce wrapping while 3D printing. The parts&nbsp;can be printed in different types of filament, such as ABS, PET or&nbsp;Nylon.

**PLA** &nbsp;filament is easiest to work with. If printing in ABS instead, you might need to scale the model up slightly (about 2%) to allow for shrinkage.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/254/medium800/arduino_screws.jpg?1469477157)

## Threads and screws

We&nbsp;used [M2 x .4 x 4mm screws](http://www.albanycountyfasteners.com/2MM-x-4-Phillips-Flat-Head-Machine-Screw-p/1011-1000.htm?1=1&CartID=0) for the Trinket Pro, RTC and display boards.

Use a screw (or a _tapping_&nbsp;tool, if you have one) to create the threads for each&nbsp;standoff&nbsp;before mounting the boards. Make sure to apply a bit of pressure and to keep the screw straight while turining.&nbsp;

# Mindfulness Clock OF DOOM

## Circuit

In schematic-like form, here’s what we’re aiming for:

![](https://cdn-learn.adafruit.com/assets/assets/000/034/341/medium800/arduino_diagram4.jpg?1469724371)

It’s basically just&nbsp;four connections in common between all four boards, plus one extra wire between the Pro Trinket and RTC:

![](https://cdn-learn.adafruit.com/assets/assets/000/034/306/medium800/arduino_screws-mounts.jpg?1469587572)

When making connections to multiple points like this, you may find it helpful to use a small piece of **[Perma-Proto](https://www.adafruit.com/categories/466)**&nbsp;board to join one end of all the wires. Perma-Proto board can be cut with tin snips or a scroll saw; you don’t need the whole board in there. Save the rest for future projects!

Make sure to use the BUS pin on the Pro Trinket, _not_&nbsp;the 5V or 3V pin; the LED displays are a bit much for the board’s voltage regulator, so everything’s powered off the USB connection instead.

**[Follow these instructions](../../../../adafruit-led-backpack/0-54-alphanumeric?view=all#0-dot-56-seven-segment-backpack)&nbsp;for guidance on assembling the LED backpacks, making sure the decimal points are properly aligned and the display is on the correct side of the board.** Just don’t add the 4-pin breadboard header, we’ll be wiring up directly.

**[Bridge the “A0” solder pads on _one_ of the two LED backpacks](../../../../adafruit-led-backpack/0-54-alphanumeric?view=all#changing-i2c-address)**. This will be the “low” four digits, while the un-bridged one will be the “high” digita.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/200/medium800/arduino_align-dec.jpg?1469399405)

## Align display

Note where the&nbsp;decimal points are and properly align the display to the points on&nbsp;the correct side of the board.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/202/medium800/arduino_display-tape-backpack.jpg?1469402614)

## Prep Display

Use tape to hold the displays in place before soldering to the backpacks. Make sure the displays are&nbsp;leveled against the backpack and then apply small strips of tape to both sides.

![](https://cdn-learn.adafruit.com/assets/assets/000/034/203/medium800/arduino_solder-display.jpg?1469402699)

## Solder&nbsp;display

Solder all of the pins to the backpack. A panavise is super helpful and will allow you angle the boards towards you to easily solder&nbsp;each pin.

## Trim&nbsp;pins

Once all of the pins are soldered, we'll need to cut the excess off each pin off to fit the board inside the enclosure.

Grip each pin with a plier and then use flush cutters to remove each pin.&nbsp;This will prevent the pins from flying everywhere when cutting.

![arduino_display-pin-cut.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/205/medium640/arduino_display-pin-cut.jpg?1469402905)

![arduino_display-pin-cut2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/206/medium640/arduino_display-pin-cut2.jpg?1469402932)

![](https://cdn-learn.adafruit.com/assets/assets/000/034/207/medium800/arduino_wire-measure.jpg?1469403059)

## Measure wires

Lay&nbsp;out the boards on the&nbsp;enclosure and lid parts so we have a good idea of how long we need each wire to be.&nbsp;Measure the wires with enough slack so&nbsp;we&nbsp;can&nbsp;easy access the boards&nbsp;when we need to change the battery.

## Flex Perma-Proto

&nbsp;

We can use a flex perma-proto to create a splitter to join wires together. Cut a piece with&nbsp;three connected through holes (the ground or power rails) to build a "T" shaped wire. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp;

## High Temperature Polyimide Tape

&nbsp;

Insulate the connections with a piece of Kapton tape (Polyimide Tape) to prevent any connections from touching.

&nbsp;

## Solder Displays

&nbsp;

After all of the splitters are built,&nbsp;solder each wire as shown in the circuit diagram.

![arduino_perma-split-soldered.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/210/medium640/arduino_perma-split-soldered.jpg?1469407326)

![arduino_perma-split-taped.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/211/medium640/arduino_perma-split-taped.jpg?1469407351)

![arduino_perma-split-taped2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/212/medium640/arduino_perma-split-taped2.jpg?1469407379)

![arduino_displays-soldreded.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/213/medium640/arduino_displays-soldreded.jpg?1469407641)

## Solder Trinket and Coin Cell Breakout
## Solder&nbsp;breakouts

&nbsp;

Solder the breakout boards to the the rest of the circuit. You can use tweezers to hold wires&nbsp;while&nbsp;soldering&nbsp;to&nbsp;each through hole.

&nbsp;

## Coin cell

The RTC breakout board uses a [12mm&nbsp;CR1220 coin cell battery](https://www.adafruit.com/products/380). Note the positive side on the Coin Cell Retainer&nbsp;and insert the battery.&nbsp;

&nbsp;

## Bridge A0

&nbsp;

[Bridge the “A0” solder pads on the “low” four digit display](../../../../adafruit-led-backpack/0-54-alphanumeric?view=all#changing-i2c-address), the un-bridged one will be the “high” digits.

![arduino_circuit-soldered.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/216/medium640/arduino_circuit-soldered.jpg?1469408080)

![arduino_coin-cell-insert.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/217/medium640/arduino_coin-cell-insert.jpg?1469408112)

![arduino_display-A0-bridgeB.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/218/medium640/arduino_display-A0-bridgeB.jpg?1469408172)

## Mount display

&nbsp;

Align the displays to the standoffs on the enclosure box and the breakout boards to&nbsp;the lid part. Use M2 x .4 x 4mm machine screws to mount all of the displays and boards.&nbsp;

## Close case

&nbsp;

The lid part snap fits into the box enclosure. Align the USB cutout on the lid with the port opening on enclosure box to press fit together.

![arduino_display-mount.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/221/medium640/arduino_display-mount.jpg?1469408332)

![arduino_close-case.jpg](https://cdn-learn.adafruit.com/assets/assets/000/034/222/medium640/arduino_close-case.jpg?1469408380)

![](https://cdn-learn.adafruit.com/assets/assets/000/034/556/medium800/arduino_clock-hero2-red.jpg?1470245495)

![](https://cdn-learn.adafruit.com/assets/assets/000/034/557/medium800/arduino_graveYard-faceplate-flat-red.jpg?1470246191)

For power, the clock connects to any USB power supply.

Now&nbsp;you're ready to add code!

# Mindfulness Clock OF DOOM

## Software Part 1

If this is your first time using the Pro Trinket microcontroller, you’ll want to begin with&nbsp;our guide for setting that up. The Pro Trinket works a little differently from “normal” Arduinos are requires some extra installation&nbsp;and a different upload procedure:

## **[Introducing Pro Trinket](../../../../introducing-pro-trinket/overview)**

To confirm that you have the driver installed and IDE properly configured, load the basic Arduino “blink” example sketch&nbsp;and&nbsp;try uploading&nbsp;to the board. If it won’t cooperate, work carefully through each of the steps in the guide linked above.

Danger: 

Using the realtime clock and LED displays requires&nbsp;a few extra libraries. These can be installed using the Arduino IDE’s Library Manager: **Sketch→Include Library→Manage Libraries…**

Scroll down or use the search field to install **RTClib** , **Adafruit\_GFX** , **Adafruit\_BusIO** , and **Adafruit\_LEDBackpack**.

The code below will set the initial time on the DS3231 realtime clock. You only need to run this once, or a few years down the line when replacing the clock’s backup battery. Copy and paste the&nbsp;code into a new Arduino sketch and upload to the Pro Trinket.

A SOLID RED status LED on the Pro Trinket means the realtime clock was found and time was set. Off or blinking indicates a problem (troubleshooting tips below).

```
// Clock-setting utility for "Mindfulness Clock."  Sets DS3231 battery-
// backed RTC time based on compilation time or a specific data/time value.
// Only needs to be run once to set the clock, or when battery is replaced.

#include &lt;Wire.h&gt;
#include &lt;RTClib.h&gt;

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  if(rtc.begin()) {
    // This line sets RTC to date &amp; time this sketch was compiled:
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // Or this line sets the RTC with an explicit date &amp; time,
    // for example to set January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    Serial.println(F("RTC time set"));
    digitalWrite(LED_BUILTIN, HIGH); // LED steady on = RTC set
  } else {
    Serial.println(F("Couldn't find RTC"));
    for(boolean b; ; b = !b) {       // Blinking = RTC error
      digitalWrite(LED_BUILTIN, b);
      delay(250);
    }
  }
}

void loop() { } // Not used -- it's all in setup()!
```

### 

- Confirm the correct board is selected (“Pro Trinket 5V/16MHz (USB)” or “Pro Trinket 3V/12MHz (USB)”, whichever you’re using).
- Confirm the&nbsp;RTClib library is correctly installed.

### 

- This usually indicates a communication problem between the Pro Trinket and the RTC. Check all four connections: BUS, GND, A4/SDA/D and A5/SCL/C, make sure the latter two aren’t reversed.
- Look the circuit over for accidental solder bridges or cold solder joints.
- Is the coin cell battery installed on the RTC board?

### 

Success! That means the clock has been set. Proceed to the next page to load the actual countdown software.

Danger: 

# Mindfulness Clock OF DOOM

## Software Part 2

Now you can copy and paste all the code (further down this page)&nbsp;into a new sketch. Before uploading to the board, you’ll want to edit this section near the top (around line 38):

```
#define YEAR  2061 // Date and time of some future event
#define MONTH    7 // (ostensibly your demise)
#define DAY     28 // But here it's set for Halley's comet
#define HOUR    12 // Example time here is set for noon,
#define MINUTE   0 // DON'T use leading zero or value is octal!
```

This sets the date and time of some future event to count down toward.

**HOUR** should be in **24-hour** format; e.g. midnight is “0”, noon is “12”, while&nbsp;3pm is “15” (12+3). **MINUTE** is **0 to 59**. For both of these values, do not&nbsp;use a leading zero; e.g. for 9:09 am, use “9” for each, not “09”. (Numbers with leading zeros are interpreted by the computer as _octal_ — a different representation, like hexadecimal or binary.)

After uploading this sketch to the Pro Trinket, you should be greeted with a (hopefully very large) number — the minutes until your set date — and the **rightmost decimal** will be **blinking**. One second on, one second off. If not, or if having other problems, see the troubleshooting tips at the bottom of this page.

```
// Mindfulness clock.  Counts down minutes to some event; presumably one's
// demise.  The display is purposefully not divided into years and days and
// so forth, just an absolute number of minutes, counting down.  It's a
// mindfulness thing; makes you think.  Minutes you're never getting back.
// STOP WATCHING INTERNET CAT VIDEOS AND GET SOMETHING DONE!

// GLOBAL STUFF ------------------------------------------------------------

// PARTS USED:
// - Adafruit Pro Trinket microcontroller, the 5V or 3V versions work
//   equally well, whichever is in stock (adafru.it/2000 or 2010).  Other
//   ATmega 328P-based microcontrollers may work (e.g. Arduino Uno), but
//   NOT 32u4-based boards like the Feather 32u4 or Arduino Leonardo,
//   nor ATtiny-based boards.  Code uses specific hardware features.
// - Adafruit DS3231 Precision RTC Breakout (http://adafru.it/3013).  Other
//   DS3231-based models like the ChronoDot (255) can also work.
// - CR1220 lithium battery (or whatever battery your RTC requires)
// - TWO 4-digit, 7-segment LED Backpacks (adafru.it/877) with LED displays
//   in your choice of colors (red is always very dramatic!)

// NOTES:
// - The two 7-segment displays will be placed side-by-side to create a
//   contiguous 8-digit display.  Bridge the 'A0' pads on the back of ONE
//   display to change its I2C address.
// - Install the LED displays correctly: display goes on the BLANK side of
//   the backpack board and does NOT cover the driver chip.  Make sure the
//   DECIMAL POINTS match the positions on the silkscreen, or it won't work.
// - Connect 'SQW' pin on the RTC board to Digital Pin 3 on microcontroller.
// - The DS3231 dates are valid through year 2100, this code through 2099.
//   Behavior is undefined past those dates and probably won't work, but
//   call it a feature...you and your Arduino have survived over 80 years.

#include &lt;Wire.h&gt;
#include &lt;RTClib.h&gt;
#include &lt;Adafruit_GFX.h&gt;
#include &lt;Adafruit_LEDBackpack.h&gt;

#define YEAR  2061 // Date and time of some future event
#define MONTH    7 // (ostensibly your demise)
#define DAY     28 // But here it's set for Halley's comet
#define HOUR    12 // Example time here is set for noon,
#define MINUTE   0 // DON'T use leading zero or value is octal!

// GLOBAL STUFF ------------------------------------------------------------

RTC_DS3231 rtc;

struct {
  uint8_t           addr;         // I2C address
  Adafruit_7segment seg7;         // 7segment object
} disp[] = {
  { 0x70, Adafruit_7segment() },  // High digits
  { 0x71, Adafruit_7segment() }   // Low digits
};

// These are declared 'volatile' because they change in an interrupt handler
volatile uint32_t minutes = 0;    // Time remaining (counts down)
volatile uint8_t  seconds = 0;    // Next-minute counter (counts up)
volatile boolean  hzFlag  = true; // true = refresh display

// UTILITY FUNCTIONS -------------------------------------------------------

// This function is called when a high-to-low transition is detected on
// digital pin 3 -- this is connected to the SQW pin on the RTC, which
// provides a super-precise 1-second square wave; won't drift the way
// delay() or millis() would (could even sleep MCU if desired).
// The displays are NOT refreshed inside this function -- it's poor form
// to try doing too much inside an interrupt routine, especially when the
// functions used in updating the displays (via the Wire library) may
// themselves rely on other interrupts.  Instead, a global flag is set here
// and the loop() routine polls this &amp; updates the display when ready.
void interrupt1hz(void) {
  if(++seconds &gt;= 60) {
    seconds = 0;
    if(minutes) minutes--;
  }
  hzFlag = true;
}

// Number of days since 2000/01/01, valid for 2000-2099.  Essentially a
// copy of the date2days() function in RTClib, but it's been declared static
// there and is inaccessible through the API.  So, a duplicate...
uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
  static const uint8_t daysInMonth[] PROGMEM =
    { 31,28,31,30,31,30,31,31,30,31,30,31 };
  if(y &gt;= 2000) y -= 2000;
  uint16_t days = d;
  for(uint8_t i=1; i&lt;m; ++i) days += pgm_read_byte(daysInMonth + i - 1);
  if((m &gt; 2) &amp;&amp; (!(y &amp; 3))) days++;        // Leap day for current year
  return days + 365 * y + (y + 3) / 4 - 1; // Add leap days for prior years
}

// SETUP() FUNCTION - RUNS ONCE AT STARTUP ---------------------------------

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);   // LED on = initializing

  for(uint8_t i=0; i&lt;2; i++) {       // Initialize displays
    disp[i].seg7.begin(disp[i].addr);
    disp[i].seg7.clear();
    disp[i].seg7.writeDisplay();
  }

  if(!rtc.begin()) {                 // Initialize RTC
    Serial.println("Couldn't find RTC");
    for(boolean b; ; b = !b) {       // Blinking = RTC error
      digitalWrite(LED_BUILTIN, b);
      delay(250);
    }
  }

  // Determine number of minutes until YEAR/MONTH/DAY/etc.
  DateTime now = rtc.now();
  uint32_t d1  = date2days(YEAR, MONTH, DAY),                   // End day
           d2  = date2days(now.year(), now.month(), now.day()); // Today
  if(d2 &gt; d1) { // Date already passed
    minutes = 0;
  } else {
    int32_t s1 = (HOUR * 60 + MINUTE) * 60L,
            s2 = (now.hour() * 60 + now.minute()) * 60L + now.second();
    if((d2 == d1) &amp;&amp; (s2 &gt;= s1)) { // Same day, hour/min/sec has passed
      minutes = 0;
    } else {
      s1     -= s2; // Seconds time difference, may be negative, this is OK
      seconds = 60 - (s1 % 60); // Counts up 0-60 for next minute mark
      minutes = (d1 - d2) * 24 * 60 + s1 / 60; // Minutes remaining
    }
  }

  // Enable 1 Hz square wave output from RTC.
  // rtc.writeSqwPinMode((Ds3231SqwPinMode)SquareWave1HZ); *should* handle
  // this, but for whatever reason is not, so fiddle registers directly...
  uint8_t ctrl;
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write((byte)DS3231_CONTROL);
  Wire.endTransmission();
  Wire.requestFrom(DS3231_ADDRESS, (byte)1);
  ctrl = Wire.read() &amp; 0b11100011; // 1 Hz, INTCN off
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write((byte)DS3231_CONTROL);
  Wire.write((byte)ctrl);
  Wire.endTransmission();

  // Attach interrupt on pin 3 to 1Hz output from RTC
  pinMode(3, INPUT_PULLUP); // Is open drain on RTC
  attachInterrupt(1, interrupt1hz, FALLING);
  digitalWrite(13, LOW); // LED off = successful init
}

// LOOP() FUNCTION - RUNS FOREVER ------------------------------------------

void loop() {
  while(!hzFlag); // Wait for interrupt to indicate elapsed second

  uint16_t hi = minutes / 10000, // Value on left (high digits) display
           lo = minutes % 10000; // Value on right (low digits) display
  disp[0].seg7.print(hi, DEC);   // Write values to each display...
  disp[1].seg7.print(lo, DEC);

  // print() does not zero-pad the displays; this may produce a gap
  // between the upper and lower sections.  Add zeros where needed...
  if(hi) {
    if(lo &lt; 1000) {
      disp[1].seg7.writeDigitNum(0, 0);
      if(lo &lt; 100) {
        disp[1].seg7.writeDigitNum(1, 0);
        if(lo &lt; 10) {
          disp[1].seg7.writeDigitNum(3, 0);
        }
      }
    }
  } else {
   disp[0].seg7.clear(); // Clear 'hi' display
  }

  // Flash rightmost dot (1 sec on, 1 sec off)
  disp[1].seg7.writeDigitNum(4, lo % 10, seconds &amp; 1);

  disp[0].seg7.writeDisplay(); // Push data to displays
  disp[1].seg7.writeDisplay();

  hzFlag = false; // Clear flag for next interrupt
}
```

### 

- Confirm the correct board is selected (“Pro Trinket 5V/16MHz (USB)” or “Pro Trinket 3V/12MHz (USB)”, whichever you’re using).
- Confirm _all three_ libraries are correctly installed: RTClib, Adafruit\_GFX and Adafruit\_LEDBackpack.

### 

- Did you install the wire from&nbsp; **SQW** on the clock board to **digital pin 3** on the Pro Trinket?
- There may be&nbsp;a power or communication problem between the Pro Trinket and the displays. Check all four connections: BUS, GND, A4/SDA/D and A5/SCL/C, make sure the latter two aren’t reversed.
- Look the circuit over for accidental solder bridges or cold solder joints.
- Are the displays correctly assembled per [these directions](../../../../adafruit-led-backpack/0-54-alphanumeric?view=all#0-dot-56-seven-segment-backpack)?

### 

Bridge the “A0” pads on the back of _one_ display with a bit of solder. This assigns each display a unique I2C address.

### 

The LED displays are mounted in the wrong positions. No need to dismantle anything, just switch the “0x70” and “0x71” on these two lines (around line 52) and re-upload:

```
  { 0x70, Adafruit_7segment() },  // High digits
  { 0x71, Adafruit_7segment() }   // Low digits
```

# Mindfulness Clock OF DOOM

## Tidbits

Nothing vital to the project here…you can call it done and put it on display&nbsp;now if you like. Just some extra tidbits and trivia for the curious…

# That extra wire between SQW and Pin 3…
![](https://cdn-learn.adafruit.com/assets/assets/000/034/342/medium800/arduino_diagram4.jpg?1469724460)

On most Arduino-type boards, the onboard oscillator (from which all timing — such as the delay() function — is derived) is good, but it’s not _that_ good. Over time, it may pick up or lose a few seconds per day. But the DS3231 RTC might&nbsp;drift by just a few seconds per _year!_

Rather than having our code continually poll&nbsp;the RTC&nbsp;or using&nbsp;delay() or millis() to refresh the display, a special feature of the DS3231 is used: an optional precise **1 Hz square wave output** can be enabled. After initially reading the clock on startup (and doing some math with dates and times), the code then watches&nbsp;this signal as a second counter. Any subsequent math is _much_ simpler! Just needs to count 60 seconds, then decrement the number of minutes.

**Pin 3 on the Pro Trinket is special.** We can “attach an _interrupt_” to it…when the signal on that pin changes, some code is automatically run. We don’t even need to continually call digitalRead(), it just _happens._

That’s how we blink the decimal point at precise 1 second intervals and count minutes, and avoid doing all that time and date math over and over and over…in fact, if we wanted, we could&nbsp;stop the processor completely in a low-power state, having it wake up each time&nbsp;this interrupt occurs. Since this isn’t a battery-powered&nbsp;project, and the always-on LED displays use more&nbsp;current than the microcontroller, there’s no real practical need for that…but I at least wanted to demonstrate the interrupt feature in action, it might come in handy for your own projects later.

```
void interrupt1hz(void) {
  if(++seconds &gt;= 60) {
    seconds = 0;
    if(minutes) minutes--;
  }
  hzFlag = true;
}

setup() {
  // This will make the interrupt1hz() function run automatically every
  // time Pin 3 changes from HIGH to LOW logic state (a "FALLING" signal):
  attachInterrupt(1, interrupt1hz, FALLING);
}
```

You’ll see this same technique in action in our [Talking d20 project](../../../../talking-d20-20-sided-gaming-die), which _does_ use low-power sleep modes to maximize battery life. It watches for an accelerometer in free-fall, waking the microcontroller when detected.

# Mind Tricks

When you glance at your clock, you might notice the decimal point “sticks” for a moment&nbsp;before it resumes blinking. How does the clock know when you’re looking at it? Or is&nbsp;this a bug in the code?

Neither! It’s an optical illusion known as _chronostasis_ or the “stopped clock illusion.” [Wikipedia has this to say about chronostasis](https://en.wikipedia.org/wiki/Chronostasis):

> Chronostasis is a type of&nbsp;temporal illusion&nbsp;in which the first impression following the introduction of a new event or task demand to the brain appears to be extended in time.&nbsp;This effect can extend apparent durations by up to 500 ms and is consistent with the idea that the visual system models events prior to perception.

Basically, _perception_ — making _sense_ of what you see — runs a little slower than _visual sensation_ —&nbsp;when photons are absorbed by the retina. Certain times (e.g. fast eye movements, as when glancing over to a clock) the mind has to fill&nbsp;in the&nbsp;gaps. Hilarity ensues.

_Science!_


## Featured Products

### Adafruit Pro Trinket - 5V 16MHz

[Adafruit Pro Trinket - 5V 16MHz](https://www.adafruit.com/product/2000)
 **Deprecation Warning: The Pro Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Pro Trinket so that people can maintain some older projets, we no longer recommend it.** Please...

In Stock
[Buy Now](https://www.adafruit.com/product/2000)
[Related Guides to the Product](https://learn.adafruit.com/products/2000/guides)
### Adafruit Pro Trinket - 3V 12MHz

[Adafruit Pro Trinket - 3V 12MHz](https://www.adafruit.com/product/2010)
 **Deprecation Warning: The Pro Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Pro Trinket so that people can maintain some older projets, we no longer recommend it.** Please...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2010)
[Related Guides to the Product](https://learn.adafruit.com/products/2010/guides)
### Adafruit DS3231 Precision RTC Breakout

[Adafruit DS3231 Precision RTC Breakout](https://www.adafruit.com/product/3013)
The datasheet for the **DS3231** explains that this part is an "Extremely Accurate I²C-Integrated RTC/TCXO/Crystal". And, hey, it does exactly what it says on the tin! This **Real Time Clock (RTC)** is the most precise you can get in a small, low power...

In Stock
[Buy Now](https://www.adafruit.com/product/3013)
[Related Guides to the Product](https://learn.adafruit.com/products/3013/guides)
### CR1220 12mm Diameter - 3V Lithium Coin Cell Battery

[CR1220 12mm Diameter - 3V Lithium Coin Cell Battery](https://www.adafruit.com/product/380)
These are the highest quality & capacity batteries, the same as shipped with the iCufflinks,&nbsp;iNecklace, Datalogging and GPS Shields, GPS HAT, etc. One battery per order (you'll want one battery per cufflink or pendant.)  
  
Brand may vary but all battery brands are verified...

In Stock
[Buy Now](https://www.adafruit.com/product/380)
[Related Guides to the Product](https://learn.adafruit.com/products/380/guides)
### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

In Stock
[Buy Now](https://www.adafruit.com/product/1995)
[Related Guides to the Product](https://learn.adafruit.com/products/1995/guides)
### Adafruit 0.56" 4-Digit 7-Segment Display with I2C Backpack - Red

[Adafruit 0.56" 4-Digit 7-Segment Display with I2C Backpack - Red](https://www.adafruit.com/product/878)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/878)
[Related Guides to the Product](https://learn.adafruit.com/products/878/guides)
### Adafruit 0.56" 4-Digit 7-Segment Display w/I2C Backpack - Yellow

[Adafruit 0.56" 4-Digit 7-Segment Display w/I2C Backpack - Yellow](https://www.adafruit.com/product/879)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/879)
[Related Guides to the Product](https://learn.adafruit.com/products/879/guides)
### Adafruit 0.56" 4-Digit 7-Segment Display w/ I2C Backpack - Green

[Adafruit 0.56" 4-Digit 7-Segment Display w/ I2C Backpack - Green](https://www.adafruit.com/product/880)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/880)
[Related Guides to the Product](https://learn.adafruit.com/products/880/guides)

## Related Guides

- [Adafruit LED Backpacks](https://learn.adafruit.com/adafruit-led-backpack.md)
- [Trinket React Counter](https://learn.adafruit.com/trinket-react-counter.md)
- [Collin's Lab: Binary & Hex](https://learn.adafruit.com/collins-lab-binary-and-hex.md)
- [Adding a Real Time Clock to Raspberry Pi](https://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi.md)
- [Adafruit DS3231 Precision RTC Breakout](https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout.md)
- [Fidget Spinner Tachometer](https://learn.adafruit.com/fidget-spinner-tachometer.md)
- [Raspberry Pi Physical Dashboard](https://learn.adafruit.com/raspberry-pi-physical-dashboard.md)
- [Arduino GPS Clock](https://learn.adafruit.com/arduino-clock.md)
- [CircuitPython MIDI to CV Skull](https://learn.adafruit.com/circuitpython-midi-to-cv-skull.md)
- [Animated Flying Toaster OLED Jewelry](https://learn.adafruit.com/animated-flying-toaster-oled-jewelry.md)
- [Your Pulse Displayed with NeoPixels](https://learn.adafruit.com/pulse-sensor-displayed-with-neopixels.md)
- [Camera LED Ring Light](https://learn.adafruit.com/camera-ring-led-light.md)
- [Guardian Robot – Zelda BOTW](https://learn.adafruit.com/guardian-robot-zelda-botw.md)
- [Wireless BLE MIDI Robot Xylophone](https://learn.adafruit.com/wireless-ble-midi-robot-xylophone.md)
- [Pro Trinket Rotary Encoder](https://learn.adafruit.com/pro-trinket-rotary-encoder.md)
