Bounce2

Bonjour.

C'est la première fois que j'utilise BOUNCE2.
J'ai 4 boutons sur 4 entrées digitales.
Est-ce que quelqu'un peu me dire si grâce au code suivant j'éliminerai les rebonds sur les boutons poussoirs.

#include <Bounce2.h>

int NumeroBouton;

const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 6;

Bounce pushbutton1 = Bounce(buttonPin1, 10);
Bounce pushbutton2 = Bounce(buttonPin2, 10);
Bounce pushbutton3 = Bounce(buttonPin3, 10);
Bounce pushbutton4 = Bounce(buttonPin4, 10);

void setup() {

  Serial.begin(9600);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  pushbutton1.attach(buttonPin1);
  pushbutton2.attach(buttonPin2);
  pushbutton3.attach(buttonPin3);
  pushbutton4.attach(buttonPin4);

  pinMode(8 , OUTPUT);
  pinMode(9 , OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

}

void loop() {
  pushbutton1.update();
  pushbutton2.update();
  pushbutton3.update();
  pushbutton4.update();

  int Bouton1 = pushbutton1.read();
  int Bouton2 = pushbutton2.read();
  int Bouton3 = pushbutton3.read();
  int Bouton4 = pushbutton4.read();

  if (Bouton2 == 0 && Bouton3 == 0 && Bouton4 == 0) {
    if (Bouton1 == 1) {
      NumeroBouton = 1;
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
  }

  if (Bouton1 == 0 && Bouton3 == 0 && Bouton4 == 0) {
    if (Bouton2 == 1) {
      NumeroBouton = 2;
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
  }

  if (Bouton1 == 0 && Bouton2 == 0 && Bouton4 == 0) {
    if (Bouton3 == 1) {
      NumeroBouton = 3;
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
    }
  }

  if (Bouton1 == 0 && Bouton2 == 0 && Bouton3 == 0) {
    if (Bouton4 == 1) {
      NumeroBouton = 4;
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
    }
  }

  if (Bouton1 == 0 && Bouton2 == 0 && Bouton3 == 0 && Bouton4 == 0) {
    NumeroBouton = 0;

    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }

  Serial.println(NumeroBouton);
}

10 ms c'est peut-être un peu juste ?

Tu peux optimiser ton code pour l'allumage des leds

    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    byte somme = Bouton1 + Bouton2 + Bouton3 + Bouton4;
    if (somme == 0) NumeroBouton = 0;
    else if (somme == 1) {
       if (Bouton1) NumeroBouton = 1;
       if (Bouton2) NumeroBouton = 2;
       if (Bouton3) NumeroBouton = 3;
       if (Bouton4) NumeroBouton = 4;
      digitalWrite(7+NumeroBouton, HIGH);
}
    Serial.println(NumeroBouton);

Bonjour

Programme équivalent, avec mes libs (cf lien dans ma signature)
Difficile de faire plus simple

#include "simpleLed.h"
#include "simpleBouton.h"

simpleLed led1(8);//Cablage pin---Led---R---GND
simpleLed led2(9);
simpleLed led3(10);
simpleLed led4(11);

simpleBouton bouton1(3);//Cablage : pin---BP---GND
simpleBouton bouton2(4);
simpleBouton bouton3(5);
simpleBouton bouton4(6);

void setup() {}
void loop() {
  bouton1.actualiser();
  bouton2.actualiser();
  bouton3.actualiser();
  bouton4.actualiser();
  led1.commander(bouton1.estEnfonce() && bouton2.estRelache() && bouton3.estRelache() && bouton4.estRelache());
  led2.commander(bouton1.estRelache() && bouton2.estEnfonce() && bouton3.estRelache() && bouton4.estRelache());
  led3.commander(bouton1.estRelache() && bouton2.estRelache() && bouton3.estEnfonce() && bouton4.estRelache());
  led4.commander(bouton1.estRelache() && bouton2.estRelache() && bouton3.estRelache() && bouton4.estEnfonce());
}