2014-09-23 2 views
0

Arduino, Mp3 플레이어 및 LED 매트릭스가있는 멋진 자명종을 작성하여 시간을 표시하고 부드러운 깨우기 조명으로 사용할 수 있습니다. 이 프로젝트가 완료되면이 프로젝트에 액세스하고 오픈 소스로 만들 계획입니다. Arduino가 SD 카드에있는 mp3 파일을 무작위로 선택하여 노래가 끝날 때까지 재생 한 다음 정지 버튼을 누를 때까지 다른 곡을 선택하기를 원합니다. 의 루트 폴더에Arduino 랜덤 파일 SD : rewindDirectory

  1. mp3 파일 현재 :

    가 선택을 할 내가있는 방법을 생각 너무 많은 메모리를 고려하지 않습니다 뭔가를하려면 :하지만 임의의 파일 선택에 문제가 SD는 null 객체를 반환 할 때까지 File::openNextFile()을 사용하여 초기화하는 동안 계산됩니다. ->int n_files

  2. 1에서 n_ 파일 사이의 임의의 숫자를 선택하십시오. ->int rand_song
  3. 원하는 파일에 도달 할 때까지 File::openNextFile() 루프를 사용하여 rand_song 파일을 엽니 다 (너무 효율적이 아니지만 몇 초가 걸릴지는 중요하지 않음). ->File chosen_rand
  4. 노래가 재생되도록 chosen_rand.name() ~ musicPlayer.startPlayingFile()의 이름을 지정하십시오. 노래의 끝이

