#include <AccelStepper.h>
//#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
AccelStepper stepper2(1, 44, 46); //ore (Typeof driver: with 2 pins, STEP, DIR) 1/8 passo MS1 on - MS2 on - MS3 -off
AccelStepper stepper1(1, 50, 52); //min passo intero MS1 off - MS2 off - MS3 - off
long posizioneore, posizionemin;
long second, secondsToday, secondsTodayA, secondsTodayB;
int presecond;
long int initial_homingore=-1;
long int initial_homingmin=-1;
int buttonPressCount = 0; // Conteggio dei press del pulsante
bool varser=false;
bool OraLegale=false; //Variabile che serve a non modificare l'orario ripetutamente a Ottobre
bool century = false;
bool h12;
bool PM;
bool homing;
byte year, month, day, DoW, hour, minute, seconds;
byte Year, Month, Data, DOW, Hour ,Minute, Second ;
byte Imposta=0; //Variabile ausiliaria per impostare data e ora manuale
byte Comando = 0; //variabile gestione pulsanti menu
char Messaggio [20] = "";
//RTC_DS3231 rtc;
DS3231 Clock;
/////////////////////////////////////////////////////////////////////
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
pinMode(4, INPUT); //aux min
pinMode(2, INPUT); //int ore
pinMode(3, INPUT); //int min
pinMode(51, INPUT_PULLUP);
pinMode(49, INPUT_PULLUP);
pinMode(47, INPUT_PULLUP);
pinMode(45, INPUT_PULLUP);
pinMode(43, INPUT_PULLUP);
pinMode(41, INPUT_PULLUP);
pinMode(39, INPUT_PULLUP);
pinMode(42, OUTPUT); //en min
pinMode(48, OUTPUT); //en ore
Clock.setSecond(10);//Set the second
Clock.setMinute(50);//Set the minute
attachInterrupt(digitalPinToInterrupt(2), INTERRUPTORE, FALLING);
attachInterrupt(digitalPinToInterrupt(3), INTERRUPTMIN, FALLING);
digitalWrite(42, LOW); //en min
digitalWrite(48, LOW); //en ore
home1(); //homing
}
///////////////////////////////////////////////////////////////////////
void loop() {
cout:
ReadDS3231();
if ((digitalRead(49) == LOW)&&(digitalRead(47) == LOW)) {Comando =1; lcd.backlight(); }// pulsante fn1+fn2
if (Comando == 1) {
if (digitalRead(49) == LOW) {SetOra (0); goto cout;}// pulsante FN1
if (digitalRead(47) == LOW) {SetOra (1); goto cout;}// pulsante FN2
if (digitalRead(45) == LOW) {SetOra (2); goto cout;}// pulsante FN3
if (digitalRead(43) == LOW) {SetOra (3); goto cout;}// pulsante FN4
if (digitalRead(41) == LOW) {SetOra (4); goto cout;}// pulsante verde
if (digitalRead(39) == LOW) {SetOra (5); goto cout;}// pulsante rosso
goto cout;
}
while (digitalRead(51) == LOW) {CICLORANDOM();}
if (varser==true) {varser=false; home1();
fxprint( " "," ", " ", " "); }
//qui ciclo regolare
stepper1.run();
stepper2.run();
if (second !=presecond) { //una volta al secondo
presecond=second;
secondsToday = (hour * long(3600)+ minute * long(60) + second);
secondsTodayA = secondsToday % 43200;
secondsTodayB = secondsToday % 28800;
stepper1.runToNewPosition(secondsTodayA/3 + posizioneore);
stepper2.runToNewPosition(secondsTodayB/3 + posizionemin);
sprintf(Messaggio, "%02d/%02d/%d - %02d:%02d:%02d", day, month, year, hour, minute, second);
lcd.setCursor(0, 0);
lcd.print(Messaggio);
fxprint( "","Sec_gg: "+ String(secondsToday),"Step_ore: "+String(secondsTodayA/3),"Step_min: "+ String(secondsTodayB/3));
}
// if (day==24 && month==12){ digitalWrite(31, HIGH);} //comp
// else{digitalWrite(31, LOW); }
// if (day==25 && month==12){ digitalWrite(33, HIGH);} //nat
// else{digitalWrite(33, LOW); }
// if (day==31 && month==12){ digitalWrite(35, HIGH);} //ultimo
// else{digitalWrite(35, LOW); }
}
////////////////////////////////////////////////////
void home1() {
homing=true;
ReadDS3231();
fxprint( "Homing..............","Woodstock cerca poso", " ", " ");
stepper1.setMaxSpeed(3000); // Set maximum speed value for the stepper
stepper1.setAcceleration(1000); // Set acceleration value for the stepper
stepper1.setCurrentPosition(0); // Set the current position to 0 steps
stepper2.setMaxSpeed(3000);
stepper2.setAcceleration(1000);
stepper2.setCurrentPosition(0);
secondsToday = (hour * long(3600)+ minute * long(60) + second);
secondsTodayA = secondsToday % 43200;
secondsTodayB = secondsToday % 28800;
stepper1.runToNewPosition(12800);
stepper1.run();
stepper2.runToNewPosition(9600);
stepper2.run();
delay(5000);
stepper1.setMaxSpeed(5000); // Set maximum speed value for the stepper
stepper1.setAcceleration(1000); // Set acceleration value for the stepper
stepper2.setMaxSpeed(5000);
stepper2.setAcceleration(1000);
fxprint( " "," ", " ", " ");
lcd.noBacklight();
homing=false;
}
///////////////////////////////////////////////////
void CICLORANDOM() {
varser=true;
while(digitalRead(51) == LOW){ //giri casuali
if (stepper1.distanceToGo() == 0) {
stepper1.moveTo(rand() % 5000);
//stepper1.setMaxSpeed((rand() % 3000) + 100);
stepper1.setAcceleration((rand() % 1000) + 200);
}
if (stepper2.distanceToGo() == 0){
stepper2.moveTo(rand() % 5000);
//stepper2.setMaxSpeed((rand() % 3000) + 100);
stepper2.setAcceleration((rand() % 1000) + 200);
}
stepper1.run();
stepper2.run();
}
while (stepper1.speed() != 0 || stepper2.speed() != 0) {//rallentamento fino a zero
if (stepper1.speed() != 0) {stepper1.run(); }
else {stepper1.stop();}
if (stepper2.speed() != 0) {stepper2.run(); }
else {stepper2.stop();}
}
}
///////////////////////////////////////////////7
void INTERRUPTORE() {
if (homing == false) { //passa da finecorsa ore durante normale funzionamento ferma ciclo
//togliere enable corrente motore ore stepper1
}
else{ //passa da finecorsa ore durante homing
posizioneore = stepper1.currentPosition()+7200;
initial_homingore = stepper1.currentPosition();
}
}
///////////////////////////////////////////////7///////////////////////////////////////////////////////////////////////////
void INTERRUPTMIN() {
if (digitalRead(4) == HIGH) {
if (homing == false) { //passa da finecorsa ore durante normale funzionamento ferma ciclo
//togliere enable corrente motore min stepper2
}
else{
posizionemin = stepper2.currentPosition();
initial_homingmin = stepper2.currentPosition();
}
}
}
///////////////////////////////////////////////7///////////////////////////////////////////////////////////////////////////
void ORALEGALE(){
//ANALIZZA ORA LEGALE/SOLARE -------
/*
L'Ultima domenica di Marzo le lancette passano dalle 02:00 alle 03:00 (Ora Legale)
L'ultima domenica di Ottobre le lancette passano dalle 03:00 alle 02:00 (Ora Solare)
*/
if (month==3){
if (DoW==7 && (day+7)>31){ //E' l'ultima domenica di Marzo
if (hour==2) {
Clock.setHour(3); //Si passa all'ora legale 02:00 -> 03:00
OraLegale=false;
}
}
} //FINE ORA LEGALE
if (month==10){
if (DoW==7 && (day+7)>31){ //E' l'ultima domenica di Ottobre
if (hour==3 && !OraLegale) {
Clock.setHour(2); //Si passa all'ora solare 03:00 -> 02:00
OraLegale=true; //Orario aggiornato all'ora solare
}
}
} //FINE ORA SOLARE
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void ReadDS3231() //Funzione di aggiornamento clock
{
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
day=Clock.getDate();
month=Clock.getMonth(century);
year=Clock.getYear();
DoW=Clock.getDoW();
//intemp=Clock.getTemperature();
return;
} //FINE ReadDS3231() --------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////
void SetOra(int num){ // imposta ora da tastiera
switch (Imposta) {
case 0:
void ReadDS3231();
Year = year ;
Month =month;
Data = day;
DOW = DoW;
Hour = hour;
Minute = minute;
Second = second;
fxprint( "DATA e ORA attuale: "," ", "Verde: Modifica ", "Rosso: Annulla ");
sprintf(Messaggio, "%02d/%02d/%d - %02d:%02d:%02d", day, month, year, hour, minute, second);
lcd.setCursor(0, 1); //seconda riga
lcd.print(Messaggio);
break;
case 1:
fxprint( "ANNO:? (00-99) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Year));
break;
case 2:
fxprint( "MESE:? (1-12) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Month));
break;
case 3:
fxprint( "GIORNO:? (1-31) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Data));
break;
case 4:
fxprint( "GIORNO: 1=LUN 7= DOM","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(DOW));
break;
case 5:
fxprint( "ORA:? (0-23) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Hour));
break;
case 6:
fxprint( "MINUTI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Minute));
break;
case 7:
fxprint( "SECONDI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Second));
break;
}
if (num == 0) {// FN1
}
if (num == 1) {// FN2
if (Imposta>1){
Imposta = Imposta - 1;
SetOra(10);}
}
if (num == 2) {// FN3
switch (Imposta) {
case 1:
if (Year ==0){Year = 99;}
else {Year = Year-1;}
fxprint( "ANNO:? (00-99) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Year));
break;
case 2:
if (Month ==1){Month = 12;}
else {Month = Month-1;}
fxprint( "MESE:? (1-12) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Month));
break;
case 3:
if (Data ==1){Data = 31;}
else {Data = Data-1;}
fxprint( "GIORNO:? (1-31) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Data));
break;
case 4:
if (DOW ==1){DOW = 7;}
else {DOW = DOW-1;}
fxprint( "GIORNO: 1=LUN 7= DOM","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(DOW));
break;
case 5:
if (Hour ==0){Hour = 23;}
else {Hour = Hour-1;}
fxprint( "ORA:? (0-23) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Hour));
break;
case 6:
if (Minute ==0){Minute = 59;}
else {Minute = Minute-1;}
fxprint( "MINUTI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Minute));
break;
case 7:
if (Second ==0){Second = 59;}
else {Second = Second-1;}
fxprint( "SECONDI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Second));
break;}
}
if (num == 3) {// FN4
switch (Imposta) {
case 1:
if (Year ==99){Year = 0;}
else {Year = Year+1;}
fxprint( "ANNO:? (0-99) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Year));
break;
case 2:
if (Month ==12){Month = 1;}
else {Month = Month+1;}
fxprint( "MESE:? (1-12) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Month));
break;
case 3:
if (Data ==31){Data = 1;}
else {Data = Data+1;}
fxprint( "GIORNO:? (1-31) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Data));
break;
case 4:
if (DOW ==7){DOW = 1;}
else {DOW = DOW+1;}
fxprint( "GIORNO: 1=LUN 7= DOM","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(DOW));
break;
case 5:
if (Hour ==23){Hour = 0;}
else {Hour = Hour+1;}
fxprint( "ORA:? (0-23) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Hour));
break;
case 6:
if (Minute ==59){Minute = 0;}
else {Minute = Minute+1;}
fxprint( "MINUTI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Minute));
break;
case 7:
if (Second ==59){Second = 0;}
else {Second = Second+1;}
fxprint( "SECONDI:? (0-59) ","FN3:(-) FN4:(+) ", "Verde:Ok FN2:Indiet.", String(Second));
break;
}
}
if (num == 4) {// verde
switch (Imposta) {
case 0:
Imposta = 1;
SetOra(10);
break;
case 1:
Imposta = 2;
SetOra(10);
break;
case 2:
Imposta = 3;
SetOra(10);
break;
case 3:
Imposta = 4;
SetOra(10);
break;
case 4:
Imposta = 5;
SetOra(10);
break;
case 5:
Imposta = 6;
SetOra(10);
break;
case 6:
Imposta = 7;
SetOra(10);
break;
case 7:
Clock.setSecond(Second);//Set the second
Clock.setMinute(Minute);//Set the minute
Clock.setHour(Hour); //Set the hour
Clock.setDoW(DOW); //Set the day of the week
Clock.setDate(Data); //Set the date of the month
Clock.setMonth(Month); //Set the month of the year
Clock.setYear(Year);
fxprint( "DATA e ORA impostata"," ", " ", " ");
ReadDS3231();
delay (800);
Imposta = 0;
Comando = 0;
home1();
lcd.noBacklight();
break;
}
}
if (num == 5) {// rosso
fxprint( "Nessuna modifica... "," ", " ", " ");
delay(800);
fxprint( " "," ", " ", " ");
Imposta = 0;
Comando = 0;
lcd.noBacklight();
}
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
void fxprint(String smessage1, String smessage2, String smessage3, String smessage4)
{
if (smessage1 != ""){
lcd.setCursor(0, 0); //prima riga
lcd.print(" ");
lcd.setCursor(0, 0); //prima riga
lcd.print(smessage1);}
if (smessage2 != ""){
lcd.setCursor(0, 1); //seconda riga
lcd.print(" ");
lcd.setCursor(0, 1); //seconda riga
lcd.print(smessage2);}
if (smessage3 != ""){
lcd.setCursor(0, 2); //terza riga
lcd.print(" ");
lcd.setCursor(0, 2); //terza riga
lcd.print(smessage3);}
if (smessage4 != ""){
lcd.setCursor(0, 3); //quarta riga
lcd.print(" ");
lcd.setCursor(0, 3); //quarta riga
lcd.print(smessage4);}
}