mercredi 2 mars 2016

Le code Arduino pour la WordClock

Bonjour à tous,

J'ai eu beaucoup de demandes pour le programme de la Word Clock en français. Je voulais faire un post ou un tuto pour l'expliquer, mais je crois que je ne vais jamais trouver le temps!
Je donne donc le code complet et posez-moi des questions si vous ne comprenez pas.



Résumé des posts précédents:

La word clock:  http://lafamillecreative.blogspot.fr/2012/11/une-word-clock-en-francais.html
La vidéo tuto:  http://lafamillecreative.blogspot.fr/2014/05/word-clock-en-francais-le-tuto.html


A+


Le code ici:




#include <WProgram.h>  // les 3 librairies pour le rtc
#include <Wire.h>
#include <DS1307.h>

int rtc[7];
int Bouton1=3;
int Bouton2=4;
int Detecteur=7;


void Set_Square_Wave(int rate){  // set the square wave output on pin 7 of the DS1307 chip
  rate = rate + 144;                         // add 0x90 (dec 144) to rate
  Wire.beginTransmission(0x68);              // write the control register
  Wire.write(0x07);                           // register address 07H)
  Wire.write(rate);                           // 90H=1Hz, 91H=4kHz, 92H=8kHz, 93H=32kHz
  Wire.endTransmission();
}

 int latchPin=8;     // 3 lignes pour le shift out
 int clockPin=12;
 int dataPin=11;


 int Compteur1=0;
 int Compteur2=0;
 int Compteur3=0;
 int Coef=0;
 int alea=0;
 int k=1;
 int Var1=0;
 int Var2=0;
 int Ancvar1=0;
 int Ancvar2=0;
 int Temp1=1000; // tempo pour mise en veille
 int Temp2=400;  // tempo pour rafraichir
 int Inc=0;
 int Allumer=0;
 int Presence=1;


void setup()
{

  pinMode(Bouton1,INPUT);// déclaration des entrées
  pinMode(Bouton2,INPUT);
  pinMode(Detecteur,INPUT);

  digitalWrite(Bouton1, HIGH);  // entrées par défaut à 1, évite une résistance de pull-UP.
  digitalWrite(Bouton2, HIGH);

 // Décommenter la partie suivante et mettez vos valeurs pour initialiser l'horloge

  //RTC.stop();
  //RTC.set(DS1307_SEC,0);    // Secondes
  //RTC.set(DS1307_MIN,10);   // Minutes
  //RTC.set(DS1307_HR,4);    // Heures
  //RTC.set(DS1307_DOW,3);    // Jour de la semaine
  //RTC.set(DS1307_DATE,7);  // Jour du mois
  //RTC.set(DS1307_MTH,11);   // Mois
  //RTC.set(DS1307_YR,12);    // Année
  //RTC.start();



  // Mettre une resistance de pull up entre pin 7 et Vbat
  // 0=1hz, 1=4KHz, 2=8KHz, 3=32KHz
  Set_Square_Wave(0); // 1Hz


 pinMode(latchPin,OUTPUT);
 pinMode(clockPin,OUTPUT);
 pinMode(dataPin,OUTPUT);


}

void loop()
{
 delay(50);
  RTC.get(rtc,true);

  Presence=digitalRead(Detecteur);
  if (Presence==1) Temp1=2000;



  Var1=1-digitalRead(Bouton1);
  Var2=1-digitalRead(Bouton2);

  if (Var1==0) Ancvar1=0;
  if (Var2==0) Ancvar2=0;
 
   if (Var1!=Ancvar1) // Front Montant du bouton 1
   {
     Inc=rtc[1]+5;
     if (Inc>59) Inc=0;
   
   
     RTC.stop();    
     RTC.set(DS1307_MIN,Inc);
     RTC.start();
   
     Temp1=1000;
     affichage();
     Ancvar1=1;
   
   
   
   }

  if (Var2!=Ancvar2) // Front Montant du bouton 2
   {
   
     Inc=rtc[2]+1;
      if (Inc>23) Inc=0;
   
     RTC.stop();
     RTC.set(DS1307_HR,Inc);
     RTC.start();
   
     Temp1=1000;
     affichage();
      Ancvar2=1;
   }


    Temp1-- ;
    if (Temp1<0) Temp1=0;
 
    if (Temp1>0) {
      if (Allumer==0) {   // lancer l'animation à l'allumage
        alea=random(100);
         if (alea>50){
           deco1();}
         else {
           deco2();}
      }
      Allumer=1;
      Temp2++;
      if (Temp2>400) {
         Temp2=0;
         affichage();
     
      }
    }
    else {
     if (Allumer==1){
       eteindre();
       Allumer=0;
     }
    }
 
     
   
}

void shiftOut(byte dataOut) {

 boolean pinState;
 digitalWrite (dataPin, LOW);
 digitalWrite (clockPin, LOW);

 for (int i=0; i<=7; i++) {
  digitalWrite(clockPin, LOW);
  if (dataOut & (1<<i)) {
   pinState=HIGH;
  }
  else {
   pinState=LOW;
  }

 digitalWrite(dataPin,pinState);
 digitalWrite (clockPin, HIGH);
 digitalWrite (dataPin, LOW);
 }

 digitalWrite (clockPin, LOW);

}

