Matrice led/joystick
Sommaire
Afficher des LEDs avec un joystick (A compléter)
Voici un tuto pour commander des LEDs à l'aide d'un joystick. Il s'agit de déplacer le point lumineux sur une "matrice" de LEDs, en l'occurrence 2 X 4 Leds (on commence tout petit :) ).
A terme le but serait de réaliser un panneau d'affichage mural du style 10 X 10 leds, sur lequel on pourrait jouer à Pong, faire défiler du texte, etc.
Ici le programme permet de déplacer la led active sur la grille en poussant la manette respectivement en haut, en bas ou vers la gauche ou la droite.
Composants
Pour ce tuto vous aurez besoin des composants suivants:
* Arduino Uno (ou Mega, peu immporte) * le câble d'alimentation USB de l'Arduino * un joystick type manette de PS2 * 8 LEDs (monochromes ou pas peu importe) * 8 résistances 220 ou 380 ohms (j'ai utilisé les deux indifféremment) * un registre 74HC595 * des jumpers (des câbles) * un violon
Les branchements
(A venir : un schéma Fritzing)
Brancher chaque cathode (le plus, la patte la plus longue) des LEDs sur chaque sortie du registre (Q0, Q1 etc.) en insérant une résistance entre les deux comme ceci :
Q0 Q1 Q2 Q3 R R R R L L L L
Q4 Q5 Q6 Q7 R R R R L L L L
de façon à avoir une grille de deux lignes de 4 leds.
Pour rappel voici un schéma du registre :
Brancher les anodes de chaque led à la terre.
Connecter le regitre à l'Arduino comme décrit dans le tuto “ShiftOut” d'Arduino http://www.arduino.cc/en/Tutorial/ShiftOut
Programme
LEDs_joystick2.ino
/*
Exemple d'affichage de LEDs
contrôlé par télécommande InfraRouge
Largement inspiré du "Shift Register Example"
disponible ici :
http://www.arduino.cc/en/Tutorial/ShiftOut
et du "Using an IR Sensor"
disponible ici :
http://learn.adafruit.com/ir-sensor/using-an-ir-sensor
Ce programme lit l'entrée du capteur IR et s'en sert pour afficher des
LEDs sélectivement dans une ligne à l'aide d'un registre 74HC595.
Créé le 22/09/13
par Frédéric Lassinot
*/
//Pin connecté au latch pin (ST_CP) du 74HC595
const int latchPin = 8;
//Pin connecté au clock pin (SH_CP) du 74HC595
const int clockPin = 12;
//Pin conencté au Data in (DS) du 74HC595
const int dataPin = 11;
const int XMAX = 3;
const int YMAX = 1;
int positionX = 0;
int positionY = 0;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println("reset");
// Reinit
registerWrite(48, LOW);
}
void loop() {
// Réception du signal
int valueX = analogRead(0);
Serial.print("X:");
Serial.println(valueX, DEC);
int valueY = analogRead(1);
Serial.print(" | Y:");
Serial.println(valueY, DEC);
delay(30);
// Correspondance de la valeur reçue et de la LED à activer
if (valueX > 600) {
// On empêche d'atteindre le max
if (positionX < XMAX) {
positionX++;
}
} else if (valueX < 500) {
// On empêche d'atteindre le max
if (positionX > 0) {
positionX--;
}
}
if (valueY > 600) {
// On empêche d'atteindre le max
if (positionY < YMAX) {
positionY++;
}
} else if (valueY < 500) {
if (positionY > 0) {
positionY--;
}
}
Serial.print("positionX = ");
Serial.println(positionX);
Serial.print("positionY = ");
Serial.println(positionY);
// Affichage de la LED
// Correspondance entre les positions des 8 LEDs (Q0, ... Q7)
// et les positionX et Y
int position = 0;
if (positionY > 0) {
position = 4 + positionX;
} else {
position = positionX;
}
Serial.print("position = ");
Serial.println(position);
// write to the shift register with the correct bit set high:
registerWrite(position, HIGH);
}
// This method sends bits to the shift register:
void registerWrite(int pos, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
// (récupère simplement la puissance de 2 correspondant à pos)
bitWrite(bitsToSend, pos, whichState);
Serial.println();
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
Note : le delay(100) peut être réglé pour rendre le mouvement plus fluide (diminuer la valeur) ou plus lent mais plus contrôlable (augmenter la valeur).
Foire Aux Questions
Si vous avez des questions, posez-les ici!\\ Retour aux tutos Arduino
premier essai
lien vers schema?!\\ dot_matrix_joystick.ino
/*
Affichage d'une matrice à LEDs (dot matrix)
contrôlé par Joystick
Ce code est dans le domaine public
*/
const int latchPin = 8;
const int dataPin = 11;
const int clockPin = 12;
const int x_pin = 0;
const int y_pin = 1;
// cursor position:
int x = 5;
int y = 5;
const int XMAX = 7;
const int YMAX = 7;
int positionX = 0;
int positionY = 0;
void setup() {
// initialisation des pins de sortie:
pinMode(8, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
// Réception du signal
readSensors();
// Affichage
refreshScreen();
}
void readSensors() {
int valueX = analogRead(x_pin);
Serial.print("X:");
Serial.println(valueX, DEC);
int valueY = analogRead(y_pin);
Serial.print(" | Y:");
Serial.println(valueY, DEC);
delay(100);
// Correspondance de la valeur reçue et de la LED à activer
if (valueX > 600) {
// On empêche d'atteindre le max
if (positionX < XMAX) {
positionX++;
}
} else if (valueX < 500) {
// On empêche d'atteindre le max
if (positionX > 0) {
positionX--;
}
}
if (valueY > 600) {
// On empêche d'atteindre le max
if (positionY < YMAX) {
positionY++;
}
} else if (valueY < 500) {
if (positionY > 0) {
positionY--;
}
}
Serial.print("positionX = ");
Serial.println(positionX);
Serial.print("positionY = ");
Serial.println(positionY);
}
void refreshScreen() {
// Transforme positionX en bianire
unsigned int xBitsToSend = 0;
unsigned int yBitsToSend = 0;
bitWrite(xBitsToSend, positionX, HIGH);
bitWrite(yBitsToSend, positionY, HIGH);
Serial.println("bitWrite");
// on envoie l'inverse des bits pour les colonnes
// (la position est donnée par le 0 et pas par les 1)
// on utilise un opérateur d'inversion
int mask = 0xFFFF;
yBitsToSend ^= mask;
Serial.print("xBitsToSend = ");
Serial.println(xBitsToSend);
Serial.println();
Serial.print("yBitsToSend = ");
Serial.println(yBitsToSend);
Serial.println();
digitalWrite(latchPin, LOW);
// Ligne
shiftOut(dataPin, clockPin, LSBFIRST, yBitsToSend);
// Colonne
shiftOut(dataPin, clockPin, LSBFIRST, xBitsToSend);
digitalWrite(latchPin, HIGH);
delay(2);
}
Foire Aux Questions
Si vous avez des questions, posez-les ici!\\ Retour aux tutos Arduino