lilypad Binary Clock not keeping time. 1/2 speed - Help?

Recently built the Binary Clock from the "Evil Genius" book and it does not keep time.

I used the 1.0 software for program and linked the lilypad to my Arduino Duemilanove board Rx / Tx for programming. All seemed well, but the clock is running about 1/2 the speed it should.

I see many posts on this related to possible programming or software issues. Would it be best to link this to a real-time clock module or does someone have a good suggestion? I spent too much time building this for it to be "semi-functional". I'm not sure how I would read the time from a module. I'm only a beginner with Adruino.

Sincerely, Dan

Does the lilypad use an 8 MHz crystal/oscilater vs 16 Hz? That could be the problem.

Let's see some code. What board did you select when building the code?

The Lilypad, if I remember correctly, runs at 8MHz, not 16MHz, so some adjustments are required. They should be handled automatically, if you select the correct board type. If not, code will run half as fast as expected.

I am not sure. I believe it is the 8MHz processor. I have the LilyPad 328.

I think the best solution would be to add a “real-time” clock module to the clock and take the time reading from there. Not sure how to do this. :frowning:

Trying to get over the learning curve!

My little 6-pin tab snapped off so I programmed the Lilypad by hooking up the power, Rx, Tx from my Duemilanove ATmega328 and sending over the program. I removed the IC it it appeared to work. Should I send it calling the board the lilypad or Duemilanove 328?

Should I send it calling the board the lilypad or Duemilanove 328?

Try both. If one gives the correct time, and the other doesn't, you'll know which is correct. If neither result in the correct time, get an RTC.

I think the key is selecting one of the '328, 8MHz boards.

Hi, I think the suggestions are correct about selecting the right board type.

My clock doesn't keep brilliant time (it gains a few minutes a day) but its basically okay and setting it with the magnet is fun anyway.

I laser cut a new case for mine.

http://www.doctormonk.com/2012/01/laser-cutting-at-fablab-manchester.html

I built mine into a nice oak frame that I had from an old clock.

I’ve tried programing my lilypad 328 with a different board selection, but no luck. I’m attaching the Rx and TX lines fro my Duemilanove board and seem to be able to update the program.

My clock is exactly 1/2 the speed it should be. I recall something about this with an older version of the software, but mine is current.

Is there a way in coding to double the value of the time it is displaying? This should effectively speed it up…!?

I’m looking at trying to figure out how to add a Real-Time clock module and just ordered one. I ay then run into problems setting the tie for day-light savings time. Grrrr… :slight_smile:

I am very new to Arduino buy trying to figure this stuff out. Below is my code if anyone had any suggestions!

#include <Time.h>

int hourLEDs = {4, 3, 2, 1}; // least sigificant bit frst
int secondLEDs = {10, 9, 8, 7, 6, 5};
int minuteLEDs = {17, 16, 15, 14, 13, 12};

int loopLEDs = {10, 9, 8, 7, 6, 5, 17, 16, 15, 14, 13, 12, 1, 2, 3, 4};

int switchPin = 18;

