Ultra son qui declenche un lecteur mp3UART

Description

Code

/**
 * Code capteur ultrason <80cm declenche un son mp3
 */
 
/* 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 setEqualizer() */
const byte EQUALIZER_NORMAL = 0x00;
const byte EQUALIZER_POP = 0x01;
const byte EQUALIZER_ROCK = 0x02;
const byte EQUALIZER_JAZZ = 0x03;
const byte EQUALIZER_CLASSIC = 0x04;
const byte EQUALIZER_BASS = 0x05;
 
/* Constantes pour la commande selectSourceDevice() */
const byte DEVICE_UDISK = 0x01;
const byte DEVICE_SDCARD = 0x02;
const byte DEVICE_AUX = 0x03; // Not used
const byte DEVICE_PC = 0x04; // Debug only
const byte DEVICE_FLASH = 0x05;
const byte DEVICE_SLEEP = 0x06;
 
 
/** Play the next song (all folders) WARNING: files are ordered by creation date! */
void playNextSong() {
  send_kt_command(0x01, 0, 0, 10);
}
 
/** Play the previous song (all folders) WARNING: files are ordered by creation date! */
void playPreviousSong() {
  send_kt_command(0x02, 0, 0, 10);
}
 
/** Play the given track number (0 ~ 2999) (all folders) WARNING: files are ordered by creation date! */
void playTrack(uint16_t track_number) {
  send_kt_command(0x03, highByte(track_number), lowByte(track_number), 10);
}
 
/** Increase the volume */
void volumeUp() {
  send_kt_command(0x04, 0, 0, 10);
}
 
/** Decrease the volume */
void volumeDown() {
  send_kt_command(0x05, 0, 0, 10);
}
 
/** 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);
}
 
/** Set the equalizer to the given audio mode */
void setEqualizer(byte mode) {
  send_kt_command(0x07, 0, mode, 20);
}
 
/** Repeat a single track (0 ~ 2999) (all folders) WARNING: files are ordered by creation date! */
void repeatSingleTrack(uint16_t track_number) {
  send_kt_command(0x08, highByte(track_number), lowByte(track_number), 10);
}
 
/** Select the source device for playing */
void selectSourceDevice(byte device) {
  send_kt_command(0x09, 0, device, 200);
}
 
/** Enter standby / low power mode */
void enterStandbyMode() {
  send_kt_command(0x0A, 0, 0, 20);
}
 
/** Exit standby / low power mode */
void exitStandbyMode() {
  send_kt_command(0x0B, 0, 0, 20);
}
 
/** Reset the MP3 module */
void resetPlayer() {
  send_kt_command(0x0C, 0, 0, 100);
}
 
/** Play/resume the music */
void playResumeMusic() {
  send_kt_command(0x0D, 0, 0, 20);
}
 
/** Pause the music */
void pauseMusic() {
  send_kt_command(0x0E, 0, 0, 20);
}
 
/** Select the source folder and track for playing */
void selectSourceFolderAndTrack(byte folder_number, byte track_number) {
  send_kt_command(0x0F, folder_number, track_number, 10);
}
 
// 0x10 "Volume adjust set" - NOT SUPPORTED
 
/** Enable or disable looping of all music files */
void loopAllMusic(byte enabled) {
  send_kt_command(0x11, 0, !!enabled, 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);
}
 
/** Pause the current track and insert an advertisement song from the "ADVERT" (case insensitive) folder */
void insertSongFromAdvertDirectory(uint16_t track_number) {
  // Assert 0 ~ 9999
  send_kt_command(0x13, highByte(track_number), lowByte(track_number), 10);
}
 
/** Play the given song in the given directory (big directory version) */
void selectSourceBigFolderAndTrack(byte folder, uint16_t track_number) {
  // Assert folder 0 ~ 15, track number 0 ~ 9999
  // 0b1100 011111001111 = 4bits folder, 12 bits track number
  send_kt_command(0x14, (folder << 4) | highByte(track_number) & 0x0f, lowByte(track_number), 10);
}
 
/** Stop playing the inserted advertisement song and resume playing the user track */
void stopInsertedSongAndResumePlaying() {
  send_kt_command(0x15, 0, 0, 20);
}
 
/** Stop the music */
void stopMusic() {
  send_kt_command(0x16, 0, 0, 20);
}
 
/** Loop all music in the given folder */
void loopFolder(byte folder_number) {
  send_kt_command(0x17, 0, folder_number, 10);
}
 
/** Random playing */
void shufflePlay() {
  send_kt_command(0x18, 0, 0, 20);
}
 
/** single repeat the currently playing track track */
void repeatCurrentTrack(byte enabled) {
  send_kt_command(0x19, 0, !enabled, 10);
}
 
/** Mute sound output **/
void muteSound(byte enabled) {
  send_kt_command(0x1A, 0, !!enabled, 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);
}
 
 
// Ultrason
/* Constantes pour les broches */
const byte TRIGGER_PIN = 2; // Broche TRIGGER
const byte ECHO_PIN = 3;    // Broche ECHO
 
/* Constantes pour le timeout */
const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s
 
/* Vitesse du son dans l'air en mm/us */
const float SOUND_SPEED = 340.0 / 1000;
 
 
 
 
 
/** Fonction setup() */
void setup() {
 
  /* Initialise le port série */  
  Serial.begin(9600);
  Serial.setTimeout(60000);
 
  /* Initialise le module KT403A */
  resetPlayer();
  selectSourceDevice(DEVICE_SDCARD);
  setVolume(15);
 
    /* Ultrason :  Initialise les broches */
  pinMode(TRIGGER_PIN, OUTPUT);
  digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
  pinMode(ECHO_PIN, INPUT);
}
 
/** Fonction loop() */
void loop() {
 
 
    /* 1. Lance une mesure de distance en envoyant une impulsion HIGH de 10µs sur la broche TRIGGER */
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
 
  /* 2. Mesure le temps entre l'envoi de l'impulsion ultrasonique et son écho (si il existe) */
  long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
 
  /* 3. Calcul la distance à partir du temps mesuré */
  float distance_mm = measure / 2.0 * SOUND_SPEED;
 
  /* Affiche les résultats en mm, cm et m */
  Serial.print(F("Distance: "));
  Serial.print(distance_mm);
  Serial.print(F("mm ("));
  Serial.print(distance_mm / 10.0, 2);
  Serial.print(F("cm, "));
  Serial.print(distance_mm / 1000.0, 2);
  Serial.println(F("m)"));
 
 
 
// le programme en cm
  if (distance_mm /10 < 80){    
 
      playNextSong();
      //break;
      delay(3000);
 
    Serial.println("touche");}
 
  /* Délai d'attente pour éviter d'afficher trop de résultats à la seconde */
  delay(500);
 
}