Analogue Clock

#include <TimerOne.h>
/**
 Analog Clock
 Paul Cox Dec 2010
*/
#define    PI      3.141592653589793e-06;
byte rows[8] = {9, 14, 8, 12, 1, 7, 2, 5};
byte cols[8] = {13, 3, 4, 10, 6, 11, 15, 16};
byte pins[16] = {5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10, 9, 8, 7, 6};
byte screen[8] = {0, 0, 0, 0, 0, 0, 0, 0};
volatile byte screenRow = 0;
volatile byte screenCol = 0;
int iHour = 0;
int iMin = 0;
int iSec = 0;
void setup()
{
  Timer1.initialize(100);
  for (int i = 2; i <= 17; i++)
  {
     pinMode(i, OUTPUT);
  }
  Timer1.attachInterrupt(doubleBuffer);
  Serial.begin(9600);
  resetAnim();
}
void doubleBuffer()
{
  digitalWrite(translatePin(rows[screenRow]), LOW);
  digitalWrite(translatePin(cols[screenCol]), HIGH);
  screenCol++;
  if (screenCol >= 8)
  {
     screenCol = 0;
     screenRow++;
     if (screenRow >= 8)
     {
       screenRow = 0;
     }
   }
   if((screen[screenRow] >> screenCol) & B1 == B1)
   {
     digitalWrite(translatePin(rows[screenRow]), HIGH);
     digitalWrite(translatePin(cols[screenCol]), LOW);
   }
   else
   {
     digitalWrite(translatePin(rows[screenRow]), LOW);
     digitalWrite(translatePin(cols[screenCol]), HIGH);
   }
}
byte translatePin(byte original)
{
  return pins[original - 1];
}
void allOFF()
{
  for (int i = 0; i < 8; i++)
  {
     screen[i] = 0;
  }
}
void on(byte row, byte column)
{
  screen[column-1] |= (B1 << (row - 1));
}
void off(byte row, byte column)
{
  screen[column-1] &= ~(B1 << (row - 1));
}
void resetAnim()
{
  for (int i = 0; i < 8; i++)
  {
     screen[i] = B11111111;
     delay(25);
  }
  for (int i = 0; i < 8; i++)
  {
     screen[i] = B00000000;
     delay(25);
  }
}
void loop()
{
  drawClock();
  iSec++;
  if (iSec == 60)
  {
    iSec = 1;
    iMin++;
    if (iMin == 60)
    {
       iMin = 0;
       iHour++;
       if (iHour == 12)
       {
          iHour = 0;
       }
    }
  }
  delay(10);
}
void drawClock()
{
  allOFF();
  setHand(iHour,12,3);
  setHand(iMin,60,4);
  setHand(iSec,60,5);
}
// work out the pixel for a hand - val is out of a possible max val
// radius is distance from the centre
void setHand(int iVal, int iMax, int iRadius)
{
  double  dAngle = (iVal * 2 * PI) / iMax;
  double  dPosX = 4.5 + (iRadius * cos(dAngle));
  double  dPosY = 4.5 + (iRadius * sin(dAngle));
  drawLine(5,5,constrain(round(dPosX),1,8),constrain(round(dPosY),1,8));
}
void drawLine(int x0, int y0, int x1, int y1)
{
  int      iTemp;
  boolean  bSteep = abs(y1 - y0) > abs(x1 - x0);
  if (bSteep)
  {
     iTemp = x0; x0 = y0; y0 = iTemp;    // swap x0,y0
     iTemp = x1; x1 = y1; y1 = iTemp;    // swap x1,y1
  }
  if (x0 > x1)
  {
     iTemp = x0; x0 = x1; x1 = iTemp;    // swap x0,x1
     iTemp = y1; y1 = y0; y0 = iTemp;    // swap y0,y1
  }
  int deltax = x1 - x0;
  int deltay = abs(y1 - y0);
  int error = deltax / 2;
  int ystep = ((y0 < y1) ? 1 : -1);
  int y = y0;
  int x = x0;
  while(x <= x1)
  {
    if (bSteep)
    {
       on(y,x);
    }
    else
    {
       on(x,y);
    }
    error = error - deltay;
    if (error < 0)
    {
       y = y + ystep;
       error = error + deltax;
    }
    x++;
  }
}

9 thoughts on “Analogue Clock

  1. Hi friend, i like you’r clock great work. when i compile the code it’s sowing some error . can you please help me. please

    double dAngle = (iVal * 2 * PI) / iMax;

    sketch_sep14c.ino: In function ‘void setHand(int, int, int)’:
    sketch_sep14c:229: error: expected `)’ before ‘;’ token
    sketch_sep14c:229: error: expected primary-expression before ‘)’ token
    sketch_sep14c:229: error: expected `;’ before ‘)’ token

Leave a Reply

Your email address will not be published. Required fields are marked *