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