0% found this document useful (0 votes)
51 views

Alarm Coding

This C program implements an alarm clock and timer using real-time functions. It contains functions for incrementing and decrementing the clock/timer, sounding an alarm, and getting user input. The main function displays a menu, takes the user selection, and calls the appropriate clock or timer functions inside a loop until the alarm time is reached. It outputs the current and alarm times during the countdown. When finished, it displays a completion message and exits.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Alarm Coding

This C program implements an alarm clock and timer using real-time functions. It contains functions for incrementing and decrementing the clock/timer, sounding an alarm, and getting user input. The main function displays a menu, takes the user selection, and calls the appropriate clock or timer functions inside a loop until the alarm time is reached. It outputs the current and alarm times during the countdown. When finished, it displays a completion message and exits.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM:

Alarm.c
#include<stdio.h>
#include<conio.h>
#include<dos.h>
struct clk
{
int hh,mm,ss;
}c1,c2;
void clock(int *h1,int *m1,int *s1)
{
*s1=*s1+1;
if(*s1==60)
{
*s1=0; *m1=*m1+1;
if(*m1==60)
{
*m1=0;*h1=*h1+1;
if(*h1==24)
*h1=0;
}
}
}
void timer(int *h,int *m,int *s)
{
if((*s)!=0)
{
*s=*s-1;
}
else if((*s)==0)
{
if(*m!=0)
{
*s=59;*m=*m-1;
}
else if(*m==0)
{
if(*h!=0)
{

*m=59;*h=*h-1;
}
}
}
}
void alarm()
{
int i;
while(!kbhit())
{
for(i=0;i<2;i++)
{
sound(5000);
delay(100);
nosound();
delay(200);
}
delay(500);
}
}
void main()
{
char ch;
struct time t;
clrscr();
printf("\nPress:-\n\tA: for alarm Clock\n\tT: for Timer\n");
printf("\Enter your Choice:");
ch=getche();
switch (ch)
{
case 'A':
case 'a':
{
printf("\n\n\n24 hr Format(HH:MM:SS)");
gettime(&t);
c1.hh=t.ti_hour; c1.mm=t.ti_min; c1.ss=t.ti_sec;
printf("\nEnter alarm time : ");
scanf("%d:%d:%d",&c2.hh,&c2.mm,&c2.ss);
if(c2.hh>24||c2.mm>60||c2.ss>60)
{
printf("\n\n\tERROR: Invalid time.\n\tRestart the program.");
delay(2500);exit(0);

}
while((c1.ss!=c2.ss)||(c1.hh!=c2.hh)||(c1.mm!=c2.mm))
{
clrscr();
printf("\n\nAlarm time:
%02d:%02d:%02d\n",c2.hh,c2.mm,c2.ss);
printf("\nCurrent Time:
%02d:%02d:%02d",c1.hh,c1.mm,c1.ss);
clock(&c1.hh,&c1.mm,&c1.ss);
delay(1000);
};
clrscr();
printf("\n\n\n\n\t\t\t\tAlarm time reached\n\n\t\t\t\tPress any to Exit.");
alarm();
exit(0);
}
break;
case 'T':
case 't':
{
printf("\n\n\nEnter time for timer (HH:MM:SS): ");
scanf("%d:%d:%d",&c1.hh,&c1.mm,&c1.ss);
while(c1.hh>0||c1.mm>0||c1.ss>0)
{
clrscr();
printf("The Current Time:\n");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t");
printf("%02d:%02d:%02d",c1.hh,c1.mm,c1.ss);
timer(&c1.hh,&c1.mm,&c1.ss);
delay(1000);
}
clrscr();
printf("Program Written by: Anshu Krishna\n");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t");
printf("00:00:00");
alarm();
exit(0);
}
break;

default:
{
printf("\n\tInvalid Input\n\n\tPlease restart the program");
delay(2500);exit(0);
}
}
}

OUTPUT:
Press:A: for alarm Clock
T: for Timer
Enter your Choice:A
24 hr Format(HH:MM:SS)
Enter alarm time : 22:30:50
Alarm time: 22:20:50
Current Time: 22:19:53

Alarm time reached


Press any to Exit.

Press:A: for alarm Clock


T: for Timer
Enter your Choice: T

Enter time for timer (HH:MM:SS): 22:25:20

The Current Time:


22:25:06

Press:A: for alarm Clock


T: for Timer
Enter your Choice:2
Invalid Input

Please restart the program

OUTPUT:
Thus the program using Real time operating system for implementing an alarm
clock.is written and the output is executed successfully.

You might also like