그래서 첫 번째 질문에 도달하면

  • 당신은 이런 식으로 할 말이 생각, 1)로 이동?

    그런 다음 위의 알고리즘을 실행하여 네 번째 요소가 누락되었습니다 (Code1 참조). 그러나 startPlaying()을 추가하면 rand_song 파일을 두 번 호출 할 때 File::rewindDirectory() (코드 2 참조)을 호출해도 위치가 보존됩니다.

    이것은 두 번째 질문으로 이어집니다. File, SDFile::rewindDirectory을 함께 사용하는 올바른 방법은 무엇입니까?

    나는 이미 파일을 닫는 것과 같은 일을 시도했다. (분명히 한 번만 열어야하기 때문이다.) (Code3 참조). 그러나 항상 똑같은, 그것은 첫 번째 파일을 위해 일하고 있지만, 되감기가 작동하지 않기 때문에, 그것은 폴더의 중간에서 시작하여 폴더의 끝을 친다.

    나는 충분히 정확하고 그것이 작동하게하는 데 필수적인 것이 누락되지 않았 으면 좋겠다.

    void setup() 
    { ... intializations done before 
        // Count the number of files 
        File folder = SD.open(music_folder); 
        n_files = countMP3File(folder); 
        folder.close(); 
        Serial.print("Number of MP3 files: "); Serial.println(n_files); 
    } 
    
    int countMP3File(File dir) 
    { 
        int counter = 0; 
        while(true) 
        { 
        File entry = dir.openNextFile(); 
        if (! entry) 
        { 
         dir.rewindDirectory(); 
         break; 
        } 
        Serial.println(entry.name()); 
        if(strstr(entry.name(), extansion) != NULL) 
         counter++; 
    
        entry.close(); 
        } 
        Serial.println("----------------"); 
        return counter; 
    } 
    
    File selectFileN(int number, File dir) 
    { 
        int counter = 0; 
        File return_entry; 
        while(true) 
        { 
        File entry = dir.openNextFile(); 
        if (! entry) 
        { 
         Serial.println("Last file reached"); 
         dir.rewindDirectory(); 
         break; 
        } 
        Serial.println(entry.name()); 
        if(strstr(entry.name(), extansion) != NULL) 
         counter++; 
        if(counter==number) 
        { 
         return_entry = entry; 
         dir.rewindDirectory(); 
         break; 
        } 
        entry.close(); 
        } 
        return return_entry; 
    } 
    

    코드 1 (작동하지 않음)

    void loop() 
    { 
        int i, rand_song; 
        File folder = SD.open(music_folder); 
        File chosen_rand; 
        rand_song = random(0, n_files)+1; 
        Serial.print("Random number: "); Serial.println(rand_song); 
        chosen_rand = selectFileN(rand_song, folder); 
        if(chosen_rand) 
        Serial.print("Random file name: "); Serial.println(chosen_rand.name()); 
        folder.close(); 
        chosen_rand.close(); 
        Serial.print("playingMusic= "); Serial.println(musicPlayer.playingMusic); 
        Serial.println(); 
        Serial.println(); 
        delay(200); 
    } 
    

    CODE2 (작동하지 않음)

    void loop() 
    { 
        int i, rand_song; 
        File folder = SD.open(music_folder); 
        File chosen_rand; 
        rand_song = random(0, n_files)+1; 
        Serial.print("Random number: "); Serial.println(rand_song); 
        chosen_rand = selectFileN(rand_song, folder); 
        if(chosen_rand) 
        Serial.print("Random file name: "); Serial.println(chosen_rand.name()); 
    
        //Added code here 
        if(!musicPlayer.startPlayingFile(chosen_rand.name())) 
        { 
        Serial.println("Could not open mp3 file"); 
        } 
        else Serial.println("Stated playing!"); 
        //End added code 
    
        folder.close(); 
        chosen_rand.close(); 
        Serial.print("playingMusic= "); Serial.println(musicPlayer.playingMusic); 
        Serial.println(); 
        Serial.println(); 
    
        //Added code here 
        delay(2000); 
        musicPlayer.stopPlaying(); 
        //End added code 
    
        delay(200); 
    } 
    

    CODE3

    void loop() 
    { 
        int i, rand_song; 
        File folder = SD.open(music_folder); 
        File chosen_rand; 
        rand_song = random(0, n_files)+1; 
        Serial.print("Random number: "); Serial.println(rand_song); 
        chosen_rand = selectFileN(rand_song, folder); 
        if(chosen_rand) 
        Serial.print("Random file name: "); Serial.println(chosen_rand.name()); 
    
        //Added code to close file before playing it 
        char * namefile = (char*)malloc(15); 
        strcpy(namefile, chosen_rand.name()); 
        folder.close(); 
        chosen_rand.close();  
        //End added code 
    
        if(!musicPlayer.startPlayingFile(chosen_rand.name())) 
        { 
        Serial.println("Could not open mp3 file"); 
        } 
        else Serial.println("Stated playing!"); 
    
        free(namefile); 
        Serial.print("playingMusic= "); Serial.println(musicPlayer.playingMusic); 
        Serial.println(); 
        Serial.println(); 
    
        delay(2000); 
        musicPlayer.stopPlaying(); 
    
        delay(200); 
    } 
    
    012 (작업)
  • 답변

    0

    좋아요, 문제가 발견되었습니다. 한 번에 오직 File의 SD 카드 만 열어야하므로 파일이 musicPlayer에서 열리면 rewindDirectory가 실패합니다. (틀렸을 때 rewindDirectory가 실패합니다.하지만 테스트를 통해 믿을 수 있습니다.)그래서 수정 사항은 간단하고 논리가 조용합니다. musicPlayer가 파일을 닫은 후에 rewindDirectory를 넣으십시오. 그렇게 할 수있는 좋은 장소는 임의 함수 파일을 선택하기 직전입니다.

    void loop() 
    { 
        int i, rand_song; 
        File folder = SD.open(music_folder); 
        File random_file; 
        rand_song = random(0, n_files)+1; 
        Serial.print("Random number: "); Serial.println(rand_song); 
        folder.rewindDirectory(); 
        random_file = selectFileN(rand_song, folder); 
        folder.close(); 
        if(!musicPlayer.startPlayingFile(random_file.name())) 
        { 
        Serial.println("Could not open mp3 file"); 
        } 
        else Serial.println("Stated playing!"); 
        random_file.close(); 
    
        Serial.print("playingMusic= "); Serial.println(musicPlayer.playingMusic); 
        Serial.println(); 
        Serial.println(); 
    
        delay(2000); 
        musicPlayer.stopPlaying(); 
        delay(200); 
    } 
    
    : 그래서 여기

    는 작업 루프의