/** * Boite à samples avec une carte Arduino et un module MP3 KT403A. * Permet de lire 12 échantillons audio via 12 boutons poussoir. */ /* Constantes pour les boutons */ const byte BUTTONS_PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A0 }; const byte BUTTONS_COUNT = 12; /* Constantes pour le reste du montage */ const byte STATUS_LED_PIN = 13; /* Constantes pour la communication avec le module KT403A */ const byte COMMAND_BYTE_START = 0x7E; const byte COMMAND_BYTE_VERSION = 0xFF; const byte COMMAND_BYTE_STOP = 0xEF; /* Constantes pour la commande selectSourceDevice() */ const byte DEVICE_UDISK = 0x01; const byte DEVICE_SDCARD = 0x02; /** Reset the MP3 module */ void resetPlayer() { send_kt_command(0x0C, 0, 0, 100); } /** Select the source device for playing */ void selectSourceDevice(byte device) { send_kt_command(0x09, 0, device, 200); } /** Set the volume to the given level (0 ~ 30) */ void setVolume(byte volume) { if (volume > 30) volume = 30; send_kt_command(0x06, 0, volume, 10); } /** Select the source track from the "MP3" (case insensitive) folder */ void selectSourceTrackInMp3Directory(uint16_t track_number) { // Assert 0 ~ 9999 send_kt_command(0x12, highByte(track_number), lowByte(track_number), 10); } /** Fonction de bas niveau pour envoyer une commande au module KT403A */ void send_kt_command(byte command, byte data_h, byte data_l, unsigned long cmd_delay) { Serial.write(COMMAND_BYTE_START); Serial.write(COMMAND_BYTE_VERSION); Serial.write((byte) 0x06); Serial.write(command); Serial.write((byte) 0x00); Serial.write(data_h); Serial.write(data_l); Serial.write(COMMAND_BYTE_STOP); // 16-bits checksum is optionnal delay(cmd_delay); } /** Fonction setup() */ void setup() { /* Place les boutons en entrées avec pull-up */ for (byte i = 0; i < BUTTONS_COUNT; ++i) { pinMode(BUTTONS_PIN[i], INPUT_PULLUP); } /* Place la LED de statut en sortie */ pinMode(STATUS_LED_PIN, OUTPUT); digitalWrite(STATUS_LED_PIN, HIGH); /* Initialise le port série */ Serial.begin(9600); /* Initialise le module KT403A */ resetPlayer(); selectSourceDevice(DEVICE_SDCARD); setVolume(15); } /** Fonction loop() */ void loop() { /* Test chaque bouton */ for (byte i = 0; i < BUTTONS_COUNT; ++i) { /* Bouton actif ? */ if (digitalRead(BUTTONS_PIN[i]) == LOW) { digitalWrite(STATUS_LED_PIN, LOW); /* Joue la musique */ selectSourceTrackInMp3Directory(i + 1); delay(40); /* Attend que le bouton soit relâché */ while(digitalRead(BUTTONS_PIN[i]) == LOW); delay(40); digitalWrite(STATUS_LED_PIN, HIGH); }