// 7segment clock by Acmecorporation v.2.1.5 ARDUINO NANO VERSION #include #include #include //http://github.com/JChristensen/DS3232RTC #include //http://www.arduino.cc/playground/Code/Time #include //http://arduino.cc/en/Reference/Wire (included with Arduino IDE) #include #include #include // for first time run there could be random value stored inside eprom addresses from 0 to 6 #define buttonPin 6 // momentary button #define interruptPin 2 //connected to DS3231 SQW output #define NUMDIGITS 4 #define PIN 5 // WS2812 #define LIGHTSENSOR A6 // connect lightsensor to D3 and A6 - connect A6 to GND with 10KOhm resistor #define NUMPIXELS 30 #define buttonPinB 8 #define ldrPin 3 segment_display D = segment_display(NUMDIGITS, PIN); Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); TimeChangeRule myDST = {"EDT", Last, Sun, Mar, 2, 60}; //Daylight time = UTC +2 hours TimeChangeRule mySTD = {"EST", Last, Sun, Oct, 2, 0}; //Standard time = UTC +1 hours Timezone myTZ(myDST, mySTD); TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev uint32_t myColor; char buf[4]; char bufhour[2]; char bufmin[2]; byte luce = 0; // auto dim variable byte mode = EEPROM.read(5); // pre-setted transistion animation uint32_t red = pixels.Color(255,0,0); uint32_t green = pixels.Color(0,255,0); uint32_t blue = pixels.Color(0,0,255); uint32_t yellow = pixels.Color(255,255,0); uint32_t white = pixels.Color(255,255,255); uint32_t pink = pixels.Color(255,0,100); uint32_t cyan = pixels.Color(0,255,255); uint32_t orange = pixels.Color(230,80,0); uint32_t colors[] = {red, green, blue, yellow, white, pink, cyan, orange}; int myarray[30]; byte index = 0; byte ind[4]; byte inde=0; int n; byte gotosleep = EEPROM.read(1); // this variable allow to Arduino go into sleep mode at specified time /////////////////{ red, green, blue, yellow, white, pink, cyan, orange}; int colist[8][3]={{255,0,0},{0,255,0},{0,0,255},{255,255,0},{255,255,255},{255,0,100},{0,255,255},{230,80,0}}; byte morningcolor = EEPROM.read(2); byte afternooncolor = EEPROM.read(3); byte eveningcolor = EEPROM.read(4); byte colR; byte colG; byte colB; byte timeformat = EEPROM.read(6); void setup() { Serial.begin(9600); for (int r = 0; r < 6 ; r++){ Serial.println(EEPROM.read(r)); } setSyncProvider(RTC.get); RTC.squareWave(SQWAVE_NONE); RTC.setAlarm(ALM1_MATCH_HOURS, 0, 0, EEPROM.read(0), 0); RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared RTC.alarmInterrupt(ALARM_1, true); RTC.alarm(ALARM_2); RTC.alarmInterrupt(ALARM_2, false); pinMode(interruptPin, INPUT_PULLUP); // to set correct time uncomment these two lines // setTime(15, 16, 0, 11, 6, 2020); // RTC.set(now()); D.begin(); pixels.begin(); pinMode(buttonPin, INPUT_PULLUP); pinMode(buttonPinB, INPUT_PULLUP); pinMode(ldrPin, OUTPUT); digitalWrite(ldrPin, HIGH); delay(2000); if (!digitalRead(buttonPin)){ SetUpMenu(); } } void(* resetFunc) (void) = 0; //declare reset function @ address 0 void loop() { luce = map(analogRead(LIGHTSENSOR), 0, 1023, 5, 255); // read the input pin Serial.print("LIGHT SENSOR "); Serial.println(luce); Serial.println(second()); if (!digitalRead(buttonPin)){ mode++; matrixing(); newrandom(); if (mode>7){ mode=0; } Serial.print(" MODE SAVED :"); Serial.println(mode); EEPROM.write(5, mode); } time_t utc = now(); time_t local = myTZ.toLocal(utc, &tcr); if (timeformat){ if (hour(local)>12){ int format = hour(local); format -=12; sprintf(buf, "%.2d%.2d", format, minute(local)); Serial.print(" 12h "); Serial.println(format); } else{ Serial.print(" 12h "); Serial.println(hour(local)); sprintf(buf, "%.2d%.2d", hour(local), minute(local)); } } else{ Serial.print(" 24h "); Serial.println(hour(local)); sprintf(buf, "%.2d%.2d", hour(local), minute(local)); } n = atoi(buf); if (hour(local) == gotosleep) { deletedisplay(); Going_To_Sleep(); } // switch between transitions switch (mode){ case 1: while (second() == 0){ matrixing(); newrandom(); } break; case 2: while (second() == 0){ matrixing(); newrandomsingle(); } break; case 3: while (second() == 0){ matrixing(); newrandomflashing(); } break; case 4: while (second() == 0) { rainbow(12); } break; case 5: while (second() == 0){ randomcolstatic (); } break; case 6: while (second() == 0) { randomcol (); } break; case 7: while (second() == 0) { randomlights(); } break; } RTC.alarm(ALARM_1); // two dots on pixels.setPixelColor(28, pixels.Color(luce, luce, luce)); pixels.setPixelColor(29, pixels.Color(luce, luce, luce)); pixels.show(); // show current time printDateTime(local, tcr -> abbrev); delay(900); // two dots off pixels.setPixelColor(28, pixels.Color(0, 0, 0)); pixels.setPixelColor(29, pixels.Color(0, 0, 0)); pixels.show(); delay(100); } ///////////////// show current time function //////////////////// void printDateTime(time_t t, const char *tz) { Serial.println(buf); Serial.println(n); if (n >= 0 && n < 1200) { colR=map(luce, 0, 1023,0,colist[morningcolor][0]); colG=map(luce, 0, 1023,0,colist[morningcolor][1]); colB=map(luce, 0, 1023,0,colist[morningcolor][2]); } else if (n >= 1200 && n < 1700) { colR=map(luce, 0, 1023,0,colist[afternooncolor][0]); colG=map(luce, 0, 1023,0,colist[afternooncolor][1]); colB=map(luce, 0, 1023,0,colist[afternooncolor][2]); } else { colR=map(luce, 0, 1023,0,colist[eveningcolor][0]); colG=map(luce, 0, 1023,0,colist[eveningcolor][1]); colB=map(luce, 0, 1023,0,colist[eveningcolor][2]); } myColor = D.color(colR, colG, colB); D.write(buf, myColor); } ////////////////////// Transitions /////////////////////// void newrandom(){ int is; int startmillis = millis(); randomize (myarray, inde); Serial.print("RAND "); pixels.clear(); for (is - 0; is < inde; is++) { Serial.print(myarray[is]); Serial.print(","); pixels.setPixelColor(myarray[is], colors[random(8)]); pixels.show(); delay(70); } int endmillis = millis(); if (endmillis - startmillis < 1000){ delay(startmillis + 1000 - endmillis); } Serial.print("MYARRAY="); Serial.println(); for(int h=0;h<4;h++){ ind[h]=0; } index=0; inde=0; } void newrandomsingle(){ int is; int startmillis = millis(); randomize (myarray, inde); Serial.print("RAND "); pixels.clear(); for (is - 0; is < inde; is++) { Serial.print(myarray[is]); Serial.print(","); pixels.setPixelColor(myarray[is], pixels.Color(colR, colG, colB)); pixels.show(); delay(25); } int endmillis = millis(); if (endmillis - startmillis < 1000){ delay(startmillis + 1000 - endmillis); } Serial.print("MYARRAY="); Serial.println(); for(int h=0;h<4;h++){ ind[h]=0; } index=0; inde=0; } void newrandomflashing(){ int is; randomize (myarray, inde); Serial.print("RAND "); pixels.clear(); for (is - 0; is < inde; is++) { Serial.print(myarray[is]); Serial.print(","); pixels.setPixelColor(myarray[is], colors[random(8)]); pixels.show(); delay(70); pixels.clear(); } Serial.print("MYARRAY []="); Serial.println(); for(int h=0;h<4;h++){ ind[h]=0; } index=0; inde=0; } void rainbow(uint8_t wait) { uint16_t i, j; for (j = 0; j < 256; j++) { int val3 = second(); myColor = Wheel((j) & 255); D.write(buf, myColor); delay(wait); if (val3 != second()) { pixels.setPixelColor(28, pixels.Color(0, 0, 0)); pixels.setPixelColor(29, pixels.Color(0, 0, 0)); pixels.show(); delay(100); } pixels.setPixelColor(28, pixels.Color(luce, luce, luce)); pixels.setPixelColor(29, pixels.Color(luce, luce, luce)); pixels.show(); } } uint32_t Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if (WheelPos < 85) { return D.color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return D.color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return D.color(WheelPos * 3, 255 - WheelPos * 3, 0); } void randomcolstatic () { pixels.clear(); for (int r = 0; r < 30; r++) { int q = random(30); pixels.setPixelColor(q, colors[random(8)]); pixels.show(); delay(30); } pixels.clear(); } void randomcol () { pixels.clear(); for (int r = 0; r < 30; r++) { int q = random(30); pixels.setPixelColor(q, colors[random(8)]); pixels.show(); delay(30); pixels.clear(); } } void randomlights () { pixels.clear(); for (int r = 0; r < 30; r++) { int q = random(30); pixels.setPixelColor(q, pixels.Color(colR, colG, colB)); pixels.show(); delay(30); pixels.clear(); } } // swapping segments void swap (int *a, int *b){ int temp = *a; *a = *b; *b = temp; } //////////////// randomize segments ///////////////////// void randomize ( int myarray[], int n ) { // Use a different seed value so that we don't get same // result each time we run this program randomSeed(analogRead(A7)); // Start from the last element and swap one by one. We don't // need to run for the first element that's why i > 0 for (int is = n-1; is > 0; is--) { long j = random(0, n); swap(&myarray[is], &myarray[j]); } } // Arduino sleep function void Going_To_Sleep() { pixels.clear(); sleep_enable(); attachInterrupt(0, wakeUp, LOW); set_sleep_mode(SLEEP_MODE_PWR_DOWN); delay(1000); sleep_cpu(); //Serial.println("just woke up!"); setSyncProvider(RTC.get); } ////////////// Arduino wake-up function ///////////////// void wakeUp() { //Serial.println("interrupt fired"); sleep_disable(); detachInterrupt(0); } /////////////// current time "matrixing" function ////////////////// void matrixing() { Serial.print("BUF..: "); Serial.println(n); int Bones = (n%10); int Btens = ((n/10)%10); int Bhundreds = ((n/100)%10); int Bthousands = (n/1000); Serial.print("NEW"); Serial.print(Bthousands); Serial.print(Bhundreds); Serial.print(Btens); Serial.print(Bones); int buf[]={Bthousands,Bhundreds,Btens,Bones}; for(int t=0;t<4;t++){ int d = t*7; switch (buf[t]){ case 1: myarray[inde]=3+d; myarray[inde+1]=6+d; index=2; break; case 2: myarray[inde]=0+d; myarray[inde+1]=1+d; myarray[inde+2]=2+d; myarray[inde+3]=3+d; myarray[inde+4]=4+d; index=5; break; case 3: myarray[inde]=0+d; myarray[inde+1]=2+d; myarray[inde+2]=3+d; myarray[inde+3]=4+d; myarray[inde+4]=6+d; index=5; break; case 4: myarray[inde]=2+d; myarray[inde+1]=3+d; myarray[inde+2]=5+d; myarray[inde+3]=6+d; index = 4; break; case 5: myarray[inde]=0+d; myarray[inde+1]=2+d; myarray[inde+2]=4+d; myarray[inde+3]=5+d; myarray[inde+4]=6+d; index = 5; break; case 6: myarray[inde]=0+d; myarray[inde+1]=1+d; myarray[inde+2]=2+d; myarray[inde+3]=4+d; myarray[inde+4]=5+d; myarray[inde+5]=6+d; index = 6; break; case 7: myarray[inde]=3+d; myarray[inde+1]=4+d; myarray[inde+2]=6+d; index = 3; break; case 8: myarray[inde]=0+d; myarray[inde+1]=1+d; myarray[inde+2]=2+d; myarray[inde+3]=3+d; myarray[inde+4]=4+d; myarray[inde+5]=5+d; myarray[inde+6]=6+d; index = 7; break; case 9: myarray[inde]=0+d; myarray[inde+1]=2+d; myarray[inde+2]=3+d; myarray[inde+3]=4+d; myarray[inde+4]=5+d; myarray[inde+5]=6+d; index = 6; break; case 0: myarray[inde]=0+d; myarray[inde+1]=1+d; myarray[inde+2]=3+d; myarray[inde+3]=4+d; myarray[inde+4]=5+d; myarray[inde+5]=6+d; index = 6; break; } ind[t]=index; inde = ind[0]+ind[1]+ind[2]+ind[3]; Serial.print(" index "); Serial.print(inde); } Serial.print(" index "); Serial.print(inde); Serial.println(); } void SetUpMenu(){ Serial.println(); byte doublezero=0; sprintf(buf, "%.2d%.2d", EEPROM.read(0),doublezero); //Serial.print("BUFFER : "); //Serial.println(buf); Serial.println("SETUP MENU"); byte setdisplay []= {0,2,4,5,6,7,8,9,11,12,17,18,20,25}; for (byte aa=0;aa<14;aa++){ pixels.setPixelColor(setdisplay[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(3000); deletedisplay(); boolean jump=true; Serial.println("SET WAKEUP TIME IN WINTER TIME FORMAT"); // set clock wakeup hour // winter time byte wakeup=EEPROM.read(0); byte setdisplaystart []= {0,2,4,5,6,7,8,9,12,15,16,21,22,23,26}; for (byte aa=0;aa<15;aa++){ pixels.setPixelColor(setdisplaystart[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ if (!digitalRead(buttonPinB)){ wakeup++; delay(200); if (wakeup>23){ wakeup=0; } } sprintf(buf, "%.2d%.2d", wakeup,doublezero); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ EEPROM.write(0, wakeup); jump=false; } } delay(2000); buttontransition(); Serial.print("WAKEUP :"); Serial.println(EEPROM.read(0)); jump=true; // set clock stop hour Serial.println("SET SHUTDOWN TIME"); byte sleep=EEPROM.read(1); byte setdisplaystop []= {0,2,4,5,6,7,8,9,12,14,15,16,20,22,23,24,25,26}; for (byte aa=0;aa<18;aa++){ pixels.setPixelColor(setdisplaystop[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ if (!digitalRead(buttonPinB)){ sleep++; delay(200); if (sleep>23){ sleep=0; } } sprintf(buf, "%.2d%.2d", sleep,doublezero); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ EEPROM.write(1, sleep); jump=false; } } delay(2000); buttontransition(); Serial.print("SLEEP :"); Serial.println(EEPROM.read(1)); jump=true; // color byte setdisplaycolor []= {0,1,4,5,7,8,10,11,12,13,14,15,19}; for (byte aa=0;aa<13;aa++){ pixels.setPixelColor(setdisplaycolor[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); // set morningcolor Serial.println("SET MORNING COLOR"); morningcolor = EEPROM.read(2); while (jump){ if (!digitalRead(buttonPinB)){ morningcolor++; delay(200); if (morningcolor>7){ morningcolor=0; } } D.write("0700", colors[morningcolor]); Serial.println(String(morningcolor)); if (!digitalRead(buttonPin)){ Serial.print("MORNING COLOR IS :"); Serial.println(morningcolor); EEPROM.write(2, morningcolor); jump=false; } } delay(2000); buttontransition(); jump=true; // set afternooncolor Serial.println("SET AFTERNOON COLOR"); afternooncolor = EEPROM.read(3); while (jump){ if (!digitalRead(buttonPinB)){ afternooncolor++; delay(200); if (afternooncolor>7){ afternooncolor=0; } } D.write("1200", colors[afternooncolor]); Serial.println(String(afternooncolor)); if (!digitalRead(buttonPin)){ Serial.print("AFTERNOON COLOR IS :"); Serial.println(afternooncolor); EEPROM.write(3, afternooncolor); jump=false; } } delay(2000); buttontransition(); jump=true; // set eveningcolor Serial.println("SET EVENING COLOR"); eveningcolor = EEPROM.read(4); while (jump){ if (!digitalRead(buttonPinB)){ eveningcolor++; delay(200); if (eveningcolor>7){ eveningcolor=0; } } D.write("1800", colors[eveningcolor]); Serial.println(String(eveningcolor)); if (!digitalRead(buttonPin)){ Serial.print("EVENING COLOR IS :"); Serial.println(eveningcolor); EEPROM.write(4, eveningcolor); jump=false; } } delay(2000); buttontransition(); jump=true; // set day Serial.println("SET DAY"); byte giorno=day(); byte setdisplayday []= {0,1,2,3,4,6,7,8,9,10,11,13,14,16,17,19,20}; for (byte aa=0;aa<17;aa++){ pixels.setPixelColor(setdisplayday[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ //Serial.println(giorno); if (!digitalRead(buttonPinB)){ giorno++; delay(200); if (giorno>31){ giorno=1; } } sprintf(buf, "%.2d%.2d", doublezero,giorno); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ Serial.print("DAY IS :"); Serial.println(giorno); jump=false; } } delay(2000); buttontransition(); jump=true; // set month Serial.println("SET MONTH"); byte mese=month(); byte setdisplaymonth []= {1,3,4,5,10,11,12,13,14,15,17,18,19,20,22,24,25,26,27}; for (byte aa=0;aa<19;aa++){ pixels.setPixelColor(setdisplaymonth[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ //Serial.println(mese); if (!digitalRead(buttonPinB)){ mese++; delay(200); if (mese>12){ mese=1; } } sprintf(buf, "%.2d%.2d", doublezero,mese); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ Serial.print("MONTH IS :"); Serial.println(mese); jump=false; } } delay(2000); buttontransition(); jump=true; // set year Serial.println("SET YEAR"); int anno=year(); byte setdisplayear []= {0,2,3,5,6,7,8,9,10,11,12,14,15,16,17,18,20,22,23}; for (byte aa=0;aa<19;aa++){ pixels.setPixelColor(setdisplayear[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ //Serial.println(anno); if (!digitalRead(buttonPinB)){ anno++; delay(200); if (anno>2030){ anno=2020; } } D.write(String(anno), orange); Serial.println(String(anno)); if (!digitalRead(buttonPin)){ Serial.print("YEAR IS :"); Serial.println(anno); jump=false; } } delay(2000); buttontransition(); jump=true; // set TIME FORMAT Serial.println("SET TIME FORMAT"); timeformat = EEPROM.read(6); while (jump){ if (!digitalRead(buttonPinB)){ timeformat++; delay(200); if (timeformat>1){ timeformat=0; } } if (timeformat){ D.write(String(timeformat+11), orange); } else{ D.write(String(timeformat+24), orange); } Serial.println(timeformat); if (!digitalRead(buttonPin)){ Serial.print("TIME FORMAT 12h IS TRUE/FALSE :"); Serial.println(timeformat); EEPROM.write(6, timeformat); jump=false; } } delay(2000); buttontransition(); jump=true; // set hour (non daylight) Serial.println("SET HOUR WINTER TIME FORMAT"); time_t utc = now(); byte orasolare=hour(utc); byte setdisplayhour []= {1,2,5,6,7,8,9,13,14,15,20,22,23}; for (byte aa=0;aa<13;aa++){ pixels.setPixelColor(setdisplayhour[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ if (!digitalRead(buttonPinB)){ orasolare++; delay(200); if (orasolare>23){ orasolare=0; } } sprintf(buf, "%.2d%.2d", orasolare,doublezero); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ Serial.print("HOUR IS :"); Serial.println(orasolare); jump=false; } } delay(2000); buttontransition(); jump=true; // set minute Serial.println("SET MINUTE"); byte minutosolare=minute(utc); byte setdisplaymin []= {1,3,4,5,10,11,12,13,15,19,22,24,25,26,27}; for (byte aa=0;aa<15;aa++){ pixels.setPixelColor(setdisplaymin[aa], pixels.Color(255, 255, 255)); pixels.show(); } delay(2000); deletedisplay(); while (jump){ if (!digitalRead(buttonPinB)){ minutosolare++; delay(200); if (minutosolare>59){ minutosolare=0; } } sprintf(buf, "%.2d%.2d", orasolare,minutosolare); D.write(buf, orange); Serial.println(buf); if (!digitalRead(buttonPin)){ Serial.print("MINUTE IS :"); Serial.println(minutosolare); jump=false; } } delay(2000); buttontransition(); setTime(orasolare, minutosolare, 0, giorno, mese, anno); RTC.set(now()); delay(2000); Serial.print("CLOCK UPDATED : "); Serial.print(hour()); Serial.print(":"); Serial.println(minute()); Serial.print(day()); Serial.print(' '); Serial.print(month()); Serial.print(' '); Serial.println(year()); Serial.println("resetting"); delay(1000); resetFunc(); //call reset delay(1000); Serial.println("never happens"); } void deletedisplay(){ for (byte aa=0;aa<30;aa++){ pixels.setPixelColor(aa, pixels.Color(0, 0, 0)); pixels.show(); } } void buttontransition(){ deletedisplay(); for (byte aa=0;aa<28;aa++){ pixels.setPixelColor(aa, pixels.Color(255, 255, 255)); pixels.show(); delay(20); } deletedisplay(); }