// pushbutton declaration
// constants won't change. They're used here to set pin numbers:
const int pointbaspin = 3; // point bas
const int pointhautpin = 4; // point haut
const int ledPinhaut = 12; // the number of the LED pin
const int ledPinbas = 10;
const int ledjourpin = 8; // LOW = nuit, HIGH = jour
const int capteurjourpin= 11;
// variables will change:
int pointhautstate ;
int pointbasstate ;
int jourstate;
String etat;
/* Servo motor with Arduino example code. Position and sweep. More info: https://www.makerguides.com/ */
// Include the servo library:
#include <Servo.h>
// Create a new servo object:
Servo myservo;
// Define the servo pin:
#define servoPin 9
// Create a variable to store the servo position:
int angle;
int x=0;
/*
* Projet le Module RTC avec Arduino
* wwww. Electronique-Mixte.fr
*
* Exemples d'Applications:
* 1. Gestion du temps
* 2. Actions temporisées
* 3. Ordonnancement des tâches
* 4. Horloge numérique
* 5. Calendrier numérique
* 6. Datation des données
* 7. Etc.
*/
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
word DateHeure[6]={0,0,0,0,0};
/*
* JoursSem[0]: Secondes
* JoursSem[1]: Minutes
* JoursSem[2]: Heures
* JoursSem[3]: Jours
* JoursSem[4]: Mois
* JoursSem[5]: Années
*/
/*
Mois 1-12
Jours 1-31
Heures 0-23
Minutes 0-59
Secondes 0-59
*/
char JoursSem[7][12] = {"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi","Dimanche"};
/*
enum Ds1307SqwPinMode {
DS1307_OFF = 0x00, // Low
DS1307_ON = 0x80, // High
DS1307_SquareWave1HZ = 0x10, // 1Hz square wave
DS1307_SquareWave4kHz = 0x11, // 4kHz square wave
DS1307_SquareWave8kHz = 0x12, // 8kHz square wave
DS1307_SquareWave32kHz = 0x13 // 32kHz square wave
};
*/
void setup ()
{
Serial.begin(9600); // open the serial port at 9600 bps:
//setup pushbutton and LED
// initialize the LED pin as an output:
pinMode(ledPinhaut, OUTPUT);
pinMode(ledPinbas, OUTPUT);
pinMode(ledjourpin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(pointhautpin, INPUT);
pinMode(pointbaspin, INPUT);
pinMode(capteurjourpin, INPUT);
// start RTC
if (! rtc.begin())
{
Serial.println("Le module RTC non disponible");
while (1); // Attente RESET
}
else
{
Serial.println("Le module RTC est OK");
rtc.writeSqwPinMode(DS1307_SquareWave4kHz);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Date du PC
//rtc.adjust(DateTime(2022,8,11,16,26,10)); // Ajustement manuelle
// Ex: 10 Janvier 2020 à 10:00:00:
//rtc.adjust(DateTime(2020, 1, 10, 10, 0, 0));
}
}
void loop ()
{
// read the state of the pushbutton value:
x=x+1;
pointbasstate = digitalRead(pointbaspin);
pointhautstate = digitalRead(pointhautpin);
jourstate = digitalRead(capteurjourpin);
if(jourstate==HIGH){
etat=String("JOUR");}
else {etat=String("NUIT");
}
angle=myservo.read();
// print etat des bouton
Serial.print("loop: "); // prints a label
//Serial.print("\t"); // prints a tab
Serial.print("--- ");
Serial.print(x);
Serial.print("\t");
Serial.print("etat: "); // prints a label
//Serial.print("\t"); // prints a tab
Serial.print("--- ");
Serial.print(etat);
//Serial.print(jourstate);
Serial.print("\t");
Serial.print("ph: "); // prints a label
//Serial.print("\t"); // prints a tab
Serial.print("--- ");
Serial.print(pointhautstate);
Serial.print("\t");
Serial.print("pb: ");
// Serial.print("\t");
Serial.print("--- ");
Serial.print(pointbasstate);
Serial.print("\t");
Serial.print("angle: ");
//Serial.print("\t");
Serial.print("--- ");
Serial.print(angle);
Serial.print("\t");
Serial.print("--- ");
//Serial.println(); // carriage return after the last label
// Lecture du module RTC
GetDateHeure(DateHeure);
Serial.print(DateHeure[2]),DEC;
Serial.print(':');
Serial.print(DateHeure[1],DEC);
Serial.print(':');
Serial.print(DateHeure[0],DEC);
Serial.println();
delay(500); // delay 200 milliseconds
// pushbutton and led
if(pointhautstate== HIGH){digitalWrite(ledPinhaut, HIGH);}
else{ digitalWrite(ledPinhaut, LOW);}
delay(10);
if((pointbasstate== HIGH)){digitalWrite(ledPinbas, HIGH);}
else{digitalWrite(ledPinbas, LOW);}
delay(10);
if((jourstate== HIGH)){digitalWrite(ledjourpin, HIGH);}
else{digitalWrite(ledjourpin, LOW);}
delay(10);
// controle: fait il nuit ?
//if (pointhautstate == HIGH && pointbasstate==LOW && jourstate==LOW ) { // nuit + porte en haut = fermer porte
if (jourstate==LOW ) {
int angle = 0 ;
myservo.write(angle);
delay(00);}
// fait il jour ?
//if (pointhautstate == LOW && pointbasstate==HIGH && jourstate==HIGH ) { // jour + porte en bas = ouvrir porter
if (jourstate==HIGH ) {
int angle = 180 ;
myservo.write(angle);
delay(0);}
// Lecture du module RTC
GetDateHeure(DateHeure);
}
void GetDateHeure(word *DateHeure)
{
// Lecture du module RTC
DateTime now = rtc.now();
// Récupération de la date
DateHeure[5]=now.year(); // Années
DateHeure[4]=now.month(); // Mois
DateHeure[3]=now.day(); // Jours
// Récupérration de l'heure
DateHeure[2]=now.hour(); // Heures
DateHeure[1]=now.minute(); // Minutes
DateHeure[0]=now.second(); // Secondes
// Attach the Servo variable to a pin:
myservo.attach(servoPin);
}