Skip to content

Instantly share code, notes, and snippets.

@remisarrailh
Created February 17, 2019 13:15
Show Gist options
  • Save remisarrailh/cacbf458b7203058a388fcdc5038a584 to your computer and use it in GitHub Desktop.
Save remisarrailh/cacbf458b7203058a388fcdc5038a584 to your computer and use it in GitHub Desktop.
rtc_scenario
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
DateTime horloge;
int pin_relai_1 = 13;
int pin_relai_2 = 11;
//Scenario 1
bool scenario1 = false;
int scenario1_jour_debut = 1;
int scenario1_jour_fin = 11;
int scenario1_heure_debut = 11;
int scenario1_heure_fin = 23;
//Scenario 2
bool scenario2 = false;
int scenario2_jour_debut = 12;
int scenario2_jour_fin = 22;
int scenario2_heure_debut = 11;
int scenario2_heure_fin = 19;
void setup () {
Serial.begin(9600);
pinMode(pin_relai_1, OUTPUT);
pinMode(pin_relai_2, OUTPUT);
Serial.println("Démarrage");
if (! rtc.begin()) {
Serial.println("Horloge RTC non trouvé");
while (1);
}
Serial.println("Horloge Trouvé");
if (! rtc.isrunning()) {
Serial.println("L'horloge non synchronisé");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//Paramètre le temps
//rtc.adjust(DateTime(2019, 1, 1, 3, 0, 0));
} else {
Serial.println("L'horloge est synchronisé");
}
}
void loop () {
//Recupère le temps
horloge = rtc.now();
afficheur_heure();
//Si le scenario n'est pas actif
if (scenario1 == false) {
//Si nous sommes dans la période de jour du scenario 1
if ((horloge.day() >= scenario1_jour_debut ) && (horloge.day() <= scenario1_jour_fin )) {
//Si l'heure est arrivé d'activer le relai
if (horloge.hour() == scenario1_heure_debut) {
scenario1 = true; //Cette variable permet de ne pas réactiver le scénario tout les secondes
Serial.println("Activation Scenario 1");
digitalWrite(pin_relai_1, HIGH);
}
}
}
//Si le scenario1 est actif
if (scenario1 == true) {
//Si l'heure de fin est arrivé, déactiver le relai
if (horloge.hour() == scenario1_heure_fin) {
Serial.println("Déactivation Scenario 1");
digitalWrite(pin_relai_1, LOW);
scenario1 = false;
}
}
//Si le scenario n'est pas actif
if (scenario2 == false) {
//Si nous sommes dans la période de jour du scenario 2
if ((horloge.day() >= scenario2_jour_debut ) && (horloge.day() <= scenario2_jour_fin )) {
//Si l'heure est arrivé d'activer le relai
if (horloge.hour() == scenario2_heure_debut) {
scenario2 = true; //Cette variable permet de ne pas réactiver le scénario tout les secondes
Serial.println("Activation Scenario 2");
digitalWrite(pin_relai_2, HIGH);
}
}
}
//Si le scenario1 est actif
if (scenario2 == true) {
//Si l'heure de fin est arrivé, déactiver le relai
if (horloge.hour() == scenario2_heure_fin) {
Serial.println("Déactivation Scenario 2");
digitalWrite(pin_relai_2, LOW);
scenario2 = false;
}
}
delay(1000);
}
//Affiche l'heure dans le moniteur série
//Ajoute un 0 si l'heure / les minutes / les seconds sont inférieur à 10
void afficheur_heure() {
Serial.print(horloge.day());
Serial.print('/');
Serial.print(horloge.month());
Serial.print('/');
Serial.print(horloge.year());
Serial.print(" - ");
Serial.print(horloge.hour());
Serial.print(':');
Serial.print(horloge.minute());
Serial.print(':');
Serial.print(horloge.second());
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment