Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <SdFat.h>
  4. #include <SdFatUtil.h>
  5. #include <SFEMP3Shield.h>
  6.  
  7. SdFat sd;
  8. int limswitch = 2; //where you plug the switch into
  9. SFEMP3Shield MP3player;
  10.  
  11. void setup(){
  12.  
  13.  
  14. Serial.begin(9600);
  15.  
  16.  
  17. MP3player.ADMixerVol(0); //0 is max, negative numbers are lower.
  18. sd.begin(SD_SEL, SPI_HALF_SPEED);
  19. MP3player.begin();
  20.  
  21. pinMode(limswitch, INPUT_PULLUP); //pullup because someone on reddit told me to.
  22. }
  23.  
  24. void loop() {
  25. int switch1 = digitalRead(limswitch); //variable for the switch
  26. int numTracks = 10; //total number of tracks. Change to be however many you have.
  27. int randomTrack = random(1, numTracks); //Used to randomly play a track
  28.  
  29. //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.
  30. /* if(switch1 == HIGH){
  31. Serial.print("Not Pressed\n");
  32. delay(100);
  33. }
  34.  
  35.  
  36.  
  37. if(switch1 == LOW){
  38. Serial.print("Pressed");
  39. delay(100);
  40. }
  41. */
  42. if (switch1 == LOW) //Button is pressed
  43. {
  44. MP3player.playTrack(randomTrack); //Play a random track
  45. Serial.print("Playing track: "); //For testing. Basically makes sure that this is being called
  46. Serial.print(randomTrack); //Make sure it's randomly choosing a track every time
  47. Serial.print("\n"); //Formatting
  48. 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.
  49. //Adjust the delay to be roughly how long your tracks are.
  50. }
  51.  
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement