Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //declaration des pins
- #define ANTIREBOND 250 //250ms d attente pour l'anti rebond du bouton poussoir
- #define PIN_BTNA 2
- #define PIN_BTNB 3
- //on peux aussi faire :
- const int PIN_LED1 = 4;
- const int PIN_LED2 = 5;
- const int PIN_LED3 = 6;
- const int PIN_LED4 = 7;
- //variables
- unsigned long time_BtnA = 0;
- unsigned long time_BtnB = 0;
- volatile uint8_t btnA_Appuyer = 0;
- volatile uint8_t btnB_Appuyer = 0;
- uint8_t nbrAppuieBtn = 0;
- uint8_t phase = 0;
- //fonction qui eteint toutes les leds
- void eteintLED(){
- digitalWrite(PIN_LED1,LOW);
- digitalWrite(PIN_LED2,LOW);
- digitalWrite(PIN_LED3,LOW);
- digitalWrite(PIN_LED4,LOW);
- }
- void allumeLed(uint8_t numLed){
- if(numLed == 1){
- digitalWrite(PIN_LED1,HIGH);
- digitalWrite(PIN_LED2,LOW);
- digitalWrite(PIN_LED3,LOW);
- digitalWrite(PIN_LED4,LOW);
- }
- else if(numLed == 2){
- digitalWrite(PIN_LED1,LOW);
- digitalWrite(PIN_LED2,HIGH);
- digitalWrite(PIN_LED3,LOW);
- digitalWrite(PIN_LED4,LOW);
- }
- else if(numLed == 3){
- digitalWrite(PIN_LED1,LOW);
- digitalWrite(PIN_LED2,LOW);
- digitalWrite(PIN_LED3,HIGH);
- digitalWrite(PIN_LED4,LOW);
- }
- else if(numLed == 4){
- digitalWrite(PIN_LED1,LOW);
- digitalWrite(PIN_LED2,LOW);
- digitalWrite(PIN_LED3,LOW);
- digitalWrite(PIN_LED4,HIGH);
- }
- }
- void btnAPress(){
- btnA_Appuyer = 1;
- }
- void btnBPress(){
- btnB_Appuyer = 1 ;
- }
- void setup(){
- //configuration des de PIN :
- pinMode(PIN_BTNA, INPUT);
- pinMode(PIN_BTNB, INPUT);
- pinMode(PIN_LED1, OUTPUT);
- pinMode(PIN_LED2, OUTPUT);
- pinMode(PIN_LED3, OUTPUT);
- pinMode(PIN_LED4, OUTPUT);
- eteintLED();
- //on utilise les interruption de la pin digital 2 et de digital 3 pour déclencher des actions.
- //RISING quand la pin passe en front montant vers 1
- // en pull down le 5V correspond au bouton appuyer
- //btnAPress est la fonction qui est appelée quand l'interruption est déclenchée
- attachInterrupt(digitalPinToInterrupt(PIN_BTNA),btnAPress,RISING);
- // FALLING quand la pin passe en front descendant vers 0V
- // en pull up 0V correspond au bouton appuyer
- //btnBPress est la fonction qui est appelée quand l'interruption est déclenchée
- attachInterrupt(digitalPinToInterrupt(PIN_BTNB),btnBPress,FALLING);
- }
- void loop(){
- if(btnA_Appuyer && (( millis() - time_BtnA ) >= ANTIREBOND) ){
- time_BtnA = millis(); //on réinitialise le temps du btn A
- btnA_Appuyer = 0; // on remet le bouton A a no appuyer
- nbrAppuieBtn++; //on incremente le nbr appuer sur le bouton de 1
- }
- if(btnB_Appuyer && (( millis() - time_BtnB ) >= ANTIREBOND) ){
- time_BtnB = millis(); //on réinitialise le temps du btn B
- btnB_Appuyer = 0; // on remet le bouton B a no appuyer
- nbrAppuieBtn = 0 ; //on remet le nbr Appui btn à 0 car on fait un reset
- phase = 0 ; //on remet phase à 0
- //on eteint toutes les leds
- eteintLED();
- }
- if(nbrAppuieBtn == 2 && phase != 1){
- phase = 1;
- allumeLed(phase); //on allume la led 1
- }
- else if(nbrAppuieBtn == 4 && phase != 2){
- phase = 2;
- allumeLed(phase); //on allume la led 2
- }
- else if(nbrAppuieBtn == 6 && phase != 3){
- phase = 2;
- allumeLed(phase); //on allume la led 3
- }
- else if(nbrAppuieBtn == 8 && phase != 4){
- phase = 2;
- allumeLed(phase); //on allume la led 4
- }
- else if(nbrAppuieBtn > 8 && phase == 4){
- phase = 0;
- nbrAppuieBtn = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement