Newer
Older
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
// Definition des pins des capteurs et actionneurs
#define capteur_hum_sol A1 //capteur humidite sol a connecter sur le A0
#define pin_hum_sol 2 //connecter le + du capteur humidite sol
#define pompe 3 //connecter le S du relais sur la pin 3, il controle la pompe
#define electrovanne 4 //connecter le S du relais sur la pin 4, il controle l'electrovanne
#define capteur_reservoir 5 //connecter le capteur de niveau d'eau
//Déclaration des variables du programme
int valeur_hum_sol = 0; // on stocke les valeurs lu par le capteur d'humidité sol
int Switch = 10; //switch boutton poussoir encoder
byte portA = 11; //encoder invertion fritzing 6
byte portB = 9; // encoder
byte etat = 0;
bool etat_switch = false;
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified backpack
void setup ()
{
Serial.begin(9600);
// activate LCD module
lcd.begin (16, 2); // for 16 x 2 LCD module
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("KHomiAuto");
delay(1000);
lcd.clear();
pinMode(capteur_hum_sol, INPUT);
pinMode(capteur_reservoir, INPUT);
pinMode(portA, INPUT);
pinMode(portB, INPUT);
pinMode(Switch, INPUT_PULLUP);
pinMode(pin_hum_sol, OUTPUT);
pinMode(pompe, OUTPUT);
pinMode(electrovanne, OUTPUT);
attachInterrupt(0, control, CHANGE);
attachInterrupt(1, control, CHANGE);
////////////////////////////////////// configuration de l'horloge //////////////////////////////////////
if (! rtc.begin())
{
Serial.print("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning())
{
Serial.print("RTC is NOT running!");
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// configurer manuellement l'heure
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
void loop ()
{
// on lit l'heure sur le rtc
DateTime now = rtc.now();
//on lance le debug pour verifier si il ya pas de probleme
affiche_lcd();
//on test si il est heure d'arroser
if ((now.hour() == heure ) && (now.minute() == minut))
{
if ( digitalRead(capteur_reservoir) == 1) // on test si le reservoir d'eau est remplit
{
arroser();
lcd.setCursor(0, 1);
lcd.print("fin arrosage");
}
else // sinon on envoie un message alert que le reservoir est vide
{
lcd.setCursor(0, 1);
lcd.print("reservoir vide");
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}
}
}
////////////////////////////////////// fonction arroser //////////////////////////////////////
void arroser () {
digitalWrite(pin_hum_sol, HIGH); // on allume le capteur
do {
//on lit 10 fois le capteur
for (int i = 0; i < 10; i++)
{
valeur_hum_sol = analogRead(capteur_hum_sol); // on lit la valeur analogique du capteur
}
///////////////////// on test le sol si il est mouille ou pas et on action ou pas les moteurs /////////////////////
if (valeur_hum_sol > 500 )
{
digitalWrite(electrovanne, HIGH); // on allume electrovanne
delay(500); // on attend 500ms
digitalWrite(pompe, HIGH); // on allume la pompe
}
}
while (valeur_hum_sol > 500); // tant que le sol est sec on arrose
if (valeur_hum_sol < 500 ) // si le sol est mouille
{
digitalWrite(electrovanne, LOW); // on eteind l'electrovanne
digitalWrite(pompe, LOW); // on eteind la pompe
digitalWrite(pin_hum_sol, LOW); // on eteind le capteur
}
}
void control() {
etat = etat << 1 | digitalRead(portA);
etat = etat << 1 | digitalRead(portB);
byte etat_test = (etat | B11110000) - B11110000;//test des 4 bits de poids faible
//Serial.println(etat_test,BIN);//débug : visualisation de la séquence
//bouton poussoir
if (digitalRead(Switch) == false)
{
etat_switch = !etat_switch;
}
// heure configuration
if (( etat_test == B0111 ) && (etat_switch == 0))
else if (( etat_test == B1011 ) && (etat_switch == 0))
{
heure--;
if ( heure < 0)
{
heure = 00;
}
}
// minute configurration
if (( etat_test == B0111 ) && (etat_switch == 1))
{
minut++;
}
else if (( etat_test == B1011 ) && (etat_switch == 1))
{
minut--;
if ( minut < 0)
{
minut = 00;
}
}
else if (heure == 25)
{
heure = 00;
}
else if (minut == 61)
{
}
}
////////////////////////////////////// fonction affichage //////////////////////////////////////
void affiche_lcd()
{
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("H=");
lcd.print(heure);
lcd.setCursor(7, 0);
lcd.print("M=");
lcd.print(minut);
/* lcd.setCursor(2, 1);
lcd.println(etat_switch);*/
////////////////////////////////////// Débug //////////////////////////////////////
void Debug()
{
DateTime now = rtc.now();
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
Serial.print(" ");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(" ,");
Serial.print(now.day());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.year());
Serial.print(" valeur humidite sol ");
Serial.print(valeur_hum_sol);
Serial.print(" heure ");
Serial.println(heure);