#include <Servo.h>  


Servo bras;   
Servo tete; 
Servo machoire;  
int bra = 0; 
int brabouge = 0;
int tet = 0; 
int tets = 0;
int mache = 0;
int bouton = 2;
int relai =  11;
long randNumber;
int fm = 20; // facteur modulo tete/machoire/bras




void setup() 
{ 
  bras.attach(3); 
  tete.attach(5); 
  machoire.attach(6); 




  Serial.begin(9600); 
  pinMode(bouton, INPUT);
  pinMode(relai, OUTPUT);

} 

void loop() { 

  if (digitalRead(bouton) == HIGH)

  { 

    digitalWrite(relai, HIGH); //ouverture relai et lecteur MP3

    delay(3350); //pause avant commencer animation


    for(tet = 90; tet>=80; tet-=1) //bouge la tête à droite    
    {                                 
      tete.write(tet);                
      delay(15);
    }        

    for(bra = 90; bra>=44; bra-=1) //leve le bras    
    {                                   
      bras.write(bra);  //petits mouvements de tête pendant qu'il leve le bras
      if (bra % fm ==0) {
        randNumber = random(-5,5);
        tets=tet;
        tet=tet+randNumber;
        tete.write(tet);  
        tet=tets;   
      }        
      delay(30); 

    }   
    delay(300);
    int compteur = 0; 
    while (compteur < 12) //bras, 12 va et vient de 8° sur la trompette
    { 

      for(bra = 44; bra < 52; bra += 1)  
      {                                   
        bras.write(bra);  //petits mouvements de tête
        if (bra % fm ==0) {
          randNumber = random(-5,5);
          tets=tet;
          tet=tet+randNumber;
          tete.write(tet); 
          tet=tets;   
        }        
        delay(15); 

      } 

      for(bra = 52; bra>=44; bra-=1)   
      {                                   
        bras.write(bra);  //petits mouvements de tête
        if (bra % fm ==0) {
          randNumber = random(-5,5);
          tets=tet;
          tet=tet+randNumber;
          tete.write(tet); 
          tet=tets;    
        }       
        delay(15); 

      } 

      compteur++;
    } 
    { 
      delay(300); 

      for(bra = 44; bra < 90; bra += 1)  //baisse le bras

      {                                   
        bras.write(bra);  //petits mouvements de tête pendant qu'il baisse le bras
        if (bra % fm ==0) {
          randNumber = random(-5,5);
          tets=tet;
          tet=tet+randNumber;
          tete.write(tet); 
          tet=tets;    
        }       
        delay(20); 

      } 

      delay(100); 

      for(tet = 80; tet < 130; tet += 1)  //bouge la tete à gauche
      {                                
        tete.write(tet);              
        delay(15);                        

      }

      for(tet = 130; tet>=50; tet-=1) //bouge la tête à droite    
      {                                 
        tete.write(tet);                
        delay(15);

      }  

      delay(100);  

      for(tet = 50; tet < 110; tet += 1)  //bouge la tête à gauche
      {                                
        tete.write(tet);              
        delay(20);                        

      }

      delay(120);    //commence à parler

      int compteur = 0; 
      while (compteur < 3) //dis son nom
      { 

        for(mache = 90; mache < 120; mache += 1)
        {                                   
          machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
          if (mache % fm ==0) {
            randNumber = random(-1,1);
            tets=tet;
            tet=tet+randNumber;
            tete.write(tet); 
            tet=tets;   
          }


          //petits mouvements de bras pendant qu'il parle
          if (mache % fm ==0) {
            randNumber = random(-7,2);
            brabouge=bra;
            bra=bra+randNumber;
            bras.write(bra);  
            bra=brabouge;
          } 

          delay(4); 

        } 
        for(mache = 120; mache>=90; mache-=1) 

        {                                   
          machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
          if (mache % fm ==0) {
            randNumber = random(-1,1);
            tets=tet;
            tet=tet+randNumber;
            tete.write(tet); 
            tet=tets;    
          } 

          //petits mouvements de bras pendant qu'il parle
          if (mache % fm ==0) {
            randNumber = random(-7,2);
            brabouge=bra;
            bra=bra+randNumber;
            bras.write(bra);  
            bra=brabouge;
          }

          delay(4); 

        } 
        compteur++;

      }

      {
        delay(200);    

        int compteur = 0; 
        while (compteur < 4) //apres avoir dis son nom
        { 

          for(mache = 90; mache < 120; mache += 1)

          {                                   
            machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
            if (mache % fm ==0) {
              randNumber = random(-1,1);
              tets=tet;
              tet=tet+randNumber;
              tete.write(tet); 
              tet=tets;   
            } 

            //petits mouvements de bras pendant qu'il parle
            if (mache % fm ==0) {
              randNumber = random(-7,2);
              brabouge=bra;
              bra=bra+randNumber;
              bras.write(bra);  
              bra=brabouge;
            }

            delay(4); 

          } 

          for(mache = 120; mache>=90; mache-=1) 

          {                                   
            machoire.write(mache);   //petits mouvements de tête pendant qu'il parle
            if (mache % fm ==0) {
              randNumber = random(-1,1);
              tets=tet;
              tet=tet+randNumber;
              tete.write(tet); 
              tet=tets;   
            }        
            delay(4); 

          } 

          compteur++;

        }    

        { 
          delay(500);


          int compteur = 0; 
          while (compteur < 6)  //3eme parole
          { 

            for(mache = 90; mache < 120; mache += 1)   
            {                                   
              machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
              if (mache % fm ==0) {
                randNumber = random(-1,1);
                tets=tet;
                tet=tet+randNumber;
                tete.write(tet); 
                tet=tets;   
              }        
              delay(4); 

            }
            for(mache = 120; mache>=90; mache-=1)      
            {                                   
              machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
              if (mache % fm ==0) {
                randNumber = random(-1,1);
                tets=tet;
                tet=tet+randNumber;
                tete.write(tet); 
                tet=tets;   
              }

              //petits mouvements de bras pendant qu'il parle
              if (mache % fm ==0) {
                randNumber = random(-7,2);
                brabouge=bra;
                bra=bra+randNumber;
                bras.write(bra);  
                bra=brabouge;
              }        
              delay(4); 

            }  

            compteur++;

          }    

          { 
            delay(500);

            int compteur = 0; 
            while (compteur < 7) //4 eme parole demarre a 26s
            { 

              for(mache = 90; mache < 120; mache += 1)   
              {                                   
                machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                if (mache % fm ==0) {
                  randNumber = random(-1,1);
                  tets=tet;
                  tet=tet+randNumber;
                  tete.write(tet); 
                  tet=tets;   
                }        
                delay(4); 

              } 
              for(mache = 120; mache>=90; mache-=1)      
              {                                   
                machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                if (mache % fm ==0) {
                  randNumber = random(-1,1);
                  tets=tet;
                  tet=tet+randNumber;
                  tete.write(tet); 
                  tet=tets;   
                }
                //petits mouvements de bras pendant qu'il parle
                if (mache % fm ==0) {
                  randNumber = random(-7,2);
                  brabouge=bra;
                  bra=bra+randNumber;
                  bras.write(bra);  
                  bra=brabouge;
                }        
                delay(4); 

              }  

              compteur++;

            }  

            { 
              delay(200);

              int compteur = 0; 
              while (compteur < 4)  //5eme parole
              { 

                for(mache = 90; mache < 120; mache += 1)   

                {                                   
                  machoire.write(mache);   //petits mouvements de tête pendant qu'il parle
                  if (mache % fm ==0) {
                    randNumber = random(-1,1);
                    tets=tet;
                    tet=tet+randNumber;
                    tete.write(tet); 
                    tet=tets;   
                  }        
                  delay(4); 

                } 
                for(mache = 120; mache>=90; mache-=1)      

                {                                   
                  machoire.write(mache);   //petits mouvements de tête pendant qu'il parle
                  if (mache % fm ==0) {
                    randNumber = random(-1,1);
                    tets=tet;
                    tet=tet+randNumber;
                    tete.write(tet); 
                    tet=tets;   
                  } 
                  //petits mouvements de bras pendant qu'il parle
                  if (mache % fm ==0) {
                    randNumber = random(-7,2);
                    brabouge=bra;
                    bra=bra+randNumber;
                    bras.write(bra);  
                    bra=brabouge;
                  }       
                  delay(4); 

                }  

                compteur++;

              }

              { 
                delay(250);

                int compteur = 0; 
                while (compteur < 4)  //6eme parole
                { 

                  for(mache = 90; mache < 120; mache += 1)   
                  {                                   
                    machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                    if (mache % fm ==0) {
                      randNumber = random(-1,1);
                      tets=tet;
                      tet=tet+randNumber;
                      tete.write(tet); 
                      tet=tets;   
                    }        
                    delay(4); 

                  }  
                  for(mache = 120; mache>=90; mache-=1)      
                  {                                   
                    machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                    if (mache % fm ==0) {
                      randNumber = random(-1,1);
                      tets=tet;
                      tet=tet+randNumber;
                      tete.write(tet); 
                      tet=tets;    
                    }
                    //petits mouvements de bras pendant qu'il parle
                    if (mache % fm ==0) {
                      randNumber = random(-7,2);
                      brabouge=bra;
                      bra=bra+randNumber;
                      bras.write(bra);  
                      bra=brabouge;
                    }       
                    delay(4); 

                  }
                  compteur++;

                }

                { 
                  delay(150);

                  int compteur = 0; 
                  while (compteur < 4)  //7eme parole
                  { 

                    for(mache = 90; mache < 120; mache += 1)   
                    {                                   
                      machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                      if (mache % fm ==0) {
                        randNumber = random(-1,1);
                        tets=tet;
                        tet=tet+randNumber;
                        tete.write(tet); 
                        tet=tets;   
                      }        
                      delay(4); 

                    } 
                    for(mache = 120; mache>=90; mache-=1)      
                    {                                   
                      machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                      if (mache % fm ==0) {
                        randNumber = random(-1,1);
                        tets=tet;
                        tet=tet+randNumber;
                        tete.write(tet); 
                        tet=tets;   
                      }       
                      delay(4); 

                    }   

                    compteur++;

                  }

                  { 

                    for(tet = 110; tet>=75; tet-=1)   //tourne la tête correspond à une coupure de parole  a 33s
                    {                                 
                      tete.write(tet);                
                      delay(15);
                    } 
                    for(tet = 75; tet < 100; tet += 1)  
                    {                                
                      tete.write(tet);              
                      delay(15);
                    } 

                    int compteur = 0; 
                    while (compteur < 4)   //8eme parole
                    { 

                      for(mache = 90; mache < 120; mache += 1)   
                      {                                   
                        machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                        if (mache % fm ==0) {
                          randNumber = random(-1,1);
                          tets=tet;
                          tet=tet+randNumber;
                          tete.write(tet); 
                          tet=tets;   
                        } 

                        //petits mouvements de bras pendant qu'il parle
                        if (mache % fm ==0) {
                          randNumber = random(-7,2);
                          brabouge=bra;
                          bra=bra+randNumber;
                          bras.write(bra);  
                          bra=brabouge;
                        }       
                        delay(4); 

                      }
                      for(mache = 120; mache>=90; mache-=1)      
                      {                                   
                        machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                        if (mache % fm ==0) {
                          randNumber = random(-1,1);
                          tets=tet;
                          tet=tet+randNumber;
                          tete.write(tet); 
                          tet=tets;    
                        }       
                        delay(4); 

                      } 

                      compteur++;

                    } 

                    { 
                      delay(200);

                      int compteur = 0; 
                      while (compteur < 4)   //9eme parole
                      { 

                        for(mache = 90; mache < 120; mache += 1)   
                        {                                   
                          machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                          if (mache % fm ==0) {
                            randNumber = random(-1,1);
                            tets=tet;
                            tet=tet+randNumber;
                            tete.write(tet); 
                            tet=tets;   
                          } 
                          //petits mouvements de bras pendant qu'il parle
                          if (mache % fm ==0) {
                            randNumber = random(-7,2);
                            brabouge=bra;
                            bra=bra+randNumber;
                            bras.write(bra);  
                            bra=brabouge;
                          }

                          delay(4); 

                        } 
                        for(mache = 120; mache>=90; mache-=1)      
                        {                                   
                          machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                          if (mache % fm ==0) {
                            randNumber = random(-1,1);
                            tets=tet;
                            tet=tet+randNumber;
                            tete.write(tet); 
                            tet=tets;   
                          }        
                          delay(4); 

                        }    

                        compteur++;

                      }

                      { 
                        delay(500);

                        int compteur = 0; 
                        while (compteur < 6)    //10eme parole
                        { 

                          for(mache = 90; mache < 120; mache += 1)   
                          {                                   
                            machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                            if (mache % fm ==0) {
                              randNumber = random(-1,1);
                              tets=tet;
                              tet=tet+randNumber;
                              tete.write(tet); 
                              tet=tets;   
                            }        
                            delay(4); 

                          }  
                          for(mache = 120; mache>=90; mache-=1)      
                          {                                   
                            machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                            if (mache % fm ==0) {
                              randNumber = random(-1,1);
                              tets=tet;
                              tet=tet+randNumber;
                              tete.write(tet); 
                              tet=tets;    
                            }       
                            delay(4); 

                          }   

                          compteur++;

                        }

                        { 
                          delay(500);

                          int compteur = 0; 
                          while (compteur < 5)    //11eme parole
                          { 

                            for(mache = 90; mache < 120; mache += 1)   
                            {                                   
                              machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                              if (mache % fm ==0) {
                                randNumber = random(-1,1);
                                tets=tet;
                                tet=tet+randNumber;
                                tete.write(tet); 
                                tet=tets;  
                              }         
                              delay(4); 

                            }  
                            for(mache = 120; mache>=90; mache-=1)      
                            {                                   
                              machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                              if (mache % fm ==0) {
                                randNumber = random(-1,1);
                                tets=tet;
                                tet=tet+randNumber;
                                tete.write(tet); 
                                tet=tets;   
                              } 

                              //petits mouvements de bras pendant qu'il parle
                              if (mache % fm ==0) {
                                randNumber = random(-7,2);
                                brabouge=bra;
                                bra=bra+randNumber;
                                bras.write(bra);  
                                bra=brabouge;
                              }       
                              delay(4); 

                            }   

                            compteur++;

                          }

                          { 
                            delay(500);

                            int compteur = 0; 
                            while (compteur < 10)    //12eme parole
                            { 

                              for(mache = 90; mache < 120; mache += 1)   
                              {                                   
                                machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                if (mache % fm ==0) {
                                  randNumber = random(-1,1);
                                  tets=tet;
                                  tet=tet+randNumber;
                                  tete.write(tet); 
                                  tet=tets;  
                                }         
                                delay(4); 

                              } 
                              for(mache = 120; mache>=90; mache-=1)      
                              {                                   
                                machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                if (mache % fm ==0) {
                                  randNumber = random(-1,1);
                                  tets=tet;
                                  tet=tet+randNumber;
                                  tete.write(tet); 
                                  tet=tets;   
                                }

                                //petits mouvements de bras pendant qu'il parle
                                if (mache % fm ==0) {
                                  randNumber = random(-7,2);
                                  brabouge=bra;
                                  bra=bra+randNumber;
                                  bras.write(bra);  
                                  bra=brabouge;
                                }        
                                delay(4); 

                              }
                              compteur++;

                            }

                            for(tet = 100; tet>=80; tet-=1)   //tourne la tête correspond à une coupure de parole  a 45 s
                            {                                 
                              tete.write(tet);                
                              delay(30);
                            } 
                            for(tet = 80; tet < 100; tet += 1)  
                            {                                
                              tete.write(tet);              
                              delay(30);
                            } 

                            { 
                              delay(300);


                              int compteur = 0; 
                              while (compteur < 5)     //13eme parole
                              { 

                                for(mache = 90; mache < 120; mache += 1)   
                                {                                   
                                  machoire.write(mache);   //petits mouvements de tête pendant qu'il parle
                                  if (mache % fm ==0) {
                                    randNumber = random(-1,1);
                                    tets=tet;
                                    tet=tet+randNumber;
                                    tete.write(tet); 
                                    tet=tets;   
                                  } 

                                  //petits mouvements de bras pendant qu'il parle
                                  if (mache % fm ==0) {
                                    randNumber = random(-7,2);
                                    brabouge=bra;
                                    bra=bra+randNumber;
                                    bras.write(bra);  
                                    bra=brabouge;
                                  }

                                  delay(4); 

                                } 
                                for(mache = 120; mache>=90; mache-=1)      
                                {                                   
                                  machoire.write(mache);   //petits mouvements de tête pendant qu'il parle
                                  if (mache % fm ==0) {
                                    randNumber = random(-1,1);
                                    tets=tet;
                                    tet=tet+randNumber;
                                    tete.write(tet); 
                                    tet=tets;   
                                  }        
                                  delay(4); 

                                }    

                                compteur++;

                              }

                              { 
                                delay(200);

                                int compteur = 0; 
                                while (compteur < 10)      //14eme parole
                                { 

                                  for(mache = 90; mache < 120; mache += 1)   
                                  {                                   
                                    machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                    if (mache % fm ==0) {
                                      randNumber = random(-1,1);
                                      tets=tet;
                                      tet=tet+randNumber;
                                      tete.write(tet); 
                                      tet=tets;  
                                    }  

                                    //petits mouvements de bras pendant qu'il parle
                                    if (mache % fm ==0) {
                                      randNumber = random(-7,2);
                                      brabouge=bra;
                                      bra=bra+randNumber;
                                      bras.write(bra);  
                                      bra=brabouge;
                                    }       
                                    delay(4); 

                                  } 
                                  for(mache = 120; mache>=90; mache-=1)      
                                  {                                   
                                    machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                    if (mache % fm ==0) {
                                      randNumber = random(-1,1);
                                      tets=tet;
                                      tet=tet+randNumber;
                                      tete.write(tet); 
                                      tet=tets;   
                                    }        
                                    delay(4); 

                                  } 

                                  compteur++;

                                } 

                                { 
                                  delay(800);

                                  int compteur = 0; 
                                  while (compteur < 13)  //15eme parole
                                  { 

                                    for(mache = 90; mache < 120; mache += 1)   
                                    {                                   
                                      machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                      if (mache % fm ==0) {
                                        randNumber = random(-1,1);
                                        tets=tet;
                                        tet=tet+randNumber;
                                        tete.write(tet); 
                                        tet=tets;   
                                      }        
                                      delay(4); 

                                    } 
                                    for(mache = 120; mache>=90; mache-=1)      
                                    {                                   
                                      machoire.write(mache);  //petits mouvements de tête pendant qu'il parle
                                      if (mache % fm ==0) {
                                        randNumber = random(-1,1);
                                        tets=tet;
                                        tet=tet+randNumber;
                                        tete.write(tet); 
                                        tet=tets;   
                                      }        
                                      delay(4); 

                                    }   

                                    compteur++;

                                  }
                                }        
                              }
                            } 
                          } 
                        } 
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    } 

    { 


      for(tet = 100; tet>=60; tet-=1)     
      {                                 
        tete.write(tet);                
        delay(20);
      } 
      for(tet = 60; tet < 80; tet += 1)  
      {                                
        tete.write(tet);              
        delay(20);                        
      }


      delay(1000);

      for(bra = 90; bra>=44; bra-=1) //leve le bras    

      { 

        bras.write(bra);  //petits mouvements de tête
        if (bra % fm ==0) {
          randNumber = random(-5,5);
          tets=tet;
          tet=tet+randNumber;
          tete.write(tet); 
          tet=tets;   
        }        
        delay(30); 

      }

      delay(300);
      int compteur = 0; 
      while (compteur < 11) //bras, 11 va et vient de 8° sur la trompette
      { 

        for(bra = 44; bra < 52; bra += 1)  
        {                                   
          bras.write(bra);  //petits mouvements de tête
          if (bra % fm ==0) {
            randNumber = random(-5,5);
            tets=tet;
            tet=tet+randNumber;
            tete.write(tet); 
            tet=tets;   
          }        
          delay(15); 

        } 

        for(bra = 52; bra>=44; bra-=1)    
        {                                   
          bras.write(bra);  //petits mouvements de tête
          if (bra % fm ==0) {
            randNumber = random(-5,5);
            tets=tet;
            tet=tet+randNumber;
            tete.write(tet); 
            tet=tets;   
          }        
          delay(15); 

        } 

        compteur++;
      } 
      { 
        delay(300);

        for(bra = 44; bra < 90; bra += 1)  //baisse le bras

        {          
          bras.write(bra);  //petits mouvements de tête 
          if (bra % fm ==0) {
            randNumber = random(-5,5);
            tets=tet;
            tet=tet+randNumber;
            tete.write(tet); 
            tet=tets;   
          }        
          delay(20); 

        } 

        delay(1000);

        for(tet = 80; tet>=60; tet-=1)     
        {                                 
          tete.write(tet);                
          delay(20);
        } 
        for(tet = 60; tet < 100; tet += 1)  
        {                                
          tete.write(tet);              
          delay(20); 

        } 
        delay(1000);
        digitalWrite(relai, LOW); //Fermeture relai et lecteur MP3

      } 
    } 
  } 
}