Arduino Library Modifications

Prior to the actual doomsday, we plan to use DOOMSDAY as a race timer for mountain bike races.  To that end we need precision time keeping.  We have a 1 ms accuracy goal relative to an absolute GPS time reference.  The GPS module provides time data in NEMA format as well as a one pulse per second (1pps) reference signal.  When the GPS signal goes out of view, a real time clock chip keeps track of time and provides its own 1 Hertz signal: a 1 Hertz square wave.

In order to sync the 1pps signals to the real time clock square wave, I made some modifications to two libraries: Time, and TinyGPS (by Mikal Hart).  Precision timing is made possible by adding a 1Hz reference to the existing library.  Millisecond accuracy is obtained by keeping track of the last 1 Hz transistion and the 1 Hz duration.  So in addition to the standard year(), month(), day(), hour(), minute(), and second() functions, I added millisecond() which returns an integer between 0 and 999.

Some other functions were added to support the 1Hz reference:

/* TJS: one Hertz interrupt to be called on rising edge of one Hz square wave. 
 *      Used to sync with GPS clock or other 1Hz source to get millisecond time accuracy
 *      trigger is one of LOW, CHANGE, RISING, or FALLING
 */
void set_1Hz_ref(time_t current_time, int interrupt_pin, void(*cb_ptr)(), int trigger);

/*
 * TJS: stop counting ticks.  Used to sync with absolute time.
 */
void pause_1Hz();

/*
 * TJS: start counting ticks.  Used to sync with absolute time.
 */
void unpause_1Hz();

The TinyGPS library also needed to be modified to support precision time synchronization.  The key enabler is a fix notification scheme.  At the heart is a user provided function that gets called when a new fix message arrives.   This function was added to support this:

/*
 * TJS: Add a callback routine to be called when a new fix arrives.
 */
void TinyGPS::add_callback(fix_cb_t fct_ptr);

// Example fix callback signature
void grab_datetime(unsigned long _date,
                   unsigned long _time,
                   long lat,
                   long lon,
                   long alt,
                   unsigned long speed,
                   unsigned long course);

Sum of Squares

There is a lovely formula for the sum S_n of the first n square integers.  Namely

S_n = 1^2 + 2^2 + 3^2 + \ldots + n^2 = \frac{n(n+1)(2n + 1)}{6}.

Sum of Squares

The sum of the first 6 cubes represented as a solid.

 

Imagine the sum S_n  as the volume of the pyramid of 1 x 1 x 1 cubes with one cube on the top layer, 4 on the next, 9 on the next and so on up to n^2 cubes on the bottom layer as seen in the figure above. From the above expression, we see that the sum is one sixth the volume of a box with dimensions  n, n + 1, and 2n + 1. So it is at least conceivable that six of these pyramids could be packed into a rectangular volume of that size.  And as the pictures indicate below, there is such an arrangement.

Pythagorean Theorem Puzzle

This looks like a great first project for the Fireball Comet. The famous Pythagorean Theorem states that if a and b are the lengths of two legs of a right triangle and c is the length of the hypotenuse, then

a^2 + b^2 = c^2.

This puzzle demonstrates this theorem, by challenging the solver to make a square with area c^2 using the pieces taken from the two squares with areas a^2 and b^2.

Looking for more cool math demonstrations. Please submit your ideas!

The Pythagorean Theorem