void  deco1() {

  for (int i=1; i <= 10; i++){
     k=1;
       for (int j=0; j <= 7; j++){
         
       
          digitalWrite(latchPin, LOW);
          alea=random(0,2);
          shiftOut(k*alea);
          alea=random(0,2);
          shiftOut(k*alea);
          alea=random(0,2);
          shiftOut(k*alea);
          digitalWrite(latchPin, HIGH);
          delay(20);
          k=k*2;
       
        }
     }


}


void deco2() {

    for (int i=0; i <= 25; i++){
 
     digitalWrite(latchPin, LOW);
    alea=random(250)+1;
    shiftOut(alea);
    alea=random(250)+1;
    shiftOut(alea);
    alea=random(250)+1;
    shiftOut(alea);
 
    digitalWrite(latchPin, HIGH);
    alea=random(200)+50;
    delay(alea);

    }

}

void affichage() {

    RTC.get(rtc,true);
       
 
     Temp2=400;

 
 
   // calcul de l'heure
 
   Compteur1=32;  // "Il est"
   Compteur2=0;
   Compteur3=160;  // "Heure" et "s"
 
   Coef=0;
   if (rtc[1]>34) {
     Coef=1;  // coef pour les heures
     Compteur3=Compteur3+2; // "Moins"
   }
 
 
     if (rtc[2]==1-Coef || rtc[2]==13-Coef) {
       Compteur2=Compteur2+8; // "Une"
       Compteur3=Compteur3-128; // Suppression du "S"
     }
   
     if (rtc[2]==2-Coef || rtc[2]==14-Coef) Compteur1=Compteur1+4; // "Deux"
     if (rtc[2]==3-Coef || rtc[2]==15-Coef) Compteur2=Compteur2+1; // "Trois"
     if (rtc[2]==4-Coef || rtc[2]==16-Coef) Compteur1=Compteur1+16; // "Quatre"
     if (rtc[2]==5-Coef || rtc[2]==17-Coef) Compteur2=Compteur2+32; // "Cinq"
     if (rtc[2]==6-Coef || rtc[2]==18-Coef) Compteur2=Compteur2+16; // "Six"
     if (rtc[2]==7-Coef || rtc[2]==19-Coef) Compteur1=Compteur1+8; // "Sept"
     if (rtc[2]==8-Coef || rtc[2]==20-Coef) Compteur2=Compteur2+2; // "Huit"
     if (rtc[2]==9-Coef || rtc[2]==21-Coef) Compteur1=Compteur1+64; // "Neuf"
     if (rtc[2]==10-Coef || rtc[2]==22-Coef) Compteur3=Compteur3+4; // "Dix"
     if (rtc[2]==11-Coef || rtc[2]==23-Coef) Compteur1=Compteur1+128; // "Onze"
     if (rtc[2]==12-Coef) {
       Compteur1=Compteur1+1; // "Midi"
       Compteur3=Compteur3-160; // Suppression de "heure" et de "S"
     }
     if ((rtc[2]==0 && Coef==0)||(rtc[2]==23 && Coef==1)) Compteur3=Compteur3-152; // "Minuit" et Suppression de "heure" et de "s" (8-160)=-152
   
    if ((rtc[1]>4 && rtc[1]<10)||(rtc[1]>54)||(rtc[1]>24 && rtc[1]<30)||(rtc[1]>34 && rtc[1]<40)) Compteur3=Compteur3+16;  // "Cinq" (minutes)
    if ((rtc[1]>9 && rtc[1]<15)||(rtc[1]>49 && rtc[1]<55)) Compteur2=Compteur2+4; // "Dix" (minutes)
    if ((rtc[1]>19 && rtc[1]<30)||(rtc[1]>34 && rtc[1]<45)) Compteur3=Compteur3+1;  // "Vingt"
    if ((rtc[1]>14 && rtc[1]<20)||(rtc[1]>44 && rtc[1]<50)) Compteur2=Compteur2+64;  // "Quart"
    if ((rtc[1]>14 && rtc[1]<20)||(rtc[1]>29 && rtc[1]<35))  Compteur1=Compteur1+2; // "Et"
    if (rtc[1]>44 && rtc[1]<50)  Compteur3=Compteur3+64; // "Le"
    if (rtc[1]>29 && rtc[1]<35) Compteur2=Compteur2+128; // "Demi"
 
 
   // affichage de l'heure
    digitalWrite(latchPin, LOW);
    shiftOut(Compteur3);
    shiftOut(Compteur2);
    shiftOut(Compteur1);
    digitalWrite(latchPin, HIGH);

}


void eteindre(){
   digitalWrite(latchPin, LOW);
    shiftOut(0);
    shiftOut(0);
    shiftOut(0);
    digitalWrite(latchPin, HIGH);
}

2 commentaires:

  1. apres compilation du programme ca donne erreur , svp y a t il un explication

    RépondreSupprimer
    Réponses
    1. Envoyez moi une copie d'écran du code erreur par mail (l’enveloppe en haut du blog), je pourrais peut-être vous aider.

      Supprimer

Un petit commentaire, ça fait toujours plaisir!