Skip to content

Instantly share code, notes, and snippets.

@edgar-bonet
Last active November 19, 2020 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edgar-bonet/ee68825b9ddec4b732b4 to your computer and use it in GitHub Desktop.
Save edgar-bonet/ee68825b9ddec4b732b4 to your computer and use it in GitHub Desktop.
Alternate firmware for Daniel Andrade's binary clock
/*
* ebclock.ino: Edgar's binary clock.
*
* This is an alternate firmware for Daniel Andrade's binary clock.
* For details and schematic, see Daniel's post at
* http://www.danielandrade.net/2008/07/15/binary-clock-with-arduino/
*
* A few things are changed with respect to the original firmware:
* - you can fine-tune the clock speed if it runs too fast or too slow
* - you do not need the 2.2 KOhm pull-up resistors
* - it consumes less power (useful when running on batteries)
* - changing the hours does not reset the seconds (useful when
* adjusting for DST).
*
* Copyright (c) 2016 Edgar Bonet Orozco.
* Released under the terms of the MIT license:
* https://opensource.org/licenses/MIT
*/
#include <avr/sleep.h>
// Pins connected to the buttons. Each button should be connected
// between the corresponding pin and ground.
const int display_button = 0;
const int set_minutes_button = A0;
const int set_hours_button = A5;
// Clock correction in ppm. Set this to a positive number if your clock
// runs slow, to a negative number if it runs fast. One second per day
// is about 11.57 ppm.
const int correction = 0;
void setup()
{
// Buttons as inputs.
pinMode(display_button, INPUT_PULLUP);
pinMode(set_minutes_button, INPUT_PULLUP);
pinMode(set_hours_button, INPUT_PULLUP);
// LEDs as outputs.
for (int i = 1; i <= 13; i++)
pinMode(i, OUTPUT);
}
void loop()
{
static const uint32_t tick_period = 1e6 - correction;
static uint8_t hours, minutes, seconds;
static uint32_t last_tick;
static uint16_t bits;
// Every second:
if (micros() - last_tick >= tick_period) {
last_tick += tick_period;
// Increment hours:minutes:seconds.
if (++seconds >= 60) {
seconds = 0;
if (++minutes >= 60) {
minutes = 0;
if (++hours >= 24)
hours = 0;
}
}
// Re-compute the bit pattern.
bits = hours/10 << 12 | hours%10 << 8
| minutes/10 << 5 | minutes%10 << 1;
}
// Light the LEDs if the "display" button is pressed.
if (digitalRead(display_button) == LOW)
for (int i = 1; i <= 13; i++)
digitalWrite(i, bits & 1<<i);
else
for (int i = 1; i <= 13; i++)
digitalWrite(i, LOW);
// Change the time if requested.
if (digitalRead(set_minutes_button) == LOW) {
if (++minutes >= 60)
minutes = 0;
seconds = 0;
delay(250);
}
if (digitalRead(set_hours_button) == LOW) {
if (++hours >= 24)
hours = 0;
delay(250);
}
// Sleep for a while to save power.
sleep_mode();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment