#include <SoftwareSerial.h>
SoftwareSerial MP3Serial(12, 13);
const int buttonAdmin = A0;
const int ledAdmin = A1;
const int playerButtons[] = {2, 3, 4, 5, 6, 7};
const int playerLeds[] = {8, 9, 10, 11, A2, A3 };
int gameState = 0;
void setup() {
MP3Serial.begin(9600);
delay(500);
sendCommand(0x3F, 0x00);
sendCommand(0x06, 0x00);
sendCommand(0x05, 0x00);
pinMode(buttonAdmin, INPUT_PULLUP);
pinMode(ledAdmin, OUTPUT);
for (int i = 0; i < 6; i++){
pinMode(playerButtons[i], INPUT_PULLUP);
pinMode(playerLeds[i], OUTPUT);
}
for (int i = 0; i < 6; i++){
digitalWrite(playerLeds[i], LOW);
}
}
void restartGame(){
if(gameState != 1){
gameState = 1;
for (int i = 0; i < 6; i++){
digitalWrite(playerLeds[i], LOW);
}
playStartGame();
}else{
gameState = 0;
}
}
void playAudioWin(){
playTrack("win.mp3");
}
void playStartGame(){
playTrack("start.mp3");
}
void sendCommand(uint8_t command, uint8_t data) {
// Формируем пакет данных
uint8_t packet[8] = {0x7E, 0xFF, 0x06, command, 0x00, 0x00, data, 0xEF};
// Отправляем пакет данных
for (int i = 0; i < 8; i++) {
MP3Serial.write(packet[i]);
}
}
void playTrack(const char *filename) {
sendCommand(0x12, 0x00);
delay(10);
sendCommand(0x08, 0x01);
delay(10);
sendCommand(0x0F, 0x00);
delay(10);
for (int i = 0; i < 11; i++) {
sendCommand(0x01, filename[i]);
delay(10);
}
delay(10);
sendCommand(0x00, 0x00);
delay(10);
sendCommand(0x0D, 0x00);
}
void checkUserResponce(){
for (int i = 0; i < 6; i++){
if(digitalRead(playerButtons[i]) == LOW){
digitalWrite(playerLeds[i], HIGH);
gameState = 2;
playAudioWin();
for (int i2 = 0; i2 < 6; i2++){
if(i2 != i){
digitalWrite(playerLeds[i2], LOW);
}
}
}else{
digitalWrite(playerLeds[i], LOW);
}
}
}
void loop() {
if(gameState == 1){
digitalWrite(ledAdmin, HIGH);
checkUserResponce();
} else if(gameState == 0){
digitalWrite(ledAdmin, LOW);
}
if(digitalRead(buttonAdmin) == LOW){
restartGame();
delay(50);
}
}