MATLAB Clock




clocksim

Contents

%jonah kadoko
% this is a similation of a clock that was done by Jonah Kadoko
% we have an analog clock moving around
cd('C:\Users\jkadoko\Documents\personalideas\Pendulumn Clock\ClockSim_Pendulum');

this is the set up of the pendulum pivot

mode=1; %mode 1 is for free vibration without dampings ; 2 is for forced fibration with damping
pivotx=100;
pivoty=200;
lengthPend=150;
numEle=3;
initSpeed=0;
initTheta=0.1745; % this is the initial angle of the pendulum.
g=9.81;
c=-0.01;
speed=10000;% the reciprocal of this is the period on which the graphics of the pendulum refresh

calculations of the pendulum positions

%pivotPos=[100 200]; % this is the coodinates of the pivot point of the pendulum
% the format is [x y]
elementPosX=zeros(numEle,1); % I am hoping to have a lot of elements in the
elementPosY=zeros(numEle,1);

elementPosX(1)=pivotx;
elementPosY(1)=pivoty-lengthPend;
%pointMassPos=[100 50];
%setting the initial conditions of the pendulum mass

initPos=[elementPosX(1) elementPosY(1)];
theta=initTheta;
i=0;
t=0;
stop=1;

setting up the clock settings

stop=1;
thetaS=pi/30;
radius=300;
lengthSHand=(4*radius)/6;lengthMHand=(2.5*radius)/6;lengthHHand=(radius/3);
centerx=0;centery=0;
second=0;
close all
axis([-200 200 -200 200]);
clockSimulation=figure;
stopButton = uicontrol('Style', 'pushbutton', 'String', 'Stop','Position', [10 10 100 20], 'Callback', 'stop=0;');
runButton = uicontrol('Style', 'pushbutton', 'String', 'Run','Position', [115 10 100 20], 'Callback', 'clocksim;');
handsX=zeros(3,1);%
%row one of the handsX has the second hand, row 2 has the minute hand and
%the 3rd row has the hour hand
% the first colum has the coordinates of the point away from the  center of
% the clock, while the second co-oridinates show the the co-ordinates of
% the center, it was not therefore neccessary to have in the final
% iteration
handsY=zeros(3,1);% the first hand is the second hand , minute hand and then hour hand
mov=VideoWriter('clock.avi');mov.FrameRate=10;open(mov);

while stop==1

this section calculates the movement of the second hand

    %the angular movements of the hands
    thetaS=-1*(pi/30)*second;thetaM=thetaS/60;thetaH=thetaM/60;

    handsX(1)=centerx+lengthSHand*cos(thetaS+pi/2);
    handsY(1)=centery+lengthSHand*sin(thetaS+pi/2);

minute hand

    handsX(2)=centerx+lengthMHand*cos(thetaM+pi/2);
    handsY(2)=centery+lengthMHand*sin(thetaM+pi/2);

hour hand

    handsX(3)=centerx+lengthHHand*cos(thetaH+pi/2);
    handsY(3)=centery+lengthHHand*sin(thetaH+pi/2);

draw the clock

    drawClock(clockSimulation,radius,centerx,centery,handsX,handsY);
    second=second+1;

    if(second==3000)
        stop=0;
    end

    pause(0.000001);
frame=getframe(gcf);
writeVideo(mov,frame);

end
hold off
close(mov)