Skip to content

Instantly share code, notes, and snippets.

@smukkejohan
Last active December 16, 2015 17:09
Show Gist options
  • Save smukkejohan/5468464 to your computer and use it in GitHub Desktop.
Save smukkejohan/5468464 to your computer and use it in GitHub Desktop.
Simple MP3 Player. Just a start and a stop button. - Written for Da Far Fik Bil exhibition.
boolean playing = false;
int playBtn = 10;
int stopBtn = 11;
int playInd = 12;
int stopInd = 13;
int statusPin = 2;
int stopCmdPin = 8;
int playCmdPin = 4;
long playMillis;
void setup() {
Serial.begin(9600);
// Indicators are outputs
pinMode(playInd, OUTPUT);
pinMode(stopInd, OUTPUT);
// Hack to have theese go to 0
pinMode(stopCmdPin, INPUT);
pinMode(playCmdPin, INPUT);
// Buttons are inputs
pinMode(playBtn, INPUT);
pinMode(stopBtn, INPUT);
pinMode(statusPin, INPUT);
Serial.println("setup");
}
void stop() {
playing = false;
Serial.println("stop");
pinMode(stopCmdPin, OUTPUT);
//digitalWrite(stopCmdPin, HIGH);
delay(200);
//digitalWrite(stopCmdPin, LOW);
pinMode(stopCmdPin, INPUT);
}
void play() {
playing = true;
Serial.println("play");
pinMode(playCmdPin, OUTPUT);
//digitalWrite(playCmdPin, HIGH);
playMillis = millis();
delay(200);
//digitalWrite(playCmdPin, LOW);
pinMode(playCmdPin, INPUT);
}
void loop() {
if (digitalRead(playBtn) == LOW && digitalRead(stopBtn) == HIGH) { // Button is pressed
if(!playing) {
play();
}
}
if (digitalRead(stopBtn) == LOW && digitalRead(playBtn) == HIGH) { // Button is pressed
if(playing) {
stop();
}
}
if(playing) {
digitalWrite(playInd, LOW);
digitalWrite(stopInd, HIGH);
}
else {
digitalWrite(playInd, HIGH);
digitalWrite(stopInd, LOW);
}
long playtime = millis() - playMillis;
if(playtime > 2000) {
if(digitalRead(statusPin) == HIGH) {
playing = false;
} else {
playing = true;
}
} else {
// Sometimes play needs an extra 'push'
if(digitalRead(statusPin) == HIGH && playing) {
play();
}
}
}
#BAUD 38400
#STAT 1
#VOLM 0
#TRIG 01, 0, 1
#TRIG 02, 5, 0
#TRIG 03, 7, 0
#TRIG 04, 6, 0
#TRIG 05, 4, 0
#TRIG 06, 3, 1
#TRIG 07, 2, 1
******************** ALL INIT COMMANDS ABOVE THIS LINE *********************
This is a sample init file for the MP3 Trigger v2, firmware version 2.52.
The init file is optional. If not present, the default parameters will be
in effect: 38.4Kbaud, full volume, and all triggers will start their corr-
esponding tracks with restart lockout disabled. If it is present, it must
be named MP3TRIGR.INI and be located in the root directory.
Only the first 512 bytes of the file are examined for commands, and the first
occurrence of the '*' character is treated as the end of file by the parser.
Comments are not allowed in the command section, but there is no restriction
on the length of the comments that follow the first '*'.
All commands must begin with the '#' character and be followed by a space,
then the command parameters separated by commas. White space is ignored. All
parameters are decimal numbers. Leading zeros are acceptable. See the above
examples - which are redundant since they are all default values.
The following commands are supported in firmware version 2.52:
#BAUD N
where N is one of the following: 2400, 9600, 19200, 31250 or 38400
#VOLM N
where N is from 0 to 255
Default is full volume = 0. Higher numbers produce lower volume.
Useful range is 0 to 64, with values above 64 being inaudible.
#RAND N
where N is from 1 to 255
The default behavior of the random trigger function is to play a random
track from all the MP3 files on the flash card. The #RAND function will
exclude the first N tracks (in the directory) from the random trigger
function. So if there are 18 MP3 files on the card and N=4, then the
first 4 MP3 files will be excluded from the random trigger function.
#TRIG N, F, L
where: N is the trigger number (1 - 18)
F is the trigger function type (see below)
L is the restart lockout enable (see below)
The defined trigger function types (F) are as follows:
F = 0: Normal operation
F = 1: Next (same as the forward Nav switch)
F = 2: Random
F = 3: Previous (same as the back Nav switch)
F = 4: Start (restarts the current track)
F = 5: Stop
F = 6: Volume Up
F = 7: Volume Down
The restart lockout feature, if enabled, will prevent that trigger
from working if audio is currently playing. Use this if you want
to prevent restarts before the track has reached the end. This
feature does not apply to function types 5 - 7.
L = 0: Restart lockout disabled (default)
L = 1: Restart lockout enabled
#STAT N
where N = 1 to enable this function
This command, when used with a value of 1, repurposes TRIG18 to
be an active low play status output. TRIG18 will be HIGH when no
audio is playing, and LOW when MP3 data is being sent to the VS-1063.
It mirrors the behavior of the Status LED after inititialization.
Be careful! The HIGH state is a weak pullup to 3.3V, and the low
state is a strong drive to 0V.
You only need to include entries for triggers that are to be non-default.
As an example, I use the following single-line init file to make trigger
18 be a "Next" function, then hard-wire the trigger so that my MP3 Trigger
powers up and loops continuously through all the tracks on the card.
#TRIG 18, 1, 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment