
Dans ce tutoriel nous allons voir comment afficher le sens de la direction de la manette Joystick sur un afficheur LCD avec Arduino:
Pour réaliser le montage on connecte:
Pour la manette joystick :
La broche +5V à 3.3V de l’Arduino
La broche GND au GND de l’Arduino
VRx à la borne analogique A0 de l’Arduino
VRy à la borne analogique A1 de l’Arduino
SW à la borne numérique N°2 de l’Arduino
Pour l’afficheur LCD :
La broche VCC à 5V de l’Arduino
La broche GND au GND de l’Arduino
la broche SDA à la borne analogique A4 de l’Arduino
La broche SCL à la borne analogique A5 de l’Arduino
#include <LiquidCrystal_I2C.h>
//—– Adressage matériel —–
// En cas de non fonctionnement, mettez la ligne 8 en
// commentaire et retirez le commentaire à la ligne 9.
LiquidCrystal_I2C lcd(0x27, 20, 4);
int VRx = A0;
int VRy = A1;
int SW = 2;
int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
void setup() {
lcd.init(); // initialisation de l’afficheur
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
}
void loop() {
lcd.backlight();
lcd.clear(); // effacer le contenu de l’Afficheur LCD
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
SW_state = digitalRead(SW);
mapX = map(xPosition, 0, 1023, -512, 512);
mapY = map(yPosition, 0, 1023, -512, 512);
if ((mapX>=-515)&& (mapX<=-510)&&(mapY>=-175)&& (mapY<=-168))
lcd.print(“Avant”);
if ((mapX>=168)&& (mapX<=175)&&(mapY>=-175)&& (mapY<=-168))
lcd.print(“Arriere”);
if ((mapX<=-166)&& (mapX>=-175)&&(mapY>=-515)&& (mapY<=-510))
lcd.print(“Droite”);
if ((mapX>=-175)&& (mapX<=-170)&&(mapY>=165)&& (mapY<=175))
lcd.print(“Gauche”);
if (SW_state==0)
lcd.print(“Bouton appuye”);
delay(1000);
}
Remarque: il faut télécharger la bibliothèque LiquidCrystal_I2C (télécharger).