/*
Créé par ArduinoGetStarted.com
Cet exemple de code est dans le domaine public.
Page du tutoriel : https://arduinogetstarted.com/tutorials/arduino-button-library
Cet exemple lit l'état des 7 boutons avec anti-rebond et l'affiche sur le moniteur.
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <ezButton.h>
ezButton redButton(2); // create ezButton object that attach to pin 7;
ezButton greenButton(3); // create ezButton object that attach to pin 7;
ezButton blueButton(4); // create ezButton object that attach to pin 7;
ezButton yellowButton(5); // create ezButton object that attach to pin 7;
ezButton blackButton(6); // create ezButton object that attach to pin 7;
ezButton whiteButton(7); // create ezButton object that attach to pin 7;
ezButton greyButton(8); // create ezButton object that attach to pin 7;
String btn = "", btnState = "";
unsigned long count = 0;
int RedButton_State,
greenButton_State,
blueButton_State,
yellowButton_State,
blackButton_State,
whiteButton_State,
greyButton_State;
char* bouton[] = {" The Red button",
" The green Button",
" The blue Button",
"The yellow Button",
" The black Button",
" The white Button",
" The grey Button"
};
char* etat[] = {" is pressed ",
"is released "
};
void setup() {
lcd.init(); lcd.backlight();
for (int pin = 2; pin < 9; pin++)pinMode(pin, INPUT_PULLUP);
// définir le temps de rebond à 50 millisecondes à chaque bouton.
redButton.setDebounceTime(50); // set debounce time to 50 milliseconds
greenButton.setDebounceTime(50); // set debounce time to 50 milliseconds
blueButton.setDebounceTime(50); // set debounce time to 50 milliseconds
yellowButton.setDebounceTime(50); // set debounce time to 50 milliseconds
blackButton.setDebounceTime(50); // set debounce time to 50 milliseconds
whiteButton.setDebounceTime(50); // set debounce time to 50 milliseconds
greyButton.setDebounceTime(50); // set debounce time to 50 milliseconds
redButton.setCountMode(COUNT_FALLING);
greenButton.setCountMode(COUNT_FALLING);
blueButton.setCountMode(COUNT_FALLING);
yellowButton.setCountMode(COUNT_FALLING);
blackButton.setCountMode(COUNT_FALLING);
whiteButton.setCountMode(COUNT_FALLING);
greyButton.setCountMode(COUNT_FALLING);
}
void loop() {
checkAllButtons();
}
void checkAllButtons() {
redButton.loop(); // DOIT d'abord appeler la fonction loop()
greenButton.loop(); // DOIT d'abord appeler la fonction loop()
blueButton.loop(); // DOIT d'abord appeler la fonction loop()
yellowButton.loop(); // DOIT d'abord appeler la fonction loop()
blackButton.loop(); // DOIT d'abord appeler la fonction loop()
whiteButton.loop(); // DOIT d'abord appeler la fonction loop()
greyButton.loop(); // DOIT d'abord appeler la fonction loop()
RedButton_State = redButton.getState();
greenButton_State = greenButton.getState();
blueButton_State = blueButton.getState();
yellowButton_State = yellowButton.getState();
blackButton_State = blackButton.getState();
whiteButton_State = whiteButton.getState();
greyButton_State = greyButton.getState();
if (redButton.isPressed()) {
btn = bouton[0]; btnState = etat[RedButton_State];
count = redButton.getCount();
}
if (redButton.isReleased()) {
btn = bouton[0]; btnState = etat[RedButton_State];
}
if (greenButton.isPressed()) {
btn = bouton[1]; btnState = etat[greenButton_State];
count = greenButton.getCount();
}
if (greenButton.isReleased()) {
btn = bouton[1]; btnState = etat[greenButton_State];
}
if (blueButton.isPressed()) {
btn = bouton[2]; btnState = etat[blueButton_State];
count = blueButton.getCount();
}
if (blueButton.isReleased()) {
btn = bouton[2]; btnState = etat[blueButton_State];
}
if (yellowButton.isPressed()) {
btn = bouton[3]; btnState = etat[yellowButton_State];
count = yellowButton.getCount();
}
if (yellowButton.isReleased()) {
btn = bouton[3]; btnState = etat[yellowButton_State];
}
if (blackButton.isPressed()) {
btn = bouton[4]; btnState = etat[blackButton_State];
count = blackButton.getCount();
}
if (blackButton.isReleased()) {
btn = bouton[4]; btnState = etat[blackButton_State];
}
if (whiteButton.isPressed()) {
btn = bouton[5]; btnState = etat[whiteButton_State];
count = whiteButton.getCount();
}
if (whiteButton.isReleased()) {
btn = bouton[5]; btnState = etat[whiteButton_State];
}
if (greyButton.isPressed()) {
btn = bouton[6]; btnState = etat[greyButton_State];
count = greyButton.getCount();
}
if (greyButton.isReleased()) {
btn = bouton[6]; btnState = etat[greyButton_State];
}
char btnCount[3]; sprintf(btnCount,"%3d time(s)", count);
lcdPrintCenter(btn, 0);
lcdPrintCenter(btnState, 1);
lcdPrintCenter(btnCount, 2);
}
void lcd_print(int x, int y, String message) {
lcd.setCursor(x, y); lcd.print(message);
}
// fonction pour centrer le texte à afficher sur lcd.============
void lcdPrintCenter(String text, int ligne) {
int len = text.length();// longeur de la variable text
// positionne le curseur au centre de la ligne choisie et
// soustrait la longeur du text à afficher
lcd.setCursor((20 - len) / 2, ligne);
lcd.print(text); // affiche le text centré
}