Ancora bruttino ma più leggibile rispetto alla prima versione e sopratutto sembra funziona. C'è da sistemare ancora la sincronizzazione del tempo per premere i pulsanti ma gira. Ho creato in un altro tab le funzioni delle videate di alcuni menù per rendere più leggibile il codice ed ho inserito anche l'RTC.
Mi piacerebbe migliorare il ritorno nei menu creando una funzione che mi riporta al numero del menù sopra .... sicuramente cìè la soluzione ma per il momento mi sfugge eppure non sono distante.
Visto che il progetto per acquari sembra molto richiesto posto il codice, suggerimenti e critiche sono obbligatorie.
/*============================================================================== Variabili Pulsanti =============================================================================*/
/*===============================================================================================================================================================================*/
/*========================================================================== Variabili di temporizzazione =======================================================================*/
int keyInput, livello, menu;
long tempo_btn = 0, debounce = 500, tempo_men = 0, ritorno; //Tempo tra due letture successive di un bottone (500 ms)
// DS1307 funzioni ora
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
int mil_time, fullYear, phase, fase;
long YY, MM, K1, K2, K3, JD;
double AG, IP;
/*===============================================================================================================================================================================*/
/*========================================================================== Variabili di inclusione ============================================================================*/
#include <Wire.h>
#include <LCDi2cR.h> // robot-electronics.co.uk
LCDi2cR lcd = LCDi2cR(4,40,0x63,0);
#define DS1307_I2C_ADDRESS 0x68
#define VERSION "1.00"
#include <OneWire.h>
/*===============================================================================================================================================================================*/
/*========================================================================== configurazione rtc ============================================================================*/
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
void setup()
{
lcd.clear();
lcd.init(); // Init the display, clears the display
lcd.setCursor(0,5);
lcd.print("Open Reef"); // Show Version
lcd.setCursor(1,8);
lcd.print (VERSION);
keyInput=40;
livello=0;
menu=0;
ritorno=0;
delay(5000);
} // setup()...
void loop()
{
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
mil_time = (hour *100) + minute;
fullYear = (2000 + year);
keyInput=lcd.keypad();
if (menu == 0 && keyInput == 20)
{
menu = 1;
}
if (menu == 0 && keyInput ==40)
{
menu = 40;
}
switch (menu)
{
case 40:
//Schermata ciclo sistema
lcd.clear();
circle();
menu = 0;
delay(100);
break;
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MENU PRINCIPALE ");
lcd.setCursor(1,0);
lcd.print("1 REGOLAZIONI");
lcd.setCursor(2,0);
lcd.print("2 ACCENSIONI ");
lcd.setCursor(3,0);
lcd.print("3 TARATURA ");
keyInput=lcd.keypad();
if(keyInput == 1)
{
menu = 11;
tempo_btn = millis();
}
if(keyInput == 2)
{
menu = 12;
tempo_btn = millis();
}
if(keyInput == 3)
{
menu = 13;
tempo_btn = millis();
}
if(keyInput == 30)
{
menu = 0;
tempo_btn = millis();
}
delay(100);
break;
case 11:
//Schermata menu regolazioni
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MENU REGOLAZIONI ");
lcd.setCursor(1,0);
lcd.print("1 DATA E ORA ");
lcd.setCursor(2,0);
lcd.print("2 VARIABILI ");
lcd.setCursor(3,0);
lcd.print(tempo_btn);
ritorno = millis();
keyInput=lcd.keypad();
if(keyInput == 1 && millis() - tempo_btn > debounce)
{
menu = 21;
tempo_btn = millis();
}
if(keyInput == 2 && millis() - tempo_btn > debounce)
{
menu = 22;
tempo_btn = millis();
}
if(keyInput == 30 && millis() - tempo_btn > debounce)
{
menu = 1;
tempo_btn = millis();
}
delay(100);
break;
case 12:
//Schermata menu principale
noMenu();
if(keyInput == 30 && millis() - tempo_btn > debounce)
{
menu = 1;
tempo_btn = millis();
}
break;
case 13:
//Schermata menu principale
noMenu();
if(keyInput == 30 && millis() - tempo_btn > debounce)
{
menu = 1;
tempo_btn = millis();
}
break;
case 21:
//Schermata menu principale
noMenu();
if(keyInput == 30 && millis() - tempo_btn > debounce)
{
menu = 11;
tempo_btn = millis();
}
break;
case 22:
//Schermata menu principale
noMenu();
if(keyInput == 30 && millis() - tempo_btn > debounce)
{
menu = 11;
tempo_btn = millis();
}
break;
} //switch...
} //loop()...
Il codice di alcuni menù invece è qui.
void noMenu ()
{
lcd.clear();
lcd.setCursor(1,6);
lcd.print("MENU ");
lcd.print(menu);
lcd.setCursor(2,3);
lcd.print("NON DISPONIBILE");
delay(300);
}
void menu1 ()
{
lcd.clear();
//Print data and hour
lcd.clear();
lcd.setCursor(0,0);
if(dayOfMonth < 10)
lcd.print('0');
lcd.print(dayOfMonth, DEC);
lcd.print("/");
if(month < 10)
lcd.print('0');
lcd.print(month, DEC);
lcd.print("/");
lcd.print(fullYear, DEC);
//Serial.print("Militar ");
//Serial.print(mil_time, DEC);
//Serial.print(" Fase Lunare ");
//Serial.println(fase, DEC);
lcd.setCursor(0,12);
if(hour < 10)
lcd.print('0');
lcd.print(hour, DEC);
lcd.print(":");
if(minute < 10)
lcd.print('0');
lcd.print(minute, DEC);
lcd.print(":");
if(second < 10)
lcd.print('0');
lcd.print(second, DEC);
lcd.setCursor(3,0);
lcd.print("MENU 1");
delay(1000);
}
void menu2 ()
{
lcd.clear();
//Print data and hour
lcd.clear();
lcd.setCursor(0,3);
lcd.print("MENU ACCENSIONI");
lcd.setCursor(1,9);
lcd.print("ON");
lcd.setCursor(2,0);
lcd.print("12345678");
lcd.setCursor(2,12);
lcd.print("12345678");
lcd.setCursor(3,9);
lcd.print("OF");
delay(1000);
}
void circle () // Da ultimare non funziona
{
if (tempo_men + 10000 > millis ())
{
menu1 ();
}
else
{
tempo_men = millis () ;
menu2 ();
}
}