Ahmed El-Hinidy
Published

Analog Clock using 1Sheeld Graphical LCD

A real-time Analog Clock using 1Sheeld Graphical LCD.

IntermediateFull instructions provided4,900
Analog Clock using 1Sheeld Graphical LCD

Things used in this project

Story

Read more

Schematics

img_3027.jpg

Code

Untitled file

Arduino
#define CUSTOM_SETTINGS

#define INCLUDE_GLCD_SHIELD

#define INCLUDE_TERMINAL_SHIELD

#define INCLUDE_CLOCK_SHIELD

#define PI 3.14159

#include <OneSheeld.h>

GLCDTextBox number12(95,25,"12");

GLCDTextBox number3(140,64,"3");

GLCDTextBox number6(98,110,"6");

GLCDTextBox number9(55,64,"9");

class MyPoint

{

 public:

    int x;

    int y;

       MyPoint(int New_x=0,int New_y=0)

       {

         x=New_x;

         y=New_y;

       }

};

class AnalogClock

{

  public:

    //Main Parameters of the Clock

    ////Location Parametes

    MyPoint Centre;

    int Radius;

    ////Time Parameters

    int Hours;

    int Minutes;

    int Seconds;

    //Hand Location Arrays

    ////Hours Array

    ////Save points of Hours Hand

    MyPoint Hours_Array[12];

    ////Minutes Array

    ////Save points of Minutes Hand

    MyPoint Minutes_Array[60];

    ////Minutes Array

    ////Save points of Minutes Hand

    MyPoint Seconds_Array[60];

    //Hand Shape Objects

    ////Hours Hand

    GLCDLine *Hours_Hand;

    ////Minutes Hand

    GLCDLine *Minutes_Hand;

    ////Seconds Hand

    GLCDLine *Seconds_Hand;

    ////Clock Frame

    GLCDEllipse *Clock_Frame;

    //Utility Functions

    ////Points Arrays Generation

    void Generate_Points_Arrays()

    {

        //Hands Lengths

        int Hours_Hand_Length = 0.3*Radius;

        int Minutes_Hand_Length = 0.5*Radius;

        int Seconds_Hand_Length = 0.7*Radius;

        

        

        //Hours

        //Top Left Quadrant - Points 0->3

        for (int i = 0,Theta = 0; i < 4; i++)

        {

            MyPoint Temp;

            Temp.x = Centre.x + Hours_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Hours_Hand_Length*cos(Theta/180.0*PI);

            

            Hours_Array[i] = Temp;

            Theta += 90 / 3;

        }

        //Bottom Left Quadrant - Points 4->6

        for (int i = 6, Theta = 0; i >= 4; i--)

        {

            MyPoint Temp;

            Temp.x = Centre.x + Hours_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Hours_Hand_Length*cos(Theta/180.0*PI);

            Hours_Array[i] = Temp;

            Theta += 90 / 3;

        }

        //Bottom Right Quadrant - Points 7->9

        for (int i = 7, Theta = 30; i < 10; i++)

        {

            MyPoint Temp;

            Temp.x = Centre.x - Hours_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Hours_Hand_Length*cos(Theta/180.0*PI);

            Hours_Array[i] = Temp;

            Theta += 90 / 3;

        }

        //Top Right Quadrant - Points 10->11

        for (int i = 11, Theta = 30; i >= 10; i--)

        {

            MyPoint Temp;

            Temp.x = Centre.x - Hours_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Hours_Hand_Length*cos(Theta/180.0*PI);

            Hours_Array[i] = Temp;

            Theta += 90 / 3;

        }

        

        

        //Minutes

        //Top Left Quadrant - Points 0->15

        for (int i = 0,Theta = 0; i < 16; i++)

        {

            MyPoint Temp;

            Temp.x = Centre.x + Minutes_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Minutes_Hand_Length*cos(Theta/180.0*PI);

            Minutes_Array[i] = Temp;

            

            Temp.x = Centre.x + Seconds_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Seconds_Hand_Length*cos(Theta/180.0*PI);

            Seconds_Array[i] = Temp;

            Theta += 90 / 15;

        }

        //Bottom Left Quadrant - Points 16->30

        for (int i = 30, Theta = 0; i >= 16; i--)

        {

            MyPoint Temp;

            Temp.x = Centre.x + Minutes_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Minutes_Hand_Length*cos(Theta/180.0*PI);

            Minutes_Array[i] = Temp;

            

            Temp.x = Centre.x + Seconds_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Seconds_Hand_Length*cos(Theta/180.0*PI);

            Seconds_Array[i] = Temp;

            Theta += 90 / 15;

        }

        //Bottom Right Quadrant - Points 31->45

        for (int i = 31, Theta = 90/15; i < 46; i++)

        {

            MyPoint Temp;

            Temp.x = Centre.x - Minutes_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Minutes_Hand_Length*cos(Theta/180.0*PI);

            Minutes_Array[i] = Temp;

            

            Temp.x = Centre.x - Seconds_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y + Seconds_Hand_Length*cos(Theta/180.0*PI);

            Seconds_Array[i] = Temp;

            Theta += 90 / 15;

        }

        //Top Right Quadrant - Points 46->59

        for (int i = 59, Theta = 90/15; i >= 46; i--)

        {

            MyPoint Temp;

            Temp.x = Centre.x - Minutes_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Minutes_Hand_Length*cos(Theta/180.0*PI);

            Minutes_Array[i] = Temp;

            

            Temp.x = Centre.x - Seconds_Hand_Length*sin(Theta/180.0*PI);

            Temp.y = Centre.y - Seconds_Hand_Length*cos(Theta/180.0*PI);

            Seconds_Array[i] = Temp;

            Theta += 90 / 15;

        }

        

    }

public:

    void SetTime(int New_Hours, int New_Minutes, int New_Seconds)

    {

        Hours = New_Hours%12;

        Minutes = New_Minutes;

        Seconds = New_Seconds;

        Seconds_Hand->setCoordinates(Centre.x, Centre.y, Seconds_Array[Seconds].x, Seconds_Array[Seconds].y);

        Minutes_Hand->setCoordinates(Centre.x, Centre.y, Minutes_Array[Minutes].x, Minutes_Array[Minutes].y);

        Hours_Hand->setCoordinates(Centre.x, Centre.y, Hours_Array[Hours].x, Hours_Array[Hours].y);

    }

    AnalogClock(MyPoint New_Centre, int New_Radius, int New_Hours, int New_Minutes, int New_Seconds)

    {

        //Update Main Location Paramaters

        Centre = New_Centre;

        Radius = New_Radius;

        //Update Time Parameters

        Hours = New_Hours%12;

        Minutes = New_Minutes;

        Seconds = New_Seconds;

        //Generare Hands Points

        Generate_Points_Arrays();

        //Create Shapes

        ////Clock Frame

        Clock_Frame = new GLCDEllipse(Centre.x, Centre.y, Radius, Radius);

        ////Hours Hand

        Hours_Hand = new GLCDLine(Centre.x, Centre.y, Hours_Array[Hours].x, Hours_Array[Hours].y);

        Minutes_Hand = new GLCDLine(Centre.x, Centre.y, Minutes_Array[Minutes].x, Minutes_Array[Minutes].y);

        Seconds_Hand = new GLCDLine(Centre.x, Centre.y, Seconds_Array[Seconds].x, Seconds_Array[Seconds].y);

    }

    void Begin()

    {

        GLCD.draw(*Clock_Frame);

        GLCD.draw(*Hours_Hand);

        GLCD.draw(*Minutes_Hand);

        GLCD.draw(*Seconds_Hand);

        GLCD.draw(number12);

        GLCD.draw(number3);

        

        GLCD.draw(number6);

        

        GLCD.draw(number9);

    }

};

MyPoint ClockCentre(100,70);

AnalogClock Clock1(ClockCentre,50,0,0,0);

void setup() {

 // put your setup code here, to run once:

 OneSheeld.begin();

 GLCD.clear();

 Clock.queryDateAndTime();

 Clock1.Begin();

}

void loop() {

 // put your main code here, to run repeatedly:

 Clock1.SetTime(Clock.getHours(),Clock.getMinutes(),Clock.getSeconds());

}

Analog Clock Arduino Sketch

Credits

Ahmed El-Hinidy

Ahmed El-Hinidy

2 projects • 31 followers
Computer Engineer. Embedded Systems and Web Development enthusiast !

Comments