#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos ;
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 RTC;
int motorPosAdress = 20;
int BtStart = 5;
bool startCount = false;
bool addCount = false;
int DayAddress = 25;
int hourAddress = 30;
bool counterAdress = 35;
//int servoAddr =60;
// int position;
// #define position EEPROM.read(servoAddr)
int retournement = 0;
int dayCount = 0;
int hourCounter = 0;
#define JSemaine now.dayOfTheWeek()
#define heure now.hour()
#define mins now.minute()
#define secs now.second()
#define jour now.day()
#define mois now.month()
#define annee now.year()
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime now = RTC.now();
void setup() {
Serial.begin(115200);
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// dayCount = EEPROM.read(DayAddress);
// hourCounter = EEPROM.read(hourAddress);
myservo.attach(11); // attaches the servo on pin 9 to the servo object
pinMode(BtStart, INPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" INCUBATEUR D'OEUFS ");
lcd.setCursor(0, 1);
lcd.print("CHOISISSEZ LE TYPE ");
lcd.setCursor(0, 2);
lcd.print("EN APPUIYANT SUR LA ");
lcd.setCursor(0, 3);
lcd.print("TOUCHE MENU S.V.P...");
delay(2000);
lcd.clear();
}
void loop() {
DateTime now = RTC.now();
if (!startCount && digitalRead(BtStart) == HIGH) {
startCount = true;
EEPROM.update(counterAdress,true);
EEPROM.update(hourAddress,0);
EEPROM.update(DayAddress,0);
Serial.print("comptage demarree day : ");
Serial.println (EEPROM.read(DayAddress));
Serial.print("compteur d'heures: ");
Serial.println (EEPROM.read(hourAddress));
Serial.print("etat start: ");
Serial.println (EEPROM.read(counterAdress));
}
if (startCount && digitalRead(BtStart) == HIGH){
// quittez l'incubation
// startCount = false;
// EEPROM.update(counterAdress,false);
}
// else{
// EEPROM.update(counterAdress,0);
// }
if ( EEPROM.read(counterAdress)==true ) {
//Serial.print("compteur d'heures: ");
//Serial.println(hourCounter);
//hourCounter++;
affichage();
if ( mins ==6 && secs == 0){
delay(999);
hourCounter++;
Serial.print("compteur d'heures: ");
Serial.println(hourAddress);
EEPROM.update(hourAddress,hourCounter);
}
if (EEPROM.read(hourCounter) ==24 ){
delay(999);
dayCount++;
hourCounter = 0;
Serial.print("compteur d'heures: ");
Serial.println(hourCounter);
EEPROM.update(hourAddress,hourCounter);
EEPROM.update(DayAddress,hourCounter);
}
if (EEPROM.read(hourCounter) % 2 == 0 || mins % 2 ==0 && secs == 0){
delay(999);
retournement ++;
Serial.print("etat du retournement ");
Serial.println(retournement);
EEPROM.update(motorPosAdress,retournement);
}
if (EEPROM.read(hourCounter) % 2 == 0 || mins % 2 ==0 && secs ==5){
if (EEPROM.read(motorPosAdress) ==1){
//rotation vers la gauche
Serial.println("rotation du moteurs vers la gauche");
for (pos = 180; pos >= 20; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if (EEPROM.read(motorPosAdress)==2){
for (pos = 20; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println("rotation du moteurs vers la droite");
//rotation vers la droite
// delay(999);
// retournement = 0;
// EEPROM.update(motorPosAdress,retournement);
}
// Serial.println(dayCount);
}
//Serial.println(dayCount);
//affichage();
}
}
void timer() {
DateTime now = RTC.now();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(heure);
lcd.print(":");
lcd.print(mins);
lcd.print(":");
lcd.print(secs);
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(jour);
lcd.print("/");
lcd.print(mois);
lcd.print("/");
lcd.print(annee);
delay(1000);
}
void affichage() {
DateTime now = RTC.now();
if (now.hour() < 10) {
lcd.setCursor(0, 2); lcd.print("0");
lcd.setCursor(1, 2); lcd.print(heure);
}
else {
lcd.setCursor(0, 2); lcd.print(heure);
}
lcd.setCursor(2, 2); lcd.print(":");
if (now.minute() < 10) {
lcd.setCursor(3, 2); lcd.print("0");
lcd.setCursor(4, 2); lcd.print(mins);
}
else {
lcd.setCursor(3, 2); lcd.print(mins);
}
lcd.setCursor(5, 2); lcd.print(":");
if (now.second() < 10) {
lcd.setCursor(6, 2); lcd.print("0");
lcd.setCursor(7, 2); lcd.print(secs);
}
else {
lcd.setCursor(6, 2); lcd.print(secs);
}
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}