I am working on making a countdown timer for the birth of a baby. I found all the requisite information on Thingiverse for a 3d printable baby bottle with a small LCD that counts down the days, hours and minutes until the due date. The fileset also included the code. The hardware I am using is the same as the source files, namely an Arduino Nano, a I2C RTC DS1307 AT24C32 Real Time Clock Module, a KY-040 rotary encoder module, and a 0.96 Inch OLED Module. When I attempt to verify the included code I get the an error message: no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)' . So I contacted the person who wrote the code, seeing if there was an update. He said there was none needed, and thought the problem was in the inclusion of required libraries. I double checked, and found all required libraries have been included in my sketch. So I continued to do some digging. I have been told that the problem lies in the fact that the current version of the DS3231 library doesn't have a constructor that accepts parameters such as SDA & SCL, which are in the code. Now that I know what the potential problem is, I still don't know how to fix it.
Any assistance will be greatly appreciated.
Here is the wiring diagram:
Here is the link to the Thingiverse files
https://www.thingiverse.com/thing:6049409/comments
Here is the code I am attempting to use:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
// Etats de la machine d'état
#define ETAT_DECOMPTE 10
#define ETAT_JOURS_CEN 20
#define ETAT_JOURS_DIZ 21
#define ETAT_JOURS_UNI 22
#define ETAT_HEURES_DIZ 30
#define ETAT_HEURES_UNI 31
#define ETAT_MINUTES_DIZ 40
#define ETAT_MINUTES_UNI 41
#define ETAT_FINAL 99
int etatMachine = ETAT_DECOMPTE;
int nbJour = 0;
int nbHeure = 0;
int nbMinute = 0;
int nbSeconde = 0;
#define OLED_LARGEUR 128
#define OLED_HAUTEUR 64
// Declaration SSD1306 I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_ADRESSE 0x3C // Voir derriere l'ecran
Adafruit_SSD1306 display(OLED_LARGEUR, OLED_HAUTEUR, &Wire, OLED_RESET);
// Switch
int pinSwitch = 8;
int valSwitch;
int valSwitchDernier;
int pinDt = 9;
int valDt;
int valDtDernier;
int pinClk = 10;
int valClk;
int valClkDernier;
int dernierEnfonce;
int dureeEnfonce;
// Horloge
DS3231 horloge(SDA, SCL);
void GestionSwitch() {
valSwitch = digitalRead(pinSwitch);
valDt = digitalRead(pinDt);
valClk = digitalRead(pinClk);
// Rotation du bouton
if (valClk != valClkDernier && valClk == 1) {
if (etatMachine == ETAT_JOURS_CEN)
nbJour = (nbJour + 100) % 1000;
if (etatMachine == ETAT_JOURS_DIZ)
nbJour = nbJour / 100 * 100 + ((nbJour / 10 + 1) % 10) * 10 + nbJour % 10;
if (etatMachine == ETAT_JOURS_UNI)
nbJour = 10 * (nbJour / 10) + (nbJour + 1) % 10;
if (etatMachine == ETAT_HEURES_DIZ)
nbHeure = (nbHeure + 10) < 24 ? nbHeure + 10 : 0;
if (etatMachine == ETAT_HEURES_UNI)
nbHeure = 10 * (nbHeure / 10) + (((nbHeure % 10) + 1) % 10);
if (etatMachine == ETAT_MINUTES_DIZ)
nbMinute = (nbMinute + 10) < 60 ? nbMinute + 10 : 0;
if (etatMachine == ETAT_MINUTES_UNI)
nbMinute = 10 * (nbMinute / 10) + (((nbMinute % 10) + 1) % 10);
}
// Click du bouton
if (valSwitch != valSwitchDernier) {
if (valSwitch == LOW) {
// Le bouton vient d'être enfoncé
dernierEnfonce = millis();
} else {
// Bouton vient d'être relâché
dureeEnfonce = millis() - dernierEnfonce;
if (dureeEnfonce >= 100) {
if (etatMachine == ETAT_FINAL) {
etatMachine = ETAT_JOURS_CEN;
} else if (etatMachine == ETAT_DECOMPTE) {
etatMachine = ETAT_JOURS_CEN;
DifferenceMinute();
} else if (etatMachine == ETAT_JOURS_CEN) {
etatMachine = ETAT_JOURS_DIZ;
} else if (etatMachine == ETAT_JOURS_DIZ) {
etatMachine = ETAT_JOURS_UNI;
} else if (etatMachine == ETAT_JOURS_UNI) {
etatMachine = ETAT_HEURES_DIZ;
} else if (etatMachine == ETAT_HEURES_DIZ) {
etatMachine = ETAT_HEURES_UNI;
} else if (etatMachine == ETAT_HEURES_UNI) {
etatMachine = ETAT_MINUTES_DIZ;
} else if (etatMachine == ETAT_MINUTES_DIZ) {
etatMachine = ETAT_MINUTES_UNI;
} else if (etatMachine == ETAT_MINUTES_UNI) {
etatMachine = ETAT_DECOMPTE;
AjusteDateHeure();
}
}
}
valSwitchDernier = valSwitch;
}
valSwitchDernier = valSwitch;
valDtDernier = valDt;
valClkDernier = valClk;
}
void DifferenceMinute() {
Time debut = horloge.getTime();
long difHeure = (long)86400 - ((long)(debut.hour) * 3600 + (long)(debut.min) * 60 + (long)debut.sec);
nbSeconde = difHeure % 60;
nbMinute = (difHeure / 60) % 60;
nbHeure = difHeure / 3600;
nbJour = 0;
if (debut.year < 2020) {
// Parcours les jours.
while (2020 > debut.year) {
nbJour++;
// Change d'annee
if (debut.mon == 12 && debut.date == 31) {
debut.date = 1;
debut.mon = 1;
debut.year = debut.year + 1;
// Change de mois
} else if ((debut.mon == 1 && debut.date == 31) ||
(debut.mon == 2 && debut.date == 28 && debut.year % 4 != 0) ||
(debut.mon == 2 && debut.date == 29 && debut.year % 4 == 0) ||
(debut.mon == 3 && debut.date == 31) || (debut.mon == 4 && debut.date == 30) ||
(debut.mon == 5 && debut.date == 31) || (debut.mon == 6 && debut.date == 30) ||
(debut.mon == 7 && debut.date == 31) || (debut.mon == 8 && debut.date == 31) ||
(debut.mon == 9 && debut.date == 30) || (debut.mon == 10 && debut.date == 31) ||
(debut.mon == 11 && debut.date == 30)) {
debut.date = 1;
debut.mon = debut.mon + 1;
}
else { // Change de jour
debut.date = debut.date + 1;
}
}
}
}
void AffichageTexte(String texte, int taille, int posX, int posY, bool isAllume) {
display.setTextSize(taille);
display.setCursor(posX, posY);
display.print(isAllume ? texte : "");
}
void Affichage() {
display.clearDisplay();
bool isAllume = etatMachine == ETAT_DECOMPTE || ((millis() - dernierEnfonce) % 1500) > 500;
// Vérifie si on a fini le décompte
if (etatMachine == ETAT_DECOMPTE)
etatMachine = nbJour == 0 && nbHeure == 0 && nbMinute == 0 ? ETAT_FINAL : etatMachine;
if (etatMachine == ETAT_FINAL) {
AffichageTexte("Youpi!", 3, 15, 20, isAllume);
} else {
// Affiche le nombre de jours.
AffichageTexte(String(nbJour / 100), 4, 14, 10, etatMachine == ETAT_JOURS_UNI ||
etatMachine == ETAT_JOURS_DIZ || (etatMachine == ETAT_JOURS_CEN && isAllume) ||
(etatMachine != ETAT_JOURS_CEN && nbJour > 99) ||
(etatMachine == ETAT_DECOMPTE && nbJour > 99));
AffichageTexte(String((nbJour % 100) / 10), 4, 39, 10, etatMachine == ETAT_JOURS_UNI ||
etatMachine == ETAT_JOURS_CEN || (etatMachine == ETAT_JOURS_DIZ && isAllume) ||
(etatMachine != ETAT_JOURS_DIZ && nbJour > 9) ||
(etatMachine == ETAT_DECOMPTE && nbJour > 9));
AffichageTexte(String(nbJour % 10), 4, 64, 10, etatMachine != ETAT_JOURS_UNI ||
(etatMachine == ETAT_JOURS_UNI && isAllume));
AffichageTexte(nbJour > 1 ? "jours" : "jour", 1, 90, 31, true);
// Affiche HH:MM:SS
AffichageTexte(String(nbHeure / 10), 2, 15, 46, (etatMachine == ETAT_HEURES_DIZ && isAllume) ||
(etatMachine != ETAT_HEURES_DIZ && nbHeure > 9));
AffichageTexte(String(nbHeure % 10), 2, 27, 46, (etatMachine != ETAT_HEURES_UNI) || isAllume);
AffichageTexte(":", 2, 39, 46, true);
AffichageTexte(String(nbMinute / 10), 2, 51, 46, (etatMachine != ETAT_MINUTES_DIZ) || isAllume);
AffichageTexte(String(nbMinute % 10), 2, 63, 46, (etatMachine != ETAT_MINUTES_UNI) || isAllume);
AffichageTexte(":", 2, 75, 46, etatMachine == ETAT_DECOMPTE);
AffichageTexte(String(nbSeconde / 10), 2, 87, 46, etatMachine == ETAT_DECOMPTE);
AffichageTexte(String(nbSeconde % 10), 2, 99, 46, etatMachine == ETAT_DECOMPTE);
display.display();
}
}
// Ajuste la date et l'heure actuelle
void AjusteDateHeure() {
int annee = 2020;
int mois = 1;
int jour = 1;
int heure = 0;
int minut = 0;
// Soustraire heures et minutes
heure = (1440 - (nbHeure * 60 + nbMinute)) / 60;
minut = (1440 - (nbHeure * 60 + nbMinute)) % 60;
// Soustraire les jours
while (nbJour > 0) {
annee = (jour == 1 && mois == 1) ? annee - 1 : annee;
mois = jour == 1 ? (mois == 1 ? 12 : mois - 1) : mois;
jour = jour == 1 ? joursDansMois(mois, annee) : jour - 1;
nbJour--;
}
horloge.setDate(jour, mois, annee);
horloge.setTime(heure, minut, 0);
DifferenceMinute();
}
// Fonction pour déterminer le nombre de jours dans un mois
int joursDansMois(int mois, int annee) {
if (mois == 2) {
if (annee % 4 == 0 && (annee % 100 != 0 || annee % 400 == 0)) {
return 29; // Année bissextile
} else {
return 28; // Aannée non-bissextile
}
} else if (mois == 4 || mois == 6 || mois == 9 || mois == 11) {
return 30; // mois de 30 jours
} else {
return 31; // mois de 31 jours
}
}
void setup() {
Serial.begin(115200);
// Préparation de l'écran OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADRESSE)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Caracteristiques du texte
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
// Switch, Dt et Clk
pinMode (pinSwitch, INPUT);
pinMode (pinDt, INPUT);
pinMode (pinClk, INPUT);
pinMode (pinSwitch, INPUT_PULLUP);
pinMode (pinDt, INPUT_PULLUP);
pinMode (pinClk, INPUT_PULLUP);
valSwitchDernier = digitalRead(pinSwitch);
valDtDernier = digitalRead(pinDt);
valClkDernier = digitalRead(pinClk);
// Initialise l'horloge
horloge.begin();
DifferenceMinute();
if (nbJour + nbHeure + nbMinute <= 0) {
horloge.setTime(0, 0, 0);
horloge.setDate(1, 1, 2020);
}
}
void loop() {
if (etatMachine == ETAT_DECOMPTE)
DifferenceMinute();
GestionSwitch();
Affichage();
delay(50);
}