Ce code ajoute aussi des caractères spéciaux et la valeur mesurée des résistances.
Code : Tout sélectionner
/*
Test LCD 5 boutons DFRobot
Ch.Aubert janv.2012
*/
/************* Librairie(s) à inclure ****************/
#include <LiquidCrystal.h>
/**************** Définir des caractères spéciaux LCD (8 caractères max) ********************/
byte Char_UP[8] = { // Flèche haut (tableau de 8 octets)
B00000, // définition de chaque octet au format binaire
B00000, // 1 pour pixel affiché - 0 pour pixel éteint
B00100, // les 3 bits de poids forts ne sont pas écrits car inutiles
B01110,
B11111,
B00000,
B00000,
B00000
};
//
byte Char_DOWN[8] = { // Flèche bas
B00000,
B00000,
B11111,
B01110,
B00100,
B00000,
B00000,
B00000
};
//
byte Char_RIGHT[8] = { // Flèche droite
B00000,
B00000,
B10000,
B11000,
B11100,
B11000,
B10000,
B00000
};
//
byte Char_LEFT[8] = { // Flèche gauche
B00000,
B00000,
B00001,
B00011,
B00111,
B00011,
B00001,
B00000
};
//
byte Char_SELECT[8] = { // Select
B00000,
B00000,
B01110,
B01110,
B01110,
B01110,
B00000,
B00000
};
//
byte Char_NONE[8] = { // Char.vide
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
/**************** Fin caractères spéciaux LCD ********************/
/************* Définition des broches (pins) du LCD 5 boutons DFRobot ****************/
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const int PinLCDBacklight = 10; // broche LCD Backlight
const int PinBouton = 0; // broche analogique (A0) utisé pour les 5 boutons
/************* Constantes programme ****************/
// boutons
const int btnNONE = 0;
const int btnRIGHT = 1;
const int btnLEFT = 2;
const int btnUP = 3;
const int btnDOWN = 4;
const int btnSELECT = 5;
//texte boutons
const char bt_txt[6][10] = {
"Bt.nul",
"Bt.droite",
"Bt.gauche",
"Bt.haut",
"Bt.bas",
"Bt.select" };
/************* Variables ****************/
int adc_key_in = 0;
int lcd_key = 0;
int old_lcd_key = 0;
void setup()
{
lcd.begin(16, 2); // initialiser la LCD library, 16 caractères, 2 lignes
// création car. spéciaux LCD
lcd.createChar(0, Char_NONE); // char vide pour bt.none
lcd.createChar(1, Char_RIGHT);
lcd.createChar(2, Char_LEFT);
lcd.createChar(3, Char_UP);
lcd.createChar(4, Char_DOWN);
lcd.createChar(5, Char_SELECT);
}
void loop() {
lcd_key = read_LCD_buttons(); // lire la valeur du bouton (->fonction)
for (int i=0; i<=5; i++)
{
lcd.setCursor(0,0); // placer le curseur à la première ligne, première position
lcd.print(bt_txt[lcd_key]); // txt. boutons
lcd.write(lcd_key); // no. symboles spéciaux LCD
lcd.print(" ");
lcd.print(adc_key_in); // valeur A/D résistance bouton
lcd.print(" ");
if (lcd_key > 0) old_lcd_key = lcd_key;
} //end for
// Dernier bouton utilisé
if (old_lcd_key > 0){
lcd.setCursor(0,1);
lcd.print("Dernier bt = ");
lcd.write(old_lcd_key); // car. spéciaux lcd
lcd.print(" ");
}
}
/************* Fonctions ****************/
// Lire la valeur A/D retournée selon le bouton choisi
int read_LCD_buttons()
{
adc_key_in = analogRead(PinBouton);
delay(50); // wait for debounce time
if (adc_key_in > 1000) return btnNONE; // On commence par la valeur la plus probable : Aucun appui sur bouton !
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // si aucun appui sur bouton, on retoune la constante btnNONE ...
}