/
MoonClock.ino
1471 lines (1117 loc) · 37.4 KB
/
MoonClock.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * *
echo "# Lunar-Phase-Clock" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/an4v/Lunar-Phase-Clock.git
git push -u origin master
git remote add origin https://github.com/an4v/Lunar-Phase-Clock.git
git push -u origin master
* * * * * * * * * * * * * * * * * * * * */
/******************************************************************************
Arduino 3D printed Lunar Clock
by G4lile0
V3.0 13/11/2016
- First public release
V4.0 25/02/2017
- Status is now stored on the RTC NVRAM, this mean that after power failure, clock will return to the old status, moon mode, and alarm configuration instead of load defaul configuration.
NVRAM configuration
00 init_controlA // if init_contrl_A and B is not 16, NVRAM will load default values
01 init_controlB //
02 BRIGHTNESS // Leds Brightness
03 beep // Buttons Beep
04 menu // menu status
05 moonMode // Moon Mode
06 alarmHour // alarm hour
07 alarmMinute // alarm minute
08 alarmStatus // Alam status
09 previous moonMode before the alarm.
V4.1 26/02/2017
Added new screen/menu with bigger digital clock.
V4.2 26/02/2017
After an alarm return to the previous status after 1 min.
Fixed bug, when switching the alarm, modify the day of the month :P
V4.2b 5/03/2017
Month changed to double digit format.
OLEd Analog Clock using U8GLIB Library
// Button Long / Short Press script by:
// (C) 2011 By P. Bauermeister
// http://www.instructables.com/id/Arduino-Dual-Function-Button-Long-PressShort-Press/
// Demo colors moon animations from "FastLED "100-lines-of-code" demo reel" by Mark Kriegsman, December 2014
// Analog clock (first screen) from "OLEd Analog Clock using U8GLIB Library" by Chris Rouse Oct 2014
// moon phase calculations, algorithm adapted from Stephen R. Schmitt's by Tim Farley 18 Aug 2009
Note:: Sketch uses 99% of program storage space,
Global variables use 56% of dyamic memory,
leaving 882 bytes for local variables.
Using a IIC 128x64 OLED with SSD1306 chip
RTC DS1307
Optional Temperature Sensor TMP 36
Wire RTC:
VCC +5v
GND GND
SDA Analog pin 4
SCL Analog pin 5
Wire OLED:
VCC +5v
GND GND
SDA Analog pin 4
SCL Analog pin 5
Wire TMP36:
VCC +3.3v
GND GND
Out Analog pin 1
******************************************************************************/
// Add libraries
#include "U8glib.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"
#include "FastLED.h"
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
// How many leds in your strip?
#define NUM_LEDS 18
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 6
//#define CLOCK_PIN 50
byte BRIGHTNESS=150;
// Define the array of leds
CRGB leds[NUM_LEDS];
#define FRAMES_PER_SECOND 120
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
//SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
// blinking variables
boolean blinking = true;
unsigned long previousBlinking = 0;
const long period_Blinking = 500;
// BEEP (to enable or disable buttons BEEPs)
boolean beep = true;
#define DHTPIN 2 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
// setup u8g object
// U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK);
//
// Setup RTC
RTC_DS1307 RTC;
char monthString[37]= {"JanFebMarAprMayJunJulAugSepOctNovDec"};
String thisMonth = "";
String thisTime = "";
String alarmTime ="";
String thisDay="";
byte clockCentreX = 64; // used to fix the centre the analog clock
byte clockCentreY = 32; // used to fix the centre the analog clock
// Menu variables
byte menu = 0;
byte moonMode=0;
//temp
char buffer[16];
//moonphase variables
int y, m, d, h;
// Alarm Clock
byte alarmHour = 17; // alarm hour
byte alarmMinute = 04; // alarm minute
boolean alarmStatus = false;
boolean alarm_button_off = false ;
// Adapt these to your board and application timings:
#define BUTTON1_PIN A0 // Button 1 MENU
#define BUTTON2_PIN A1 // Button 2 UP
#define BUTTON3_PIN A2 // Button 3 DOWN
#define DEFAULT_LONGPRESS_LEN 10 // Min nr of loops for a long press
#define DELAY 20 // Delay per loop in ms
//////////////////////////////////////////////////////////////////////////////
enum { EV_NONE=0, EV_SHORTPRESS, EV_LONGPRESS };
//////////////////////////////////////////////////////////////////////////////
// Class definition
class ButtonHandler {
public:
// Constructor
ButtonHandler(int pin, int longpress_len=DEFAULT_LONGPRESS_LEN);
// Initialization done after construction, to permit static instances
void init();
// Handler, to be called in the loop()
int handle();
protected:
boolean was_pressed; // previous state
int pressed_counter; // press running duration
const int pin; // pin to which button is connected
const int longpress_len; // longpress duration
};
ButtonHandler::ButtonHandler(int p, int lp)
: pin(p), longpress_len(lp)
{
}
void ButtonHandler::init()
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // pull-up
was_pressed = false;
pressed_counter = 0;
}
int ButtonHandler::handle()
{
int event;
int now_pressed = !digitalRead(pin);
if (!now_pressed && was_pressed) {
// handle release event
if (pressed_counter < longpress_len)
event = EV_SHORTPRESS;
else
event = EV_LONGPRESS;
}
else
event = EV_NONE;
// update press running duration
if (now_pressed)
++pressed_counter;
else
pressed_counter = 0;
// remember state, and we're done
was_pressed = now_pressed;
return event;
}
//////////////////////////////////////////////////////////////////////////////
// Instanciate button objects
ButtonHandler button1(BUTTON1_PIN);
ButtonHandler button2(BUTTON2_PIN, DEFAULT_LONGPRESS_LEN*2);
ButtonHandler button3(BUTTON3_PIN);
//void print_event(const char* button_name, int event)
//{
// if (event)
// Serial.print(button_name);
// Serial.print(".SL"[event]);
// Serial.print(event);
//}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void menu0(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_profont15r);
//u8g.setFont(u8g_font_6x10);
//
//***** RTC **********
DateTime now = RTC.now();
// display date at bottom of screen
if (now.day() < 10){ thisDay=" ";} else { thisDay="";}// add leading space if required
thisDay += String(now.day(), DEC) + "/";
thisMonth="";
for (int i=0; i<=2; i++){
thisMonth += monthString[((now.month()-1)*3)+i];
}
thisDay=thisDay + thisMonth + "/";
thisDay=thisDay + String(now.year() , DEC);
const char* newDay = (const char*) thisDay.c_str();
u8g.drawStr(22,63, newDay);
// *********************
// display time in digital format
thisTime="";
if (now.hour() < 10){ thisTime=thisTime + " ";} // add leading space if required
thisTime=String(now.hour()) + ":";
if (now.minute() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.minute()) + ":";
if (now.second() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.second());
const char* newTime = (const char*) thisTime.c_str();
u8g.drawStr(10,10, newTime);
// *********************
//
// Now draw the clock face
u8g.drawCircle(clockCentreX, clockCentreY, 20); // main outer circle
u8g.drawCircle(clockCentreX, clockCentreY, 2); // small inner circle
//
//hour ticks
for( int z=0; z < 360;z= z + 30 ){
//Begin at 0° and stop at 360°
float angle = z ;
angle=(angle/57.29577951) ; //Convert degrees to radians
int x2=(clockCentreX+(sin(angle)*20));
int y2=(clockCentreY-(cos(angle)*20));
int x3=(clockCentreX+(sin(angle)*(20-5)));
int y3=(clockCentreY-(cos(angle)*(20-5)));
u8g.drawLine(x2,y2,x3,y3);
}
// display second hand
float angle = now.second()*6 ;
angle=(angle/57.29577951) ; //Convert degrees to radians
int x3=(clockCentreX+(sin(angle)*(20)));
int y3=(clockCentreY-(cos(angle)*(20)));
u8g.drawLine(clockCentreX,clockCentreY,x3,y3);
//
// display minute hand
angle = now.minute() * 6 ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x3=(clockCentreX+(sin(angle)*(20-3)));
y3=(clockCentreY-(cos(angle)*(20-3)));
u8g.drawLine(clockCentreX,clockCentreY,x3,y3);
//
// display hour hand
angle = now.hour() * 30 + int((now.minute() / 12) * 6 ) ;
angle=(angle/57.29577951) ; //Convert degrees to radians
x3=(clockCentreX+(sin(angle)*(20-11)));
y3=(clockCentreY-(cos(angle)*(20-11)));
u8g.drawLine(clockCentreX,clockCentreY,x3,y3);
//
// now add temperature if needed
//getting the voltage reading from the temperature sensor
// tempReading = analogRead(tempPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
// float voltage = tempReading * aref_voltage;
// voltage /= 1024.0;
// now print out the temperature
// int temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//float t = dht.readTemperature();
//
//char Tstr[5];
//dtostrf(dht.readTemperature(), 3,0, Tstr);
//String thisTemp1 = String(abs(dht.readTemperature())) + "C";
String thisTemp1 = String((int) dht.readTemperature()*(9/5)+32) + "F";
// printing output as follows used less program storage space
const char* thisTemp = (const char*) thisTemp1.c_str();
u8g.drawStr(90,10,thisTemp);
// the print command could be used, but uses more memory
//u8g.setPrintPos(100,10); thisMonth="";
for (int i=0; i<=2; i++){
thisMonth += monthString[((now.month()-1)*3)+i];
}
//u8g.print(thisTemp1);
//
//
}
void menu1(void) {
//***** RTC **********
DateTime now = RTC.now();
// *********************
// display time in digital format
u8g.setFont(u8g_font_osb21n);
// thisTime="";
// if (now.hour() < 10){ thisTime=thisTime + " ";} // add leading space if required
thisTime=String(now.hour()) + ":";
if (now.minute() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.minute()) + ":";
if (now.second() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.second());
const char* newTime = (const char*) thisTime.c_str();
if (now.hour() < 10){ u8g.drawStr(21,30, newTime);} else {u8g.drawStr(5,30, newTime);} // add leading space if required
u8g.setFont(u8g_font_profont15r);
alarmTime="";
if (alarmHour < 10){ alarmTime="0";} // add leading zero if required
alarmTime=alarmTime + String (alarmHour)+":";
if (alarmMinute < 10){ alarmTime=alarmTime+ "0";} // add leading zero if required
alarmTime=alarmTime + String(alarmMinute);
if (alarmStatus == 0) { alarmTime=alarmTime + " Off" ;} else { alarmTime=alarmTime + " On" ;}
const char* newalarmTime = (const char*) alarmTime.c_str();
u8g.drawStr(35,50, newalarmTime);
}
void menu11(void) {
menu1();
if (blinking) u8g.drawFrame(4, 4, 35, 30);
}
void menu12(void) {
menu1();
if (blinking) u8g.drawFrame(43, 4, 35, 30) ;
}
void menu13(void) {
menu1();
if (blinking) u8g.drawFrame(84, 4, 35, 30) ;
}
void menu14(void) {
menu1();
if (blinking) u8g.drawFrame(33, 38, 17, 16) ;
}
void menu15(void) {
menu1();
if (blinking) u8g.drawFrame(54, 38, 17, 16) ;
}
void menu16(void) {
menu1();
if (blinking) u8g.drawFrame(74, 38, 24, 16) ;
}
void menu3(void) {
// graphic commands to redraw the complete screen should be placed here
// u8g.setFont(u8g_font_profont22);
u8g.setFont(u8g_font_osb21n);
//
//***** RTC **********
DateTime now = RTC.now();
// display date at bottom of screen
if (now.day() < 10){ thisDay="_";} // add leading space if required
thisDay = String(now.day(), DEC) + "/";
// thisDay=thisDay + thisMonth + "/";
if (now.month() < 10){ thisDay=thisDay+ "0";} // add leading space if required
thisDay=thisDay + String(now.month() , DEC);
thisDay=thisDay + thisMonth + "/";
// to add only the 3rd and the 4th digit.. other option is to subtract 2000
thisDay=thisDay + String((now.year()-2000) , DEC);
// thisDay=thisDay + String(now.year()%10 + '0' , DEC);
// thisDay=thisDay + String((now.year()/10)%10 + '0', DEC);
const char* newDay = (const char*) thisDay.c_str();
if (now.day() < 10){ u8g.drawStr(18,35, newDay);} else {u8g.drawStr(2,35, newDay);} // add leading space if required
}
void menu31(void) {
menu3();
if (blinking) u8g.drawFrame(1, 10, 35, 29) ;
}
void menu32(void) {
menu3();
if (blinking) u8g.drawFrame(45, 10, 35, 29) ;
}
void menu33(void) {
menu3();
if (blinking) u8g.drawFrame(91, 10, 35, 29) ;
}
void menu2(void) {
u8g.setFont(u8g_font_osb21n);
u8g.setScale2x2();
//char Tstr[5];
//dtostrf(dht.readTemperature(), 3,0, Tstr);
//String thisTemp1 = String(abs(dht.readTemperature())) + "C";
String thisTemp1 = String((int) dht.readTemperature()*(9/5)+32);
// printing output as follows used less program storage space
const char* thisTemp = (const char*) thisTemp1.c_str();
u8g.drawStr(13,28,thisTemp);
u8g.setFont(u8g_font_profont15r);
u8g.drawStr(45,13,"o");
u8g.undoScale();
// tone (11,2000,500);
}
void menu4(void) {
u8g.setFont(u8g_font_osb21n);
u8g.setScale2x2();
//char Tstr[5];
//dtostrf(dht.readTemperature(), 3,0, Tstr);
//String thisTemp1 = String(abs(dht.readTemperature())) + "C";
String thisTemp1 = String((int) dht.readHumidity());
// printing output as follows used less program storage space
const char* thisTemp = (const char*) thisTemp1.c_str();
u8g.drawStr(13,28,thisTemp);
u8g.setFont(u8g_font_profont15r);
u8g.drawStr(46,24,"%");
u8g.undoScale();
}
void menu5(void) {
char buf[4];
u8g.drawStr( 0, 15, "Config Menu:");
u8g.drawStr( 0, 33, "Bright:"); // de 34 a 33
snprintf (buf, 4, "%d", BRIGHTNESS);
u8g.drawStr(80, 33, buf); //de 90 a 80
u8g.drawStr( 0, 48, "Beep:");
if (beep) { u8g.drawStr(80, 49, "On");} else { u8g.drawStr(80, 48, "Off"); }
u8g.drawStr( 0, 63, "Moon mode:");
switch (moonMode) {
case 0:
u8g.drawStr(80, 63, "Phase");
break;
case 1:
u8g.drawStr(80, 63, "Crep");
break;
case 2:
u8g.drawStr(80, 63, "Demo 1");
break;
case 3:
u8g.drawStr(80, 63, "Demo 2");
break;
case 4:
u8g.drawStr(80, 63, "Demo 3");
break;
case 5:
u8g.drawStr(80, 63, "Demo 4");
break;
case 6:
u8g.drawStr(80, 63, "Demo 5");
break;
case 7:
u8g.drawStr(80, 63, "Demo 6");
break;
case 8:
u8g.drawStr(80, 63, "Full");
break;
}
}
void menu51(void) {
menu5();
if (blinking) u8g.drawFrame(77, 21, 24, 14) ; //de 87 a 77
}
void menu52(void) {
menu5();
if (blinking) u8g.drawFrame(77, 37, 24, 14) ;
}
void menu53(void) {
menu5();
if (blinking) u8g.drawFrame(77, 51, 45, 13) ;
}
void menu6(void) {
u8g.setFont(u8g_font_osb21n);
u8g.setScale2x2();
DateTime now = RTC.now();
thisTime=String(now.hour()) + "";
if (now.minute() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.minute());
const char* newTime = (const char*) thisTime.c_str();
if (now.hour() < 10){ u8g.drawStr(14,30, newTime);} else {u8g.drawStr(-2,30, newTime);} // add leading space if required
u8g.setFont(u8g_font_profont15r);
if (blinking) u8g.drawStr(27,30,".");
u8g.undoScale();
}
void getPhase(int Y, int M, int D) { // calculate the current phase of the moon
double AG, IP; // based on the current date
byte phase; // algorithm adapted from Stephen R. Schmitt's
// Lunar Phase Computation program, originally
long YY, MM, K1, K2, K3, JD; // written in the Zeno programming language
// http://home.att.net/~srschmitt/lunarphasecalc.html
// calculate julian date
YY = Y - floor((12 - M) / 10);
MM = M + 9;
if(MM >= 12)
MM = MM - 12;
K1 = floor(365.25 * (YY + 4712));
K2 = floor(30.6 * MM + 0.5);
K3 = floor(floor((YY / 100) + 49) * 0.75) - 38;
JD = K1 + K2 + D + 59;
if(JD > 2299160)
JD = JD -K3;
IP = normalize((JD - 2451550.1) / 29.530588853);
AG = IP*29.53;
phase = IP*39;
// Serial.print(AG);
// Serial.println();
// Serial.print(menu);
// Serial.println();
/*
*
if(AG < 1.20369)
//phase = B00000000;
else if(AG < 3.61108)
//phase = B00000001;
else if(AG < 6.01846)
//phase = B00000011;
else if(AG < 8.42595)
//phase = B00000111;
else if(AG < 10.83323)
//phase = B00001111;
else if(AG < 13.24062)
//phase = B00011111
else if(AG < 15.64800)
//phase = B00111111;
else if(AG < 18.05539)
//phase = B00111110;
else if(AG < 20.46277)
//phase = B00111100;
else if(AG < 22.87016)
//phase = B00111000;
else if(AG < 25.27754)
//phase = B00110000;
else if(AG < 27.68493)
//phase = B00100000;
else
//phase = 0;
*/
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::Black;
}
if (phase <17) {
for( int z=0; z < (phase-2) ;z= z + 3 ){
leds[z] = CRGB::White;
leds[z+1] = CRGB::White;
leds[z+2] = CRGB::White;
// sprintf(buffer, "0 a 17 %d", (z+2));
// Serial.println(buffer);
}
}
if ((phase >=17)&&(phase <39)) {
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::White;
}
for( int z=0; z < (phase -21) ;z= z + 3 ){
leds[z] = CRGB::Black;
leds[z+1] = CRGB::Black;
leds[z+2] = CRGB::Black;
// sprintf(buffer, "18 a 39 %d", (z+2));
// Serial.println(buffer);
}
}
FastLED.show();
}
void demoMode0(void) {
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for( int i = 0; i < 8; i++) {
leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
void demoMode1(void) {
//Modo DEMO
empty();
DateTime now = RTC.now();
if (now.second()<17) {
for( int z=0; z < (now.second()) ;z= z + 3 ){
leds[z] = CRGB::White;
leds[z+1] = CRGB::White;
leds[z+2] = CRGB::White;
// sprintf(buffer, "0 a 17 %d", (z+2));
// Serial.println(buffer);
}
}
if ((now.second()>18)&&(now.second()<39)) {
full();
for( int z=0; z < (now.second()-21) ;z= z + 3 ){
leds[z] = CRGB::Black;
leds[z+1] = CRGB::Black;
leds[z+2] = CRGB::Black;
// sprintf(buffer, "18 a 39 %d", (z+2));
// Serial.println(buffer);
}
}
if (now.second()>38) {
// rainbow();
// addGlitter(80);
demoMode0();
}
FastLED.show();
}
void empty(){
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::Black;
}
}
void full(){
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::White;
}
}
void demoMode2(void) {
gHue++;
fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
addGlitter(80);
FastLED.show();
}
void demoMode3(void) {
gHue++;
// EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
// built-in FastLED rainbow, plus some random sparkly glitter
// rainbow();
// addGlitter(80);
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
// uint8_t BeatsPerMinute = 62;
// CRGBPalette16 palette = PartyColors_p;
// uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
// for( int i = 0; i < NUM_LEDS; i++) { //9948
// leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
// }
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(13,0,NUM_LEDS);
leds[pos] += CHSV( gHue, 255, 192);
FastLED.show();
}
void demoMode4() {
// eight colored dots, weaving in and out of sync with each other << esta es buena
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for( int i = 0; i < 3; i++) {
leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
dothue += 32;
}
gHue++;
FastLED.show();
}
void demoMode5()
{
rainbow();
addGlitter(80);
gHue++;
FastLED.show();
}
void demoMode6()
{
empty();
demoMode0();
FastLED.show();
}
/*
*
*
*
#define COOLING 55
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120
bool gReverseDirection = false;
void demoMode5()
{
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
CRGB color = HeatColor( heat[j]);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (NUM_LEDS-1) - j;
} else {
pixelnumber = j;
}
leds[pixelnumber] = color;
}
FastLED.show();
}
*/
void crepuscularMode(void) {
//Modo DEMO
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::Black;
}
DateTime now = RTC.now();
int secondstoalarm=0;
DateTime alarma = DateTime( now.year(), now.month(), now.day(), alarmHour, alarmMinute, 0);
secondstoalarm = alarma.unixtime()-now.unixtime();
// sprintf(buffer, "unixtime %d", (secondstoalarm));
// Serial.println(buffer);
if ((secondstoalarm>0) && (secondstoalarm<254)) {
FastLED.setBrightness(255-secondstoalarm);
for( int z=0; z < 18;z= z + 1 ){
leds[z] = CRGB::White;
} }
FastLED.show();
}
double normalize(double v) { // normalize moon calculation between 0-1
v = v - floor(v);
if (v < 0)
v = v + 1;
return v;
}
void write_NVRAM(void) {
uint8_t writeData[9] = {16,16, BRIGHTNESS,beep,menu,moonMode,alarmHour,alarmMinute,alarmStatus };
RTC.writenvram(0, writeData , 9);
}
void setup(void) {
DateTime now = RTC.now();
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2015, 2, 4, 21, 3, 0));
}
// rtc.adjust(DateTime( now.year(), now.month(), now.day(), now.hour()%24, now.minute(), now.second()));
// Serial.begin(9600);
//We give the power supply to the DTH11 using the pin 3
pinMode(2, OUTPUT);