Buongiorno a tutti,
Volevo realizzare un calendario lunare con un software trovato in rete che utilizza OLED 1.3 e il modulo RTC DS1307. Entrambi i moduli sono collegati con SDA su pin A4 e SCL su A5. Già in passato avevo utilizzato un altro codice che funzionava bene con questa stessa configurazione. In pratica caricato nell'arduino non si accende L'OLED. Dopo diverse prove ho scoperto che se carico il vecchio codice funzionante e subito dopo (senza scollegare arduino dall'alimentazione) carico quello nuovo, anche quest'ultimo viene visualizzato con successo.
Di seguito allego i codici:
Vecchio codice funzionante
#include "U8glib.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C setup u8g object
RTC_DS1307 RTC;
static unsigned char full_moon_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0xff, 0x3f, 0x00,
0x80, 0xff, 0x7f, 0x00, 0xe0, 0xff, 0xff, 0x01, 0xf0, 0xff, 0xff, 0x03,
0xf0, 0xff, 0xff, 0x03, 0xf8, 0xff, 0xff, 0x07, 0xfc, 0xff, 0xff, 0x0f,
0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f, 0xfe, 0xff, 0xff, 0x1f,
0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f,
0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x1f,
0xfe, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0x0f, 0xfc, 0xff, 0xff, 0x0f,
0xfc, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x07, 0xf0, 0xff, 0xff, 0x03,
0xf0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x01, 0x80, 0xff, 0x7f, 0x00,
0x00, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00 };
static unsigned char waning_gibbous_bits[] = {
0x00, 0xf8, 0x0f, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x80, 0xff, 0xff, 0x00,
0xe0, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x7f, 0x00,
0xf8, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x1f, 0x00,
0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xff, 0xff, 0x0f, 0x00,
0xff, 0xff, 0x0f, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00,
0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x07, 0x00,
0xff, 0xff, 0x07, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x0f, 0x00,
0xfe, 0xff, 0x0f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xf8, 0xff, 0x1f, 0x00,
0xf8, 0xff, 0x3f, 0x00, 0xf0, 0xff, 0x3f, 0x00, 0xe0, 0xff, 0xff, 0x00,
0x80, 0xff, 0xff, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0x1f, 0x00 };
static unsigned char last_quarter_bits[] = {
0x00, 0xf8, 0x0f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x80, 0xff, 0x01, 0x00,
0xe0, 0xff, 0x01, 0x00, 0xf0, 0xff, 0x00, 0x00, 0xf8, 0x7f, 0x00, 0x00,
0xf8, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00,
0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00,
0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00,
0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00,
0xff, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00,
0xfe, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00,
0xf8, 0x7f, 0x00, 0x00, 0xf0, 0xff, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00,
0x80, 0xff, 0x01, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xf8, 0x0f, 0x00 };
static unsigned char crescent_old_bits[] = {
0x00, 0xf8, 0x07, 0x00, 0x00, 0xff, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00,
0xe0, 0x0f, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00,
0xf8, 0x01, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
0x3f, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
0x7e, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00,
0xf8, 0x01, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00,
0x80, 0x1f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xf8, 0x07, 0x00 };
static unsigned char new_moon_bits[] = {
0x00, 0xf8, 0x07, 0x00, 0x00, 0x07, 0x38, 0x00, 0x80, 0x00, 0x40, 0x00,
0x60, 0x00, 0x80, 0x01, 0x10, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x04,
0x08, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x10,
0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x20,
0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20,
0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x20,
0x01, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, 0x10,
0x02, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x04,
0x08, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x02, 0x60, 0x00, 0x80, 0x01,
0x80, 0x00, 0x40, 0x00, 0x00, 0x07, 0x38, 0x00, 0x00, 0xf8, 0x07, 0x00 };
static unsigned char crescent_new_bits[] = {
0x00, 0xf8, 0x07, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x7e, 0x00,
0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x07,
0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x80, 0x1f,
0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x3f,
0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f,
0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x3f,
0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x1f,
0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xe0, 0x07,
0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xfc, 0x01,
0x00, 0x00, 0x7e, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0xf8, 0x07, 0x00 };
static unsigned char first_quarter_bits[] = {
0x00, 0xfc, 0x07, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xe0, 0x7f, 0x00,
0x00, 0xe0, 0xff, 0x01, 0x00, 0xc0, 0xff, 0x03, 0x00, 0x80, 0xff, 0x07,
0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0x1f,
0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfc, 0x1f, 0x00, 0x00, 0xfc, 0x3f,
0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f,
0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f,
0x00, 0x00, 0xfe, 0x3f, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xfe, 0x1f,
0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0xff, 0x07,
0x00, 0x80, 0xff, 0x07, 0x00, 0xc0, 0xff, 0x03, 0x00, 0xe0, 0xff, 0x01,
0x00, 0xe0, 0x7f, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0xfc, 0x07, 0x00 };
static unsigned char waxing_gibbous_bits[] = {
0x00, 0xfc, 0x07, 0x00, 0x00, 0xff, 0x3f, 0x00, 0xc0, 0xff, 0x7f, 0x00,
0xc0, 0xff, 0xff, 0x01, 0x80, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0x07,
0x00, 0xff, 0xff, 0x07, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x1f,
0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x3f,
0x00, 0xfc, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f,
0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0x3f,
0x00, 0xf8, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0x1f,
0x00, 0xfc, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0x07,
0x00, 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0x03, 0xc0, 0xff, 0xff, 0x01,
0xc0, 0xff, 0x7f, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xfe, 0x07, 0x00 };
//
String nfm = ""; // days to next full moon
void setup() {
Serial.begin(9600);
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
}
// following line sets the RTC to the date & time this sketch was compiled
// un REM the line below to set clock, then re REM it
// and upload this sketch again
Wire.begin();
}
void loop() {
//draw loop
u8g.firstPage();
do {
drawMoon();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(50);
}
void drawMoon(void){
DateTime now = RTC.now();
u8g.setFont(u8g_font_profont12);
u8g.setFont(u8g_font_5x7);
u8g.drawStr(15,10, "Calcolatore Fasi Lunari");
u8g.drawLine(0,13,128,13);
int mp = moon_phase();
u8g.setFont(u8g_font_profont15);
switch (mp){
case 0:
u8g.drawStr(15,61, " Luna Piena ");
u8g.drawXBM(45,18,30,30,full_moon_bits);
break;
case 1:
u8g.drawStr(15,61, " Gibbosa Calante");
u8g.drawXBM(45,18,30,30,waning_gibbous_bits);
break;
case 2:
u8g.drawStr(15,61, " Ultimo Quarto ");
u8g.drawXBM(45,18,30,30,last_quarter_bits);
break;
case 3:
u8g.drawStr(15,61, " Luna Calante ");
u8g.drawXBM(45,18,30,30,crescent_old_bits);
break;
case 4:
u8g.drawStr(15,61, " Luna Nuova ");
u8g.drawXBM(45,18,30,30,new_moon_bits);
break;
case 5:
u8g.drawStr(15,61, " Luna Crescente ");
u8g.drawXBM(45,18,30,30,crescent_new_bits);
break;
case 6:
u8g.drawStr(15,61, " Primo Quarto");
u8g.drawXBM(45,18,30,30,first_quarter_bits);
break;
case 7:
u8g.drawStr(15,61, " Gibbosa Crescente");
u8g.drawXBM(45,18,30,30,waxing_gibbous_bits);
break;
}
const char* newNfm = (const char*) nfm.c_str();
u8g.drawStr(110,30, newNfm);
}
int moon_phase(){
// calculates the age of the moon phase(0 to 7)
// there are eight stages, 0 is full moon and 4 is a new moon
DateTime now = RTC.now();
double jd = 0; // Julian Date
double ed = 0; //days elapsed since start of full moon
int b= 0;
jd = julianDate(now.year(), now.month(), now.day());
//jd = julianDate(1972,1,1); // used to debug this is a new moon
jd = int(jd - 2244116.75); // start at Jan 1 1972
jd /= 29.53; // divide by the moon cycle
b = jd;
jd -= b; // leaves the fractional part of jd
ed = jd * 29.53; // days elapsed this month
nfm = String((int(29.53 - ed))); // days to next full moon
b = jd*8 +0.5;
b = b & 7;
return b;
}
double julianDate(int y, int m, int d){
// convert a date to a Julian Date}
int mm,yy;
double k1, k2, k3;
double j;
yy = y- int((12-m)/10);
mm = m+9;
if(mm >= 12) {
mm = mm-12;
}
k1 = 365.25 *(yy +4172);
k2 = int((30.6001 * mm) + 0.5);
k3 = int((((yy/100) + 4) * 0.75) -38);
j = k1 +k2 + d + 59;
j = j-k3; // j is the Julian date at 12h UT (Universal Time)
return j;
}
Nuovo codice che non viene visualizzato su OLED:
ONTI:
http://utenti.quipo.it/base5/scienze/etaluna.htm
http://calendario.eugeniosongia.com/epatta.htm
http://calendario.eugeniosongia.com/numerodoro.htm
http://www.marcopersichetti.it/joomla/index.php/calcolo-delle-fasi-lunari
https://it.wikipedia.org/wiki/Fasi_lunari
https://www.studiamo.it/pages/curiosita-e-tempo-libero-fasi-lunari-e-patta/
https://it.wikipedia.org/wiki/Epatta
https://books.google.it/books?id=hsU8wQCNH3UC&printsec=frontcover&hl=it&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false
*/
#include "Adafruit_SH110X.h"
#include "RTClib.h"
#include "img_fasi.h"
Adafruit_SH1106G display = Adafruit_SH1106G(128, 64, &Wire, -1);
RTC_DS1307 rtc;
char giorno_settimana[7][12] = {"Domenica", "Lunedi'", "Martedi'", "Mercoledi'", "Giovedi'", "Venerdi'", "Sabato"};
signed int anno_corrente;
byte mese_corrente;
byte giorno_solare;
byte epatta_anno;
byte epatta_mese;
byte giorno_lunare;
String fase;
void setup() {
display.begin(0x3c);
display.clearDisplay();
if (! rtc.begin()) {
Serial.println(F("La batteria dell'orologio è scarica!"));
Serial.flush();
abort();
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //NECESSARIO? BOH!
}
void loop() {
// put your main code here, to run repeatedly:
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
DateTime now = rtc.now();
anno_corrente = now.year();
mese_corrente = now.month();
giorno_solare = now.day();
display.fillRect(0, 0, 127, 15, 0); //metodo per refreshare solo una porzione di schermo (ora e data)
display.print(giorno_settimana[now.dayOfTheWeek()]);
display.print(F(" "));
char buff_day[] = "DD/MM/YYYY";
display.println(now.toString(buff_day));
display.print(F("ore "));
char buff_sec[] = "hh:mm:ss";
display.print(now.toString(buff_sec));
display.drawLine(0, 17, 127, 17, 1);
display.setCursor(0, 19);
calcolo_epatta_annuale();
display.print(F("Epatta Annuale "));
display.println(epatta_anno);
calcolo_epatta_mensile();
display.print(F("Epatta Mensile "));
display.println(epatta_mese);
calcolo_giorno_lunare();
display.print(F("Giorno Lunare "));
display.println(giorno_lunare);
display.drawLine(0, 44, 127, 44, 1);
display.setCursor(0, 51);
calcolo_fase_lunare();
display.println(fase);
display.drawLine(104, 17, 104, 63, 1);
display.setCursor(0, 46);
//immagine della Fase Lunare
if (giorno_lunare == 0) display.drawBitmap(108, 48, luna_nuova, 14, 14, 1);
else if (giorno_lunare >= 1 && giorno_lunare <= 6) display.drawBitmap(108, 48, crescente, 14, 14, 1);
else if (giorno_lunare >= 7 && giorno_lunare <= 8) display.drawBitmap(108, 48, primo_quarto, 14, 14, 1);
else if (giorno_lunare >= 9 && giorno_lunare <= 14) display.drawBitmap(108, 48, gibbosa_crescente, 14, 14, 1);
else if (giorno_lunare == 15) display.drawBitmap(108, 48, luna_piena, 14, 14, 1);
else if (giorno_lunare >= 16 && giorno_lunare <= 21) display.drawBitmap(108, 48, gibbosa_calante, 14, 14, 1);
else if (giorno_lunare >= 24 && giorno_lunare <= 29) display.drawBitmap(108, 48, calante, 14, 14, 1);
else if (giorno_lunare >= 22 && giorno_lunare <= 23) display.drawBitmap(108, 48, ultimo_quarto, 14, 14, 1);
display.display();
if (now.hour() == 0 && now.minute() == 0 && now.second() == 0) display.clearDisplay(); //ad un dato orario lo schermo si aggiorna
}
signed int calcolo_epatta_annuale() {
//CALCOLO DELL'EPATTA ANNUALE COSI' COME VENNE CORRETTA IN EPOCA GREGORIANA
byte oro = (anno_corrente % 19) + 1; //Anno del Ciclo Metoniano, corrisponde all'Anno Lunare nel suo ciclo di 19 anni
signed int a = ((oro * 11) - 10) % 30;
byte secolare = anno_corrente / 100;
byte b = secolare - 15;
signed int c = (b - (b / 25)) / 3;
signed int d = (b * 3) / 4;
signed int e = (d - c) % 30;
if ( a >= e) epatta_anno = a - e;
else if (a < e) epatta_anno = 30 - (e - a);
return epatta_anno;
}
signed int calcolo_epatta_mensile() {
//CALCOLO DELL'EPATTA MENSILE
switch(mese_corrente) {
case 1: epatta_mese = 0;
break;
case 2: epatta_mese = 1;
break;
case 3: epatta_mese = 0; //il mese di Marzo ha di nuovo Epatta 0 perché il 1° del mese avviene il Capodanno Lunare e Febbraio che lo precede ha meno giorni degli altri mesi
break;
case 4: epatta_mese = 1;
break;
case 5: epatta_mese = 2;
break;
case 6: epatta_mese = 3;
break;
case 7: epatta_mese = 4;
break;
case 8: epatta_mese = 5;
break;
case 9: epatta_mese = 7; //il mese di Settembre somma due unità invece che una e passa a 7 perché preceduto da due mesi di 31 giorni
break;
case 10: epatta_mese = 8;
break;
case 11: epatta_mese = 9;
break;
case 12: epatta_mese = 10;
break;
default: {
Serial.println(F("I mesi sono solo 12! Hai sbagliato a digitare! L'Epatta Mensile risulterà errata!"));
Serial.flush();
}
break;
}
}
signed int calcolo_giorno_lunare() {
//CALCOLO DELL'ETA' DELLA LUNA, GIORNO DEL CICLO LUNARE
giorno_lunare = (giorno_solare + epatta_mese + epatta_anno) % 30;
return giorno_lunare;
}
void calcolo_fase_lunare() {
//CALCOLO DELLA FASE LUNARE IN BASE ALL'ETA' DELLA LUNA
if (giorno_lunare == 0) fase = F("Luna Nuova");
else if (giorno_lunare >= 1 && giorno_lunare <= 6) fase = F("Crescente");
else if (giorno_lunare >= 7 && giorno_lunare <= 8) fase = F("Primo Quarto");
else if (giorno_lunare >= 9 && giorno_lunare <= 14) fase = F("Gibbosa Crescente");
else if (giorno_lunare == 15) fase = F("Luna Piena");
else if (giorno_lunare >= 16 && giorno_lunare <= 21) fase = F("Gibbosa Calante");
else if (giorno_lunare >= 24 && giorno_lunare <= 29) fase = F("Calante");
else if (giorno_lunare >= 22 && giorno_lunare <= 23) fase = F("Ultimo Quarto");
}
Vorrei capire il perchè di questo problema gentilmente. Grazie