-
Notifications
You must be signed in to change notification settings - Fork 4
/
WordClock_Main.ino
732 lines (598 loc) · 20.5 KB
/
WordClock_Main.ino
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// code includes birthday messages and also automatic daylight saving calculations.
#include <FastLED.h>
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <SoftwareSerial.h>
#include <EEPROM.h>
FASTLED_USING_NAMESPACE
#define DATA_PIN 6
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 121
#define PHOTO_RESISTOR A0
#define BUTTON_PIN 2
CRGB leds[NUM_LEDS];
SoftwareSerial BT(8, 9); // pin D8 to RXD p-in D9 purple to TXD
//Global Variables
uint8_t animate_speed = 100;
uint8_t Hour_DST;
int DST;
uint8_t colour_hue = 35;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
const byte numChars = 35;
char receivedData[numChars]; // an array to store the received data
boolean changingTime = false;
boolean addingBday = false;
boolean removingBday = false;
boolean listingBday = false;
boolean newData = false;
// arrays for the LEDs
enum wordLEDs {
ITS,
FIVE,
TEN,
QUARTER,
TWENTY,
TWENTY_FIVE,
HALF,
PAST,
TO,
OCLOCK,
HAPPY,
BIRTHDAY
};
uint8_t wordArray[][11] = {
{119,118,116,115},
{83,84,85,86},
{91,90,89},
{102,103,104,105,106,107,108},
{97,96,95,94,93,92},
{83,84,85,86,92,93,94,95,96,97},
{78,79,80,81},
{70,69,68,67},
{56,57},
{5,4,3,2,1,0},
{55,76,77,98,99},
{43,44,65,66,87,88,109,110}
};
uint8_t wordLengths[] = {4,4,3,7,6,10,4,4,2,6,5,8}; // this is the size of each of the above arrays for indexing the hourarray later in script
uint8_t hourArray[][7] = {
{59,60,61,62,63,64}, // this outlnes which LEDs to light up, 0-24 arrays, 0 being twleve 1=one 2=two... 24=twelve
{36,37,38},
{34,35,36},
{31,30,29,28,27},
{13,14,15,16},
{54,53,52,51},
{9,8,7},
{17,18,19,20,21},
{51,50,49,48,47},
{39,40,41,42},
{47,46,45},
{27,26,25,24,23,22},
{59,60,61,62,63,64},
{36,37,38},
{34,35,36},
{31,30,29,28,27},
{13,14,15,16},
{54,53,52,51},
{9,8,7},
{17,18,19,20,21},
{51,50,49,48,47},
{39,40,41,42},
{47,46,45},
{27,26,25,24,23,22},
{59,60,61,62,63,64}
};
uint8_t hourLengths[] = {6,3,3,5,4,4,3,5,5,4,3,6,6,3,3,5,4,4,3,5,5,4,3,6,6}; // this is the size of each of the above arrays for indexing the hourarray later in script
long previousMillis = 0;
long interval = 500; //how long in milli seconds to update clock
// settings to implement on startup
void setup() {
//Pin Settings
pinMode(PHOTO_RESISTOR, INPUT);// Set photo resistor
pinMode(BUTTON_PIN, INPUT); //set a button - for changing colour
//LED settings
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//RTC Clock settings
RTC.begin();
setSyncProvider(RTC.get); // the function to get the time from the RTC
// Bluetooth
BT.begin(9600);
BT.println("Connected to WordClock");
BT.println("AT+NAMEWordClock_009"); // send the command AT+NAME[name] where [name] is the name for the bluetooth
//****settings to edit the time on the real time clock ******
//**** Un-comment below 2 lines to set the time and load onto chip then comment out the lines and re-load onto the chip********
// setTime(18,50, 00, 14, 05, 2023); //this sets the system time set to GMT without the daylight saving added.
// RTC.set(now()); //sets time onto the RTC. Make sure to comment this out and then re-upload after setting the RTC. -- (setTime() and now() are Part of the Time Library)
// **** comment this out after this sketch is uploaded for the first time ****
// **** it stores the number of birthdays stored in the EEPROM
// EEPROM.write(0, 0);
// To add birthdates without bluetooth use this function, **** comment this out after loding for the first time ****
// addManualBday(26, 3); // pass the variables (day, month)
}
void loop() {
//bluetooth settings
bluetoothGetInput();
bluetoothCheckInput();
bluetoothChangeTime();
bluetoothAddBirthday();
bluetoothRemoveBirthday();
// run the Clock face LEDs every 500ms
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Clockset();
}
hourAnimation(); //hour animataion to run at full speed
brightnessSet(); // changing the brightness
if (digitalRead(BUTTON_PIN) == HIGH) {
colour_hue++;
delay(100);
}
}
void Clockset(){
// ********************* Calculate offset for Daylight saving hours UK/Europe *********************
// for UK/ Europe DST starts on last Sunday March at 2:00am until Last Sunday October at 2:00am
int MonthStartDST = 3; //dst starts in march
int MonthEndDST = 10; //dst ends in october
int TimeDSTSwitch = 2; // clocks change at 2:00am
int y = year();
int DayStartDST = calculateLastSundayInMonth(y, MonthStartDST); //find the last sunday in the month
int DayEndDST = calculateLastSundayInMonth(y, MonthEndDST); //find the last sunday in the month
SetDST(MonthStartDST, DayStartDST, MonthEndDST, DayEndDST, TimeDSTSwitch);
// ********************* Calculate offset for Daylight saving hours USA *********************
// // for USA DST starts on second Sunday March at 2:00am until First Sunday November at 2:00am
// int MonthStartDST = 3; //dst strats in march
// int MonthEndDST = 11; //dst ends in october
// int TimeDSTSwitch = 2; // clocks change at 2:00am
// int y = year();
//
// int DayStartDST = calculateFirstSundayInMonth(y, MonthStartDST) + 7; // add 7 to get the second week of the month
// int DayEndDST = calculateFirstSundayInMonth(y, MonthEndDST);
//
// SetDST(MonthStartDST, DayStartDST, MonthEndDST, DayEndDST, TimeDSTSwitch);
// *****************************************
// the first 8 seconds of the hour is a special animation if its past this time set time as normal
if (minute() == 00 && second() > 8 || minute() > 00) {
FastLED.clear (); // reset the LEDs prevents old times staying lit up
lightWordLEDs(ITS); // light up Its LEDs
if (minute() >= 5 && minute() < 10 || minute() >= 55 && minute() < 60) {
lightWordLEDs(FIVE); // if functions to set the time minutes
}
if (minute() >= 10 && minute() < 15 || minute() >= 50 && minute() < 55) {
lightWordLEDs(TEN);
}
if (minute() >= 15 && minute() < 20 || minute() >= 45 && minute() < 50 ) {
lightWordLEDs(QUARTER);
}
if (minute() >= 20 && minute() < 25 || minute() >= 40 && minute() < 45) {
lightWordLEDs(TWENTY);
}
if (minute() >= 25 && minute() < 30 || minute() >= 35 && minute() < 40) {
lightWordLEDs(TWENTY_FIVE);
}
if (minute() >= 30 && minute() < 35) {
lightWordLEDs(HALF);
}
if (minute() >= 5 && minute() < 35) { // this sets the 'past' light so it only lights up for first 30 mins
lightWordLEDs(PAST);
lightHourLEDs(Hour_DST);
}
if (minute() >= 35 && minute() < 60 && hour() <= 23) {
lightWordLEDs(TO);
lightHourLEDs(Hour_DST+1);
}
if (minute() >= 0 && minute() < 5) { // sets the 'oclock' light if the time is on the hour
lightHourLEDs(Hour_DST);
lightWordLEDs(OCLOCK);
}
//*************** light up the Happy birthday on birthdays ******************
int birthdayCount = EEPROM.read(0);
int birthdayAddress = 1;
for (int i = 0; i < birthdayCount; i++) {
int bdayDay = EEPROM.read(birthdayAddress);
int bdayMonth = EEPROM.read(birthdayAddress + 1);
if(month() == bdayMonth && day() ==bdayDay) {
lightBirthdayLEDs(HAPPY);
lightBirthdayLEDs(BIRTHDAY);
FastLED.show();
}
birthdayAddress += 2;
}
FastLED.show();
}
// **************animations for quarter to qurter past and half past the hour *******************
if(minute() == 45 && second() ==00) {
FastLED.clear ();
animateWordLEDs(ITS);
delay(500);
animateWordLEDs(QUARTER);
animateWordLEDs(TO);
delay(500);
animateHourLEDs(Hour_DST+1);
FastLED.show();
}
if(minute() == 15 && second() ==00) {
FastLED.clear ();
animateWordLEDs(ITS);
delay(500);
animateWordLEDs(QUARTER);
animateWordLEDs(PAST);
delay(500);
animateHourLEDs(Hour_DST);
FastLED.show();
}
if(minute() == 30 && second() ==00) {
FastLED.clear ();
animateWordLEDs(ITS);
delay(500);
animateWordLEDs(HALF);
animateWordLEDs(PAST);
delay(500);
animateHourLEDs(Hour_DST);
FastLED.show();
}
} //end of clock set
//******* functions to turn LEDs on ******************
void lightWordLEDs(enum wordLEDs word) {
for (uint8_t i = 0; i < wordLengths[word]; i++) {
leds[wordArray[word][i]] = CRGB::Black;
leds[wordArray[word][i]] += CHSV(colour_hue, 250, 250);
}
}
void lightHourLEDs(uint8_t Hour) {
for(uint8_t i = 0; i < hourLengths[Hour]; i++) {
leds[hourArray[Hour][i]] = CRGB::Black;
leds[hourArray[Hour][i]] += CHSV( colour_hue, 250, 250);
}
}
void lightBirthdayLEDs(enum wordLEDs word) {
for (uint8_t i = 0; i < wordLengths[word]; i++) {
leds[wordArray[word][i]] += CHSV( gHue+(i*10), 250, 250);
EVERY_N_MILLISECONDS( 1000 ) { gHue++; } //change the colour of the animation
}
}
void animateWordLEDs(enum wordLEDs word) {
for (uint8_t i = 0; i < wordLengths[word]; i++) {
leds[wordArray[word][i]] += CHSV(colour_hue, 250, 250);
FastLED.show();
delay(animate_speed);
}
}
void animateHourLEDs(uint8_t Hour) {
for(uint8_t i = 0; i < hourLengths[Hour]; i++) {
leds[hourArray[Hour][i]] += CHSV( colour_hue, 250, 250);
FastLED.show();
delay(animate_speed);
}
}
void Animation(){ /// animation to run on the hour
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 15);
uint8_t pos = random8(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 250);
EVERY_N_MILLISECONDS( 20) { gHue++; } //change the colour of the animation
delay(10);
}
void hourAnimation() {
//animation for on the hour
if(minute() == 00 && second() >= 00 && second() <=8) {
Animation();
}
if(minute() == 00 && second() == 01) { // do a 10sec animation when it hits the hour
lightWordLEDs(ITS);
FastLED.show();
}
if(minute() == 00 && second() >1 && second() <=2) {
lightWordLEDs(ITS);
lightHourLEDs(Hour_DST);
FastLED.show();
}
if(minute() == 00 && second() >2 && second() <=8) { // do a 8sec animation when it hits the hour
lightWordLEDs(ITS);
lightHourLEDs(Hour_DST);
lightWordLEDs(OCLOCK);
FastLED.show();
}
}
//******* Brightness settings to work with the light dependent resistor ******************
void brightnessSet () {
int ambiant_Light = analogRead(PHOTO_RESISTOR); // read the value of the photoresisor
int Brightness_Set = map(ambiant_Light, 0, 400, 20, 250); // map the ambiant light value to a LED brightness value
if (ambiant_Light >= 400) {
FastLED.setBrightness(250);
}
if (ambiant_Light < 400) {
FastLED.setBrightness(Brightness_Set); // set the brightness of the LEDs
}
}
// ******************* bluetooth implementation to change date and time ********************************
void bluetoothGetInput() { //take the message set by bluetooth and then add all the characters together in a char array
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (BT.available() > 0 && newData == false) { //create a char array untill you get a /n signal from bluetooth
rc = BT.read();
if (rc != endMarker) {
receivedData[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedData[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void bluetoothCheckInput() {
//If the message sent is the same as the trigger words
if (newData == true && (strcasecmp("settime",receivedData) == 0)) {
BT.println();
BT.println("Set the Time (without daylight savings time added) & Date as: hh,mm,ss,dd,mm,yyyy");
newData = false;
changingTime = true; // set a switch to true that time is going to be changed
}
if (newData == true && (strcasecmp("addbday",receivedData) == 0)){
BT.println();
BT.println("Enter birthday to add as: dd/mm");
newData = false;
addingBday = true; // set a switch to true that time is going to be changed
}
if (newData == true && (strcasecmp("removebday",receivedData) == 0)){
BT.println();
BT.println("Enter birthday to remove as: dd/mm");
newData = false;
removingBday = true; // set a switch to true that time is going to be changed
}
if (newData && strcasecmp("gettime", receivedData) == 0) {
char hoursString[3];
char minutesString[3];
char secondsString[3];
BT.println();
BT.print("Date: ");
BT.print(day());
BT.print("/");
BT.print(month());
BT.print("/");
BT.println(year());
sprintf(hoursString, "%02d", hour());
sprintf(minutesString, "%02d", minute());
sprintf(secondsString, "%02d", second());
BT.print("Time: ");
BT.print(hoursString);
BT.print(":");
BT.print(minutesString);
BT.print(":");
BT.println(secondsString);
newData = false;
}
if (newData && strcasecmp("listbday", receivedData) == 0) {
int birthdayCount = EEPROM.read(0);
int birthdayAddress = 1;
if (birthdayCount == 0) {
BT.println();
BT.print("Birthday count: ");
BT.println(birthdayCount);
} else {
BT.println();
for (int i = 0; i < birthdayCount; i++) {
int bdayMonth = EEPROM.read(birthdayAddress);
int bdayDay = EEPROM.read(birthdayAddress + 1);
BT.print("Birthday ");
BT.print(i + 1);
BT.print(": ");
BT.print(bdayMonth);
BT.print("/");
BT.println(bdayDay);
birthdayAddress += 2;
}
}
newData = false;
}
if (newData == true &&
strcasecmp("settime",receivedData) != 0 && changingTime == false &&
strcasecmp("addbday",receivedData) != 0 && addingBday == false &&
strcasecmp("removebday",receivedData) != 0 && removingBday == false &&
strcasecmp("listbday",receivedData) != 0 && listingBday == false) {
newData = false;
// if the user input isnt same as trigger word then inform user command not recognised
BT.println();
BT.println("Command not recognised: " + String(receivedData));
BT.println("Avaliable commands: ");
BT.println("settime");
BT.println("gettime");
BT.println("addbday");
BT.println("removebday");
BT.println("listbday");
}
}
void bluetoothChangeTime(){
if (newData == true && changingTime == true){ // if a new message has been recieved and the Change time switch is active change the time
char *strings[10]; //following code parses out the date based on being delimited by commas fullstops etc. this gives
char *ptr = NULL;
byte index = 0;
ptr = strtok(receivedData, " :/,."); // takes a list of delimiters
while(ptr != NULL){
strings[index] = ptr;
index++;
ptr = strtok(NULL, " :/,."); // takes a list of delimiters
}
long hr = atol(strings[0]); // take the parsed date from array which corresponds to hour minute seconds ect.
long mm = atol(strings[1]);
long ss = atol(strings[2]);
long dd = atol(strings[3]);
long mth = atol(strings[4]);
long yyyy = atol(strings[5]);
setTime(hr,mm,ss,dd,mth,yyyy); //this sets the system time set to GMT without the daylight saving added.
RTC.set(now());
BT.print("Date set as: ");
BT.print(dd);
BT.print("/");
BT.print(mth);
BT.print("/");
BT.println(yyyy);
//sprintf(hoursString, "%02d", hours);
//sprintf(minutesString, "%02d", minutes);
//sprintf(secondsString, "%02d", seconds);
BT.print("Time set as: ");
BT.print(hr);
BT.print(":");
BT.print(mm);
BT.print(":");
BT.print(ss);
BT.println(" ");
newData = false;
changingTime = false;
}
}
void bluetoothAddBirthday() {
if (newData == true && addingBday) {
char *ptr = strtok(receivedData, "/");
int month = -1;
int day = -1;
if (ptr) {
day = atoi(ptr);
ptr = strtok(NULL, "/");
if (ptr) {
month = atoi(ptr);
}
}
if (month >= 1 && month <= 12 && day >= 1 && day <= 31) {
int birthdayCount = EEPROM.read(0);
int birthdayAddress = (birthdayCount * 2) + 1;
EEPROM.write(birthdayAddress, day);
EEPROM.write(birthdayAddress + 1, month);
EEPROM.write(0, birthdayCount + 1);
BT.print("Added birthday: ");
BT.print(day);
BT.print("/");
BT.println(month);
} else {
BT.print("Invalid birthday: ");
BT.print(String(receivedData));
}
newData = false;
addingBday = false;
}
}
void bluetoothRemoveBirthday() {
if (newData == true && removingBday) {
char *ptr = strtok(receivedData, "/");
int month = -1;
int day = -1;
if (ptr) {
day = atoi(ptr);
ptr = strtok(NULL, "/");
if (ptr) {
month = atoi(ptr);
}
}
if (month >= 1 && month <= 12 && day >= 1 && day <= 31) {
int birthdayCount = EEPROM.read(0);
int birthdayAddress = 1;
bool birthdayFound = false;
for (int i = 0; i < birthdayCount; i++) {
if (birthdayFound) {
EEPROM.write(birthdayAddress - 2, EEPROM.read(birthdayAddress));
EEPROM.write(birthdayAddress - 1, EEPROM.read(birthdayAddress + 1));
} else if (EEPROM.read(birthdayAddress) == day && EEPROM.read(birthdayAddress + 1) == month) {
birthdayFound = true;
}
birthdayAddress += 2;
}
if (birthdayFound) {
EEPROM.write(0, birthdayCount - 1);
BT.print("Removed birthday: ");
BT.print(day);
BT.print("/");
BT.println(month);
} else {
BT.print("Birthday not found: ");
BT.print(String(receivedData));
}
}
newData = false;
removingBday = false;
}
}
void addManualBday(int day, int month) {
int birthdayCount = EEPROM.read(0);
int birthdayAddress = (birthdayCount * 2) + 1;
EEPROM.write(birthdayAddress, day);
EEPROM.write(birthdayAddress + 1, month);
EEPROM.write(0, birthdayCount + 1);
}
void SetDST(int MonthStartDST, int DayStartDST, int MonthEndDST, int DayEndDST, int TimeDSTSwitch) {
// *********** Test if time is between DST start and end dates and set DST *********
if ((month() > MonthStartDST && month() < MonthEndDST) || (month() == MonthStartDST && day() >= DayStartDST) || (month() == MonthEndDST && day() <= DayEndDST)) {
// Check if the time is after 2am
DST = 1;
if (month() == MonthStartDST && day() == DayStartDST){
if (hour() < TimeDSTSwitch) {
DST = 0;
} else {
DST = 1; }
}
if (month() == MonthEndDST && day() == DayEndDST){
if (hour() < TimeDSTSwitch) {
DST = 1;
} else {
DST = 0; }
}
} else {
DST = 0;
}
Hour_DST = hour()+DST; //Add the DST to the hour to get correct DST
if(hour() == 23 && DST == 1) {
Hour_DST = 00;
}
}
// these two function use the Zeller Congruence to calculate the date of the Last or first sunday of a given month for a given year.
int calculateLastSundayInMonth(int year, int month) {
int d; // Number of days in the month
if (month == 2) {
// February
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
d = 29; // Leap year
} else {
d = 28; // Non-leap year
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
// April, June, September, November
d = 30;
} else {
// January, March, May, July, August, October, December
d = 31;
}
int K = year % 100;
int J = year / 100;
int w = (d + 13 * (month + 1) / 5 + K + K / 4 + J / 4 + 5 * J) % 7;
int diff = w - 1; // Calculate the difference from 0 (Sunday)
int lastSunday = d - diff;
return lastSunday;
}
int calculateFirstSundayInMonth(int year, int month) {
if (month == 1) {
month = 13;
year = year - 1;
}
if (month == 2) {
month = 14;
year = year - 1;
}
int d = 1;
int K = year % 100;
int J = year / 100;
int w = (d + 13 * (month + 1) / 5 + K + K / 4 + J / 4 + 5 * J) % 7;
d = ((w + 5) % 7) + 1; // Convert so Monday=1, Sunday=7
int firstSunday;
if (d == 7) {
int firstSunday = 1;
} else {
int firstSunday = 8 - d;
}
return firstSunday;
}