Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SdFat.h>
- #include <SdFatUtil.h>
- #include <SFEMP3Shield.h>
- SdFat sd;
- int limswitch = 2; //where you plug the switch into
- SFEMP3Shield MP3player;
- void setup(){
- Serial.begin(9600);
- MP3player.ADMixerVol(0); //0 is max, negative numbers are lower.
- sd.begin(SD_SEL, SPI_HALF_SPEED);
- MP3player.begin();
- pinMode(limswitch, INPUT_PULLUP); //pullup because someone on reddit told me to.
- }
- void loop() {
- int switch1 = digitalRead(limswitch); //variable for the switch
- int numTracks = 10; //total number of tracks. Change to be however many you have.
- int randomTrack = random(1, numTracks); //Used to randomly play a track
- //This is to test the functionality of the button independently from everything else. Uncomment this and comment out the if statement to make sure it's reading the button properly.
- /* if(switch1 == HIGH){
- Serial.print("Not Pressed\n");
- delay(100);
- }
- if(switch1 == LOW){
- Serial.print("Pressed");
- delay(100);
- }
- */
- if (switch1 == LOW) //Button is pressed
- {
- MP3player.playTrack(randomTrack); //Play a random track
- Serial.print("Playing track: "); //For testing. Basically makes sure that this is being called
- Serial.print(randomTrack); //Make sure it's randomly choosing a track every time
- Serial.print("\n"); //Formatting
- delay(1000); //Without this delay, when the button is pressed rapidly it queues up all the sounds and after a short bit it just constantly plays random tracks.
- //Adjust the delay to be roughly how long your tracks are.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement