So I programmed my Arduino Mega 2560 to play certain sound effects from a DF Mini Player that I have. I took the base code that I modified from online so some fo the names might seem odd. Currently, when uploaded to the Arudino, it works as it should, "buttonPause" and "play()" only play one of the audio files WHILE I am pressing a button. "buttonNext"/"play1()" and "buttonPrevious"/"play2()" activate a sound when a button is tapped (not held down). To allow the sound effect to play in full only while I'm, holding down "buttonPause," I had to use a delay as well as interrupt to stop the playback as soon as I released the button. However, I know aside from changing pin numbers, there are some other alterations I have to make for the interrupt to adapt it to work with the ATTiny85. Any idea how I can get started on making those changes? I have been confused by the sources I have found online.
Full code for using the DF Mini Player with the Arduino Mega
/// MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
# define ACTIVATED LOW
int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
boolean isPlaying = false;
const byte interruptPin = 3;
volatile byte state = LOW;
int buttonState = 0;
int buttonState1= 0;
int buttonState2= 0;
int ledPin = 9;
void setup () {
pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);
mySerial.begin (9600);
attachInterrupt(digitalPinToInterrupt(interruptPin), pause, CHANGE);
pinMode(ledPin, OUTPUT);
}
void loop () {
buttonState = digitalRead(buttonPause);
buttonState1 = digitalRead(buttonNext);
buttonState2 = digitalRead(buttonPrevious);
if (buttonState == LOW)
{
play();
setColor(255, 0, 222); // red
digitalWrite(ledPin, HIGH);
delay(1500);
}
if (buttonState1 == HIGH)
{
}
if (buttonState == HIGH)
{
}
if (buttonState1 == LOW)
{
play1();
delay(100);
}
if (buttonState2 == HIGH)
{
}
if (buttonState2 == LOW)
{
play2();
}
}
void pause()
{
execute_CMD(0x16,0,0);
digitalWrite(ledPin, LOW);
}
void play()
{
execute_CMD(0x03,0,1);
}
void play1()
{
execute_CMD(0x03,0,2);
}
void play2()
{
execute_CMD(0x03,0,3);
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}
Board Layout
The amount and placements of the resistors seemed to reduce the amount of noise as much as possible.