/* 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); }