bonjour.
j'aurais besoin de faire des actions en fonction d'un temps ( par exemple un calcul tout les X minutes, une sauvegarde des données sur carte SD, un envoie vers mon serveur)
j'ai dans mon tiroir une DS1302.
quelle bibliothèque me conseillez vous afin de me faciliter ces taches.
Par exemple la comparaison avec une heure donnée afin d'effectuer une tache
Le calcul d'un temps passé afin d'effectuer une tache.
-Je sais a peu près bien manipuler le Timestamp pour les calculs.
-Je n'ai pas trouvé d'exemples qui me conviennent avec des datalogers
-Je ne peux pas utiliser Millis car la bibliothèque Serial perturbe cette fonction.
Si vous avez une piste!
now(); // returns the current time as seconds since Jan 1 1970
The time and date functions can take an optional parameter for the time. This prevents errors if the time rolls over between elements. For example, if a new minute begins between getting the minute and second, the values will be inconsistent. Using the following functions eliminates this problem
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
minute(t); // returns the minute for the given time t
second(t); // returns the second for the given time t
day(t); // the day for the given time t
weekday(t); // day of the week for the given time t
month(t); // the month for the given time t
year(t); // the year for the given time t
time_t est une structure qui permet de faire des comparaisons
time_t Time type
Alias of a fundamental arithmetic type capable of representing times, as those returned by function time.
For historical reasons, it is generally implemented as an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC (i.e., a unix timestamp). Although libraries may implement this type using alternative time representations.
-Je ne peux pas utiliser Millis car la bibliothèque Serial perturbe cette fonction.
oui bon y'a quelques nano-secondes potentiellement de blocage pendant les interruptions.... mais c'est peanuts par rapport à la dérive de l'horologe de toutes façons... Serial n'y est pas pour grands chose...
Alain-alph:
-Je ne peux pas utiliser Millis car la bibliothèque Serial perturbe cette fonction.
Bonjour,
Ca vient d'où cette affirmation?
Heureusement que les interruptions ne perturbent pas cette fonction.
La seule façon de perturber cette fonction serait qu'on rate une interruption timer, donc il faudrait que les interruptions soient bloquées pendant plus de 2 ms.
La RTCLib fonctionnera bien et vous offre en prime des classes pour gérer le temps —> La classe DateTime et la classe TimeSpan. Avec ça vous pouvez faire des additions soustraction ou calcul de durée.
#include <Wire.h>
#include <RTClib.h> // https://github.com/adafruit/RTClib
RTC_DS1307 rtc;
const char * semaine[]= {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
void setup ()
{
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println(F("Erreur RTC"));
while (1);// on ne va pas plus loin
}
if (! rtc.isrunning()) {
Serial.println(F("Configurer la RTC..."));
// ceci met à jour la RTC avec la date et heure de dernière compilation
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop () {
DateTime now = rtc.now(); // on lit l’heure dans l’instance now
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(" (");
Serial.print(semaine[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.println();
// calcul d’une date dans 7 jours 0h 30 min et 6 secondes
DateTime futur (now + TimeSpan(7,0,30,6)); // TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds);
Serial.print(F" nouvelle date: "));
Serial.print(futur.year());
Serial.print('/');
Serial.print(futur.month());
Serial.print('/');
Serial.print(futur.day());
Serial.print(' ');
Serial.print(futur.hour());
Serial.print(':');
Serial.print(futur.minute());
Serial.print(':');
Serial.println(futur.second());
delay(3000);
}
Modifié ici depuis ma tablette donc sans garantie - mais vous voyez l’idée...
J'ai fait une erreur, en fait j'ai ecrit serial au lieu de sofware serial. C'est donc la bibliothèque sotwareserial qui perturbe Millis()
La bibliothèque time ressemble très fort a ce que je recherche. Merci
Oui je sais que la 1302 n'est pas le top, mais elle est dans mon kit!
Je viens de trouver une bibliothèque (car il y a malheureusement plusieurs version de 1302) assez étoffée elle appelle (je ne sais pas si on dit comme cela) la bibliothèque Time. mais surtout time_t est connue de cette bibliothèque. @Lesept merci pour cette piste. @J-M-L Merci pour le bout de code. Je vais regarder de près le calcul des dates