Skip to content

Instantly share code, notes, and snippets.

@DaniilVysotskiy
Created May 26, 2025 08:34
Show Gist options
  • Save DaniilVysotskiy/ff1ec44c70b9a31ebdaa2d2f0ce237f6 to your computer and use it in GitHub Desktop.
Save DaniilVysotskiy/ff1ec44c70b9a31ebdaa2d2f0ce237f6 to your computer and use it in GitHub Desktop.
ARDUINO: Audio project for museum
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
SoftwareSerial mySerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
const int COLS = 28;
const int ROWS = 5;
int colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
int rowPins[ROWS] = {32, 33, 34, 35, 36};
int trackMap[ROWS][COLS] = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28},
{29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56},
{57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84},
{85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112},
{113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140}
};
// Текущее состояние кнопок (true - нажата)
bool buttonState[ROWS][COLS] = {false};
// Номер текущего играющего трека (0 - ничего не играет)
int currentTrack = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
if (!myDFPlayer.begin(mySerial)) {
Serial.println("DFPlayer не подключен!");
while(true);
}
myDFPlayer.volume(20); // 30 - максимальная громкость
for (int i = 0; i < ROWS; i++) pinMode(rowPins[i], INPUT);
for (int i = 0; i < COLS; i++) {
pinMode(colPins[i], OUTPUT);
digitalWrite(colPins[i], HIGH);
}
}
void loop() {
bool anyButtonPressed = false;
int pressedTrack = 0;
// Сканируем матрицу кнопок
for (int col = 0; col < COLS; col++) {
digitalWrite(colPins[col], LOW);
for (int row = 0; row < ROWS; row++) {
if (digitalRead(rowPins[row]) == LOW) { // Кнопка нажата
anyButtonPressed = true;
pressedTrack = trackMap[row][col];
buttonState[row][col] = true;
} else {
buttonState[row][col] = false;
}
}
digitalWrite(colPins[col], HIGH);
delay(1);
}
// Управление воспроизведением
if (anyButtonPressed) {
if (pressedTrack != currentTrack) {
myDFPlayer.stop();
myDFPlayer.play(pressedTrack);
currentTrack = pressedTrack;
Serial.print("Играет трек: ");
Serial.println(currentTrack);
}
} else {
if (currentTrack != 0) {
myDFPlayer.stop();
currentTrack = 0;
Serial.println("Воспроизведение остановлено");
}
}
delay(100); // Задержка для стабильности
}
@DaniilVysotskiy
Copy link
Author

5413790656268206096
Схема сборки
Начало матрицы на стенде — верхний левый угол
Начало строк — левый нижний угол платы
Начало рядов — слева

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment