Hi All,
I am very close to finishing my first proper project - the code is probably very very cumbersome, but it does what I want it too (ish) and I'm taking that as a win.
I would like to add one more thing and just can't seem to get my head around something that is probably simple. or a better way of doing it.
So at the moment I have two buttons (button one and button two) and when you push either of them it plays a song.
Press the same button again (if song is still playing) and it stops the song and returns the main loop.
Currently have it set up so if the button press is even it plays the song, if the number of presses is odd it knows to stop the song.
What I would like to do if possible, is have one of the buttons play one of two songs.
I am thinking something along the lines of;
if the number of presses is even and in a range of 2, 6, 10, 14, 18, 22, 26 etc play one song
if the number of presses is the other even numbers 4, 8, 12, 16, 20, 24 etc play the other song.
And if it's odd, then as currently, stop playing and return to main loop.
That is a very wordy request but hopefully makes sense?
Any help/advice much appreciated.
#include <Wire.h>
#include <SparkFun_Qwiic_MP3_Trigger_Arduino_Library.h>
MP3TRIGGER mp3;
const byte mp3InterruptPin = 12;
const int buttonOnePin = 8;
const int buttonTwoPin = 7;
//Is song playing 0=No 1=Yes
int songState = 0;
int songNameInt = 0;
int buttonOnePushCounter = 1; // counter for the number of button presses
int buttonOneState = 1; // current state of the button
int lastButtonOneState = 0; // previous state of the button
int buttonTwoPushCounter = 1; // counter for the number of button presses
int buttonTwoState = 1; // current state of the button
int lastButtonTwoState = 0; // previous state of the button
int song5Played = 0;
int song4Played = 0;
//LED LIGHTS
int led01 = 3;
int led02 = 5;
int led03 = 10;
int hold = 50;
void setup() {
// put your setup code here, to run once:
pinMode(buttonOnePin, INPUT_PULLUP);
pinMode(buttonTwoPin, INPUT_PULLUP);
pinMode(mp3InterruptPin, INPUT_PULLUP);
pinMode (led01, OUTPUT);
pinMode (led02, OUTPUT);
pinMode (led03, OUTPUT);
Serial.begin(9600);
Wire.begin();
delay (150);
mp3.begin();
delay (200);
mp3.setVolume(15); //Volume can be 0 (off) to 31 (max)
mp3.playFile(1);
delay (1200);
mp3.playFile(2);
delay (2000);
mp3.playFile(3);
}
void loop() {
// read the pushbutton input pin:
buttonOneState = digitalRead(buttonOnePin);
buttonTwoState = digitalRead(buttonTwoPin);
songState = digitalRead(mp3InterruptPin);
analogWrite (led01, 5);
analogWrite (led02, 0);
analogWrite (led03, 0);
delay (hold);
analogWrite (led01, 1);
analogWrite (led02, 5);
analogWrite (led03, 0);
delay (hold);
analogWrite (led01, 0);
analogWrite (led02, 1);
analogWrite (led03, 5);
delay (hold);
analogWrite (led01, 0);
analogWrite (led02, 0);
analogWrite (led03, 1);
delay (hold);
//read current song playing
String songName = mp3.getSongName();
if (songName == "F003 "){
songNameInt = 3;
}
else if (songName == "F004 "){
songNameInt = 4;
}
else if (songName == "F005 "){
songNameInt = 5;
}
// BUTTON ONE SETUP
// compare the buttonState to its previous state
if (buttonOneState != lastButtonOneState) {
// if the state has changed, increment the counter
if (buttonOneState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonOnePushCounter++;
Serial.println("button one on");
Serial.print("number of button one pushes: ");
Serial.println(buttonOnePushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("button one off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonOneState = buttonOneState;
// BUTTON TWO SETUP
// compare the buttonState to its previous state
if (buttonTwoState != lastButtonTwoState) {
// if the state has changed, increment the counter
if (buttonTwoState == LOW) {
buttonTwoPushCounter++;
} else {
Serial.println("button two off");
}
delay(50);
}
lastButtonTwoState = buttonTwoState;
//BUTTON ONE PUSHED = IF SONG 4 IS PLAYING IT WILL INSTEAD NOW PLAY SONG 5
if ( ((buttonOnePushCounter % 2) == 0) && (songNameInt == 4) && (song4Played == 1)) {
mp3.clearInterrupts();
mp3.playFile(5);
song4Played = 0;
songNameInt = 5;
song5Played = 1;
buttonTwoPushCounter++;
}
//BUTTON ONE PUSHED = IT WILL PLAY SONG 5 IF SONG 5 IS NOT ALREADY PLAYING
else if ( ((buttonOnePushCounter % 2) == 0) && (songNameInt != 5) && (song5Played == 0)) {
mp3.clearInterrupts();
mp3.playFile(5);
song5Played = 1;
songNameInt = 5;
}
//BUTTON ONE PUSHED = IT WILL STOP PLAYING SONG 5 IF IT IS PLAYING
else if ( (buttonOnePushCounter & 0x01) && (songNameInt == 5) && (song5Played == 1)) {
mp3.stop();
delay (180);
song5Played = 0;
}
//BUTTON TWO PUSHED = IF SONG 5 IS PLAYING IT WILL INSTEAD NOW PLAY SONG 4
else if ( ((buttonTwoPushCounter % 2) == 0) && (songNameInt == 5) && (song5Played == 1)) {
mp3.clearInterrupts();
mp3.playFile(4);
song5Played = 0;
songNameInt = 4;
song4Played = 1;
buttonOnePushCounter++;
}
//BUTTON TWO PUSHED = IT WILL PLAY SONG 4 IF SONG 4 IS NOT ALREADY PLAYING
else if ( ((buttonTwoPushCounter % 2) == 0) && (songNameInt != 4) && (song4Played == 0)) {
mp3.clearInterrupts();
mp3.playFile(4);
song4Played = 1;
songNameInt = 4;
}
//BUTTON TWO PUSHED = IT WILL STOP PLAYING SONG 4 IF IT IS PLAYING
else if ( (buttonTwoPushCounter & 0x01) && (songNameInt == 4) && (song4Played == 1)) {
mp3.stop();
delay (180);
song4Played = 0;
}
if ((songState == 0) && (song5Played == 1))
{
mp3.clearInterrupts(); //Clear any interrupt bits before starting a new song
mp3.playFile(3);
song5Played = 0;
buttonOnePushCounter++;
}
else if ((songState == 0) && (song4Played == 1))
{
mp3.clearInterrupts(); //Clear any interrupt bits before starting a new song
mp3.playFile(3);
song4Played = 0;
buttonTwoPushCounter++;
}
// play main loop if nothing else is playing or loop has finished
if (songState == 0)
{
mp3.clearInterrupts(); //Clear any interrupt bits before starting a new song
mp3.playFile(3);
}
}