-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalalarmclock.c
99 lines (91 loc) · 2.56 KB
/
Digitalalarmclock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define CLEAR_SCREEN() system("cls")
char buffer[9];
int hours, minutes, seconds;
HANDLE hThread;
void beeping() // To produce sound
{
int frequency = 1000; // 1000 Hz (1 kHz)
int duration = 500; // 500 milliseconds
// Play the beep sound
Beep(frequency, duration);
}
void timings() // To retrieve current time
{
time_t now; // declaration of time variable
struct tm *ltime;
time(&now);
ltime = localtime(&now);
strftime(buffer, sizeof(buffer), "%H:%M:%S", ltime);
}
DWORD WINAPI alarmtracking(LPVOID lpParam) // To ring the alarm
{
int ch, cm, cs;
while (1)
{
timings();
sscanf(buffer, "%d:%d:%d", &ch, &cm, &cs);
if (ch == hours && cm == minutes && cs == seconds)
{
printf("\nALARM TIME!\n");
beeping();
ExitThread(0); // Terminate the thread after the alarm rings
}
Sleep(1000);
}
return 0;
}
void setalarm() // for setting the alarm
{
int currenthours, currentminutes, currentseconds;
printf("\nEnter the hours, minutes, and seconds\n");
scanf("%d %d %d", &hours, &minutes, &seconds);
timings();
// Parse the time from buffer
sscanf(buffer, "%d:%d:%d", ¤thours, ¤tminutes, ¤tseconds);
if (hours < currenthours ||
(hours == currenthours && minutes < currentminutes) ||
(hours == currenthours && minutes == currentminutes && seconds < currentseconds))
{
printf("\nCannot set the alarm as the time has already passed");
exit(0);
}
else
{
printf("\nAlarm set\n");
beeping();
hThread = CreateThread(NULL, 0, alarmtracking, NULL, 0, NULL); // Start a new thread for alarm tracking
if (hThread == NULL)
{
printf("Error creating thread\n");
exit(1);
}
}
}
void digitalclock() // To show timings like a digital clock
{
while (1)
{
// Get current time
timings();
printf("\nTo set an alarm clock press escape button\n");
printf("\n\n\t\t%s", buffer); // Printing the current time
Sleep(1000);
CLEAR_SCREEN();
if (kbhit()) // when the user presses a key
{
printf("\nSetting the alarm\n");
setalarm();
}
}
}
int main()
{
digitalclock(); // To call the digital clock
WaitForSingleObject(hThread, INFINITE); // Wait for the alarm thread to finish (optional)
return 0;
}