0

I have an assigment to make an analog clock in C PIC18 starter kit. I need to draw all the hands of the clock, aka seconds, minutes and hours.

I have initialized all 60 points of the clock diamater in a 2D array with x and y values and have the values of the clock center.

In order to draw the hands I've been provided for this assigment with the DrawLine function, which looks like this:

void drawLine( BYTE x0, BYTE y0, BYTE x1, BYTE y1, LineWidth lw )

the x0, y0 are where the line starts, and x1,y1 are where the line ends.

the drawLine function works like a XOR, so if I call it again on the same values the line disappears from the screen.

the screen values when they're x=0,y=0 starts from the top-left corner of the screen.

I have built a function in order to draw the hour hand of the clock. I have an i value which increments by 1 for each coordinate of the clock, and it goes like this:

center[0][0]+(cord[i][0]-center[0][0])/2

But for some reason it only works on the 4th quadrant of the clock (aka when i is between 15 and 30), otherwise the lines it draws dosn't resemble an hand of a clock.

Below this is the full code for your understanding, but I would like to know what is wrong with my function, and what do I need to do in order of it to draw normally for the rest of thequadrants.

BYTE cord[60][2] = {
{67,0},{71,1},{74,2},{79,3},{81,4},
{82,5},{84,6},{88,9},{91,12},{92,14},
{93,16},{95,18},{96,21},{97,25},{98,29},
{98,32},{98,35},{97,39},{96,43},{95,45},
{93,47},{92,50},{89,53},{85,56},{84,58},
{82,58},{79,60},{76,61},{73,62},{70,63},
{67,63},{64,63},{60,62},{57,61},{54,60},
{51,58},{47,56},{45,54},{43,52},{41,50},
{40,47},{38,44},{37,41},{36,38},{35,35},
{35,32},{35,29},{36,26},{37,22},{38,18},
{40,16},{41,13},{44,10},{47,7},{49,6},
{51,5},{52,4},{54,3},{57,2},{61,1}};




void main(void)
{
    BYTE xtemp, ytemp ;
    BYTE i = 0;
    BYTE center[1][2] = {{67,32}};
    InitializeSystem();

    while(1)                            
    {
        xtemp = center[0][0]+(cord[i][0]-center[0][0])/2;
        ytemp =  center[0][1]+(cord[i][1]-center[0][1])/2;
        drawLine( center[0][0], center[0][1], xtemp, ytemp, thick ) ;
        DelayMs(50);
        drawLine( center[0][0], center[0][1], xtemp, ytemp, thick );
        i++;
            if(i>60)
                i = 0;

    }
}
Play38
  • 115
  • 1
  • 11
  • 3
    Is `BYTE` an unsigned type? If so, you'll run into trouble when either `cord[i][0] - center[0][0]` or `cord[i][1] - center[0][1]` should be negative, which happens to be whenever the tip of the hand isn't in the bottom-right quadrant. – Graeme Cole Apr 02 '19 at 14:48
  • oh wow, it solved my problem! after I've change all the vars from BYTE to int it works now perfectly. – Play38 Apr 02 '19 at 14:57

1 Answers1

1

There is no standard datatype BYTE included in C. I guess you have a typedef with an unsigned type.
When you do a signed calculation like:

   xtemp = center[0][0]+(cord[i][0]-center[0][0])/2;

you need a signed signed type like int.

Mike
  • 4,041
  • 6
  • 20
  • 37