Музыкальный плеер на Arduino


В этой статье мы рассмотрим изготовление музыкального плеера на базе микроконтроллера Ардуино. Прежде всего, такие проекты полезны для обучения навыков работы с микроконтроллером и программирования.

Инструменты и материалы:
-Перемычки;
-Модуль чтения SD-карт;
-ЖК-дисплей 16×2 с платой I2C;
-Ardunio Uno R3;
-Тактовая кнопка — 3 шт;
-Макетная плата;
-Динамик;
-Пластиковый кейс;








Шаг первый: схема
Схема подключения не сложная. Монтаж мастер производит на макетной плате.


Шаг второй: конвертация музыкальные файлы
Теперь нужно преобразовать аудиофайлы в файлы с расширением .wav, чтобы Arduino мог их распознать. Для преобразования мастер использует аудиоконвертер по этому адресу: https://audio.online-convert.com/convert-to-wav. Кликаем «Выбрать файлы» и выбираем аудиофайлы, которые необходимо преобразовать. Затем в дополнительных настройках нужно изменить битовое разрешение на 8-битное, частоту дискретизации на 16000 Гц, аудиоканалы на моно. Дальше нужно кликнуть «Показать дополнительные параметры» и измените формат PCM на 8-битный без знака PCM. Теперь нужно установить SD-карту в компьютер или ноутбук, нажать кнопку «Загрузить» и выбрать SD-карту для загрузки. После загрузки музыкального файла мы нужно переименовать его, например, в music1 или music5, в зависимости от того, сколько файлов на носителе.





Шаг третий: код
Теперь нужно загрузить код на Ардуино. В данном коде установлен лимит на 12 песен. При необходимости количество легко можно изменить внеся изменения в код.
 Показать / Скрыть текст//Code modified by Random Projects// #include <SD.h> // need to include the SD library #define SD_ChipSelectPin 4 //connect pin 4 of arduino to cs pin of sd card #include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files #include <SPI.h> // need to include the SPI library #include <LiquidCrystal_I2C.h> // Driver Library for the LCD Module LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2); // Adjust to (0x27,20,4) for 20×4 LCD TMRpcm tmrpcm; // create an object for use in this sketch int temp=1; int pp=5; int next=6; int prev=7; void setup() { pinMode(pp,INPUT_PULLUP); pinMode(next,INPUT_PULLUP); pinMode(prev,INPUT_PULLUP); lcd.init(); // Initiate the LCD module lcd.backlight();// Turn on the backlight tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc Serial.begin(9600); if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present { Serial.println("SD fail"); return; } lcd.clear(); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" Welcome Aravind! "); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Music Player 1"); delay(2000); tmrpcm.setVolume(5); // tmrpcm.play("music1.wav"); //the sound file "song" will play each time the arduino powers up, or is reset lcd.clear(); //try to provide the file name with extension lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 1"); } void loop() { while(digitalRead(pp)==0 || digitalRead(next)==0 || digitalRead(prev)==0) { if(digitalRead(pp)==0) { tmrpcm.pause(); while(digitalRead(pp)==0); delay(200); } else if(digitalRead(next)==0) { if(temp<4)//temp should be lesser than no. of songs temp=temp+1; while(digitalRead(next)==0); delay(200); song(); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music"); } else if(digitalRead(prev)==0) { if(temp>1) temp=temp-1; while(digitalRead(prev)==0); delay(200); song(); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music"); } } } void song (void) { if(temp==1) { tmrpcm.play("music1.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 1"); } else if(temp==2) { tmrpcm.play("music2.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 2"); } else if(temp==3) { tmrpcm.play("music3.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 3"); } else if(temp==4) { tmrpcm.play("music4.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 4"); } else if(temp==5) { tmrpcm.play("music5.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 5"); } else if(temp==6) { tmrpcm.play("music6.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 6"); } else if(temp==7) { tmrpcm.play("music7.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 7"); } else if(temp==8) { tmrpcm.play("music8.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 8"); } else if(temp==9) { tmrpcm.play("music9.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 9"); } else if(temp==10) { tmrpcm.play("music10.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 10"); } else if(temp==11) { tmrpcm.play("music11.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 11"); } else if(temp==12) { tmrpcm.play("music12.wav"); lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print(" P/P Next Prev"); // Print the string "Hello World!" lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0) lcd.print("Now : Music 12"); } }

Шаг четвертый: корпус
После сборки устройства и загрузки кода все готово. Подключаем питание и можно проигрывать треки. Не желательно установить все в корпус. Для корпуса мастер использует обычный прозрачный пластиковый кейс. В нем необходимо вырезать отверстия для USB-порта Arduino, модуля чтения SD-карт, провода питания.





Все готово.

Источник (Source)

Источник: usamodelkina.ru

Добавить комментарий