void setup()
{
for (int i = 0; i < 4; i++)
{
pinMode(hourLEDs*, OUTPUT);*

  • }*
  • for (int i = 0; i < 6; i++)*
  • {*
    _ pinMode(minuteLEDs*, OUTPUT);_
    _
    }_
    _
    for (int i = 0; i < 6; i++)_
    _
    {_
    _ pinMode(secondLEDs, OUTPUT);
    }
    setTime(0);
    }
    void loop()
    {

    if (digitalRead(switchPin))
    {
    adjustTime(1);
    }
    else if (minute() == 0 && second() == 0)
    {
    spin(hour());
    }
    updateDisplay();
    delay(1);
    }
    void updateDisplay()
    {_
    time_t t = now();
    _ setOutput(hourLEDs, 4, hourFormat12(t));
    setOutput(minuteLEDs, 6, minute(t));
    setOutput(secondLEDs, 6, second(t));
    }
    void setOutput(int *ledArray, int numLEDs, int value)
    {
    for (int i = 0; i < numLEDs; i++)
    {
    digitalWrite(ledArray, bitRead(value, i));
    }
    }
    void spin(int count)
    {
    for (int i = 0; i < count; i++)
    {
    for (int j = 0; j < 16; j++)
    {
    digitalWrite(loopLEDs[j], HIGH);
    delay(50);
    digitalWrite(loopLEDs[j], LOW);
    }
    }*_

Myself, I prefer to write my own functions rather than use libraries written by others.
This is so I can understand what’s going on, as well as so that I can modify it to do exactly what I want.

Let’s not use the Time.h library. Instead, let’s use this:

// these four variables hold the time
byte hour = 12; // we will not start at 0, because this is a 12-hour clock
byte minute = 0;
byte second = 0;
byte tick = 0; // we will use 16 ticks = 1 second (real time)
// these variables control changing the time
unsigned long lastTick = 0UL; // start of last tick
unsigned long oneTick = 31250UL; // because we want our clock to run at double speed

void figureTime(byte runfast) { // figure out what time it is now
  // if argument is true, clock will run very quickly (for setting mode)
  while ((micros()-lastTick) >= oneTick) {
    lastTick += oneTick;
    if (runfast) {
      tick = 0;
      second += 20; 
      second -= (second%20); 
    }
    else {
      tick++;
    }
  }
  while (tick >= 16) {
    tick -= 16;
    second++;
  }
  while (second >= 60) {
    second -= 60;
    minute++;
  }
  while (minute >= 60) {
    minute -= 60;
    hour++;
  }
  while (hour > 12) {
    hour -= 12;
  }
}

Next, I’ll integrate it with your code.
This is untested code; if there are problems, let me know.

int hourLEDs[] = {4, 3, 2, 1}; // least sigificant bit frst
int secondLEDs[] = {10, 9, 8, 7, 6, 5};
int minuteLEDs[] = {17, 16, 15, 14, 13, 12};

int loopLEDs[] = {10, 9, 8, 7, 6, 5, 17, 16, 15, 14, 13, 12, 1, 2, 3, 4};

int switchPin = 18;


// these four variables hold the time
byte hour = 12; // we will not start at 0, because this is a 12-hour clock
byte minute = 0;
byte second = 0;
byte tick = 0; // we will use 16 ticks = 1 second (real time)

// these variables control changing the time
unsigned long lastTick = 0UL; // start of last tick
unsigned long oneTick = 31250UL; // because we want our clock to run at double speed



void setup()       
{
  for (int i = 0; i < 4; i++)
  {
     pinMode(hourLEDs, OUTPUT);
  }
  for (int i = 0; i < 6; i++)
  {
     pinMode(minuteLEDs, OUTPUT);
  }
  for (int i = 0; i < 6; i++)
  {
     pinMode(secondLEDs, OUTPUT);
  }
}

void loop()                     
{
  byte settingNow = (digitalRead(switchPin)==HIGH); // are we setting the time now?
  figureTime(settingNow);
  updateDisplay(!settingNow);
  delay(1);
}

void updateDisplay(byte spinOK)
// argument is true if spinning is OK
{
  if (spinOK && (minute==0) && (second==0)) { // "spin" mode
    setOutput(loopLEDs, 16, tick);
  }
  else { // just show the time
    setOutput(hourLEDs, 4, hour);
    setOutput(minuteLEDs, 6, minute);
    setOutput(secondLEDs, 6, second);
  }
}


void setOutput(int *ledArray, int numLEDs, int value)
{
    for (int i = 0; i < numLEDs; i++)
    {
     digitalWrite(ledArray, bitRead(value, i));
    }
}

void figureTime(byte runfast) { // figure out what time it is now
  // if argument is true, clock will run very quickly (for setting mode)
  while ((micros()-lastTick) >= oneTick) {
    lastTick += oneTick;
    if (runfast) {
      tick = 0;
      second += 20; 
      second -= (second%20); 
    }
    else {
      tick++;
    }
  }
  while (tick >= 16) {
    tick -= 16;
    second++;
  }
  while (second >= 60) {
    second -= 60;
    minute++;
  }
  while (minute >= 60) {
    minute -= 60;
    hour++;
  }
  while (hour > 12) {
    hour -= 12;
  }
}