Project tutorial
Analog Clock using 1Sheeld Graphical LCD

Analog Clock using 1Sheeld Graphical LCD

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

  • 2,198 views
  • 0 comments
  • 7 respects

Components and supplies

About this project

This is a mini project of where I draw an Analog Clock on 1Sheeld's Graphical LCD using its new GLCD shield. This clock also updates time and moves the "hours, minutes, and seconds" hands according to the actual time on your smartphone using 1Sheeld's Clock shield.

Code

Untitled fileArduino
#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

Schematics

img_3027.jpg
Img 3027

Comments

Similar projects you might like

4x4x4 LED cube with Arduino Uno and 1sheeld

Project tutorial by Hassan Ibrahim

  • 29,722 views
  • 6 comments
  • 60 respects

1Sheeld CanSat

Project tutorial by Fatima Fouda

  • 3,984 views
  • 0 comments
  • 19 respects

LCD display distance of an ultrasonic sensor

Project tutorial by adam johns

  • 6,481 views
  • 0 comments
  • 8 respects

Bluetooth Steering wheel for a robot using 1Sheeld

Project tutorial by Kristian Blåsol

  • 2,438 views
  • 1 comment
  • 13 respects

Voice Assistant Using A Smart Phone And 1Sheeld

Project tutorial by Oguz Salar

  • 1,380 views
  • 0 comments
  • 5 respects

Led fading using 1Sheeld

Project tutorial by Nado Hollow

  • 671 views
  • 1 comment
  • 2 respects
Add projectSign up / Login