granteeric

declenchement moteur apres detection cellule

Jul 30th, 2022
1,547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************ déclenchement moteur après action d'un cellule avec temporisation ***************************/
  2.  
  3. #define PIN_DETECTEUR       2               // pin 2 a l'interruption extern 0  ou peut prendre aussi la pin 3.
  4. #define PIN_MOTEUR          4               // pin digital 4 ou autres
  5. #define PIN_POT_TEMPS       A0              // pin ADC
  6.  
  7. #define AVEC_POT_TEMPS      1               // 0 si pas de potentiometre - 1 avec
  8. #define DETECTEUR_PULLUP    1               // 1 PULLUP le level HIGH ou 1 est par defaut, LOW ou 0 indique qu'il y a un contact
  9.  
  10. unsigned long tempsEcoule = 0;        
  11. volatile unsigned long tempsAntiRebond = 0;
  12. volatile unsigned int dureeAntiRebond = 250;
  13. unsigned int tempsDeclenchement = 5000;     //5000 ms soit 5 secondes
  14. volatile int startDetection = 0;            //volatile pour que la variable soit relue dans tous les contextes, 0 pas de détection commencer, 1 detection commencer
  15. volatile int finBlocage = 0 ;
  16. volatile int lancementDecomptage = 0 ;
  17.  
  18. int moteurActive = 0;
  19. volatile unsigned long tempsMoteurActive = 0;        
  20. unsigned int dureeMoteurApresFinBlocage = 3000;      //temps de fonctionnement du moteur après avoir détecté le déblocage
  21.  
  22.  
  23. void fnDetection();
  24. void fnFinDetection();
  25. void initVariable();
  26. #if AVEC_POT_TEMPS == 1
  27.   void reglageTemporisationDureeBlocage();
  28. #endif
  29.  
  30. void setup() {
  31.  
  32.   //initialisation des entrées et sorties
  33.   pinMode(PIN_DETECTEUR, INPUT);
  34.   pinMode(PIN_MOTEUR, OUTPUT);
  35.   pinMode(PIN_POT_TEMPS, INPUT);
  36.  
  37.   attachInterrupt(digitalPinToInterrupt(PIN_DETECTEUR),fnDetection, CHANGE );
  38.  
  39. }
  40.  
  41. void loop() {
  42.  
  43.   if(startDetection && !lancementDecomptage){
  44.  
  45.     #if AVEC_POT_TEMPS == 1
  46.       reglageTemporisationDureeBlocage();
  47.     #endif
  48.  
  49.     tempsEcoule = millis();
  50.     lancementDecomptage = 1;
  51.   }
  52.  
  53.  
  54.   else if( ((millis() - tempsEcoule ) > tempsDeclenchement) && lancementDecomptage && !moteurActive){
  55.     // il est temps de demarrer le moteur
  56.     digitalWrite(PIN_MOTEUR, HIGH);
  57.     moteurActive = 1;
  58.    }
  59.  
  60.    else if( ((millis() - tempsMoteurActive ) > dureeMoteurApresFinBlocage) && moteurActive && finBlocage){
  61.       //on arrete le moteur
  62.       digitalWrite(PIN_MOTEUR , LOW);
  63.       initVariable();
  64.    }
  65.    
  66.    else if( ((millis() - tempsEcoule ) > 250) && lancementDecomptage && finBlocage && !moteurActive){
  67.     // le carton c'est débloqué avant le délai de déclenchement moteur
  68.       initVariable();
  69.   }
  70.  
  71. }
  72.  
  73.  
  74.  
  75. void fnDetection(){
  76.    if( !digitalRead(PIN_DETECTEUR) && (millis() - tempsAntiRebond) > dureeAntiRebond ){
  77.       tempsAntiRebond = millis();
  78.       if (!startDetection){
  79.      startDetection = 1;      //on passe la variable start detection a 1 quand il y a une detection faite
  80.       }
  81.    }
  82.    else if(startDetection && lancementDecomptage) {
  83.       finBlocage = 1;
  84.       tempsMoteurActive = millis();
  85.    }
  86. }
  87.  
  88. void fnFinDetection(){
  89.   if(startDetection && lancementDecomptage) finBlocage = 1;
  90. }
  91.  
  92. void initVariable(){
  93.   startDetection = 0;
  94.   finBlocage = 0;
  95.   lancementDecomptage = 0;
  96.   moteurActive = 0;
  97. }
  98.  
  99. #if AVEC_POT_TEMPS == 1
  100.   void reglageTemporisationDureeBlocage(){
  101.     int valeurPot =  analogRead(PIN_POT_TEMPS);
  102.     if (valeurPot < 300)                          tempsDeclenchement = 1000;
  103.     else if (valeurPot >= 300 && valeurPot < 400) tempsDeclenchement = 2000;
  104.     else if (valeurPot >= 400 && valeurPot < 500) tempsDeclenchement = 3000;
  105.     else if (valeurPot >= 500 && valeurPot < 600) tempsDeclenchement = 4000;
  106.     else if (valeurPot >= 600 && valeurPot < 700) tempsDeclenchement = 5000;
  107.     else if (valeurPot >= 700 && valeurPot < 800) tempsDeclenchement = 6000;
  108.     else if (valeurPot >= 800 && valeurPot < 900) tempsDeclenchement = 7000;
  109.     else if (valeurPot >= 900 )                   tempsDeclenchement = 8000;
  110.   }
  111. #endif
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment