Advertisement
granteeric

allume 4 leds avec BTN

Jun 1st, 2022
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //declaration des pins
  2. #define ANTIREBOND              250         //250ms d attente pour l'anti rebond du bouton poussoir
  3. #define PIN_BTNA                2
  4. #define PIN_BTNB                3
  5.  
  6. //on peux aussi faire :
  7. const int PIN_LED1 = 4;
  8. const int PIN_LED2 = 5;
  9. const int PIN_LED3 = 6;
  10. const int PIN_LED4 = 7;
  11.  
  12. //variables
  13. unsigned long time_BtnA = 0;
  14. unsigned long time_BtnB = 0;
  15.  
  16. volatile uint8_t btnA_Appuyer = 0;
  17. volatile uint8_t btnB_Appuyer = 0;
  18. uint8_t nbrAppuieBtn = 0;
  19. uint8_t phase = 0;
  20.  
  21.  
  22. //fonction qui eteint toutes les leds
  23. void eteintLED(){
  24.     digitalWrite(PIN_LED1,LOW);
  25.     digitalWrite(PIN_LED2,LOW);
  26.     digitalWrite(PIN_LED3,LOW);
  27.     digitalWrite(PIN_LED4,LOW);
  28. }
  29.  
  30. void allumeLed(uint8_t numLed){
  31.    
  32.     if(numLed == 1){
  33.         digitalWrite(PIN_LED1,HIGH);
  34.         digitalWrite(PIN_LED2,LOW);
  35.         digitalWrite(PIN_LED3,LOW);
  36.         digitalWrite(PIN_LED4,LOW);
  37.     }
  38.     else if(numLed == 2){
  39.         digitalWrite(PIN_LED1,LOW);
  40.         digitalWrite(PIN_LED2,HIGH);
  41.         digitalWrite(PIN_LED3,LOW);
  42.         digitalWrite(PIN_LED4,LOW);
  43.     }
  44.     else if(numLed == 3){
  45.         digitalWrite(PIN_LED1,LOW);
  46.         digitalWrite(PIN_LED2,LOW);
  47.         digitalWrite(PIN_LED3,HIGH);
  48.         digitalWrite(PIN_LED4,LOW);
  49.     }    
  50.     else if(numLed == 4){
  51.         digitalWrite(PIN_LED1,LOW);
  52.         digitalWrite(PIN_LED2,LOW);
  53.         digitalWrite(PIN_LED3,LOW);
  54.         digitalWrite(PIN_LED4,HIGH);
  55.     }    
  56.  
  57. }
  58.  
  59. void btnAPress(){
  60.     btnA_Appuyer = 1;
  61. }
  62.  
  63. void btnBPress(){
  64.     btnB_Appuyer = 1 ;
  65. }
  66.  
  67. void setup(){
  68.     //configuration des de PIN :
  69.     pinMode(PIN_BTNA, INPUT);
  70.     pinMode(PIN_BTNB, INPUT);
  71.     pinMode(PIN_LED1, OUTPUT);
  72.     pinMode(PIN_LED2, OUTPUT);
  73.     pinMode(PIN_LED3, OUTPUT);
  74.     pinMode(PIN_LED4, OUTPUT);
  75.    
  76.     eteintLED();
  77.  
  78.     //on utilise les interruption de la pin digital 2 et de digital 3 pour déclencher des actions.
  79.             //RISING quand la pin passe en front montant vers 1
  80.             // en pull down le 5V correspond au bouton appuyer
  81.             //btnAPress est la fonction qui est appelée quand l'interruption est déclenchée
  82.     attachInterrupt(digitalPinToInterrupt(PIN_BTNA),btnAPress,RISING);
  83.            
  84.             // FALLING quand la pin passe en front descendant vers 0V
  85.             // en pull up 0V correspond au bouton appuyer
  86.             //btnBPress est la fonction qui est appelée quand l'interruption est déclenchée
  87.     attachInterrupt(digitalPinToInterrupt(PIN_BTNB),btnBPress,FALLING);
  88. }
  89.  
  90. void loop(){
  91.  
  92.     if(btnA_Appuyer && (( millis() - time_BtnA ) >= ANTIREBOND) ){
  93.         time_BtnA = millis();   //on réinitialise le temps du btn A
  94.         btnA_Appuyer = 0;       // on remet le bouton A a no appuyer
  95.         nbrAppuieBtn++;            //on incremente le nbr appuer sur le bouton de 1
  96.     }
  97.  
  98.     if(btnB_Appuyer && (( millis() - time_BtnB ) >= ANTIREBOND) ){
  99.         time_BtnB = millis();   //on réinitialise le temps du btn B
  100.         btnB_Appuyer = 0;       // on remet le bouton B a no appuyer
  101.         nbrAppuieBtn = 0 ;      //on remet le nbr Appui btn à 0 car on fait un reset
  102.         phase = 0 ;             //on remet phase à 0
  103.         //on eteint toutes les leds
  104.         eteintLED();
  105.     }
  106.  
  107.     if(nbrAppuieBtn == 2 && phase != 1){
  108.         phase = 1;
  109.         allumeLed(phase);  //on allume la led 1
  110.     }
  111.     else if(nbrAppuieBtn == 4 && phase != 2){
  112.         phase = 2;
  113.         allumeLed(phase);  //on allume la led 2
  114.     }
  115.     else if(nbrAppuieBtn == 6 && phase != 3){
  116.         phase = 2;
  117.         allumeLed(phase);  //on allume la led 3
  118.     }
  119.     else if(nbrAppuieBtn == 8 && phase != 4){
  120.         phase = 2;
  121.         allumeLed(phase);  //on allume la led 4
  122.     }    
  123.     else if(nbrAppuieBtn > 8 && phase == 4){
  124.         phase = 0;
  125.         nbrAppuieBtn = 1;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement