Facebook
From qwerty, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 213
  1. #include <SD.h>
  2. #include <TMRpcm.h>
  3.  
  4. TMRpcm tmrpcm;
  5.  
  6. void setup() {
  7.   // Initialize Serial Monitor
  8.   Serial.begin(9600);
  9.  
  10.   // Initialize microSD card
  11.   if (!SD.begin(10)) {
  12.     Serial.println("SD card initialization failed!");
  13.     return;
  14.   }
  15.  
  16.   // Initialize audio player
  17.   tmrpcm.speakerPin = 9;  // Set the speaker pin
  18. }
  19.  
  20. void loop() {
  21.   // Check if the file exists
  22.   if (SD.exists("sound.wav")) {
  23.     // Play the audio file
  24.     tmrpcm.play("sound.wav");
  25.  
  26.     // Wait for the playback to finish
  27.     while (tmrpcm.isPlaying()) {
  28.       delay(100);
  29.     }
  30.   } else {
  31.     Serial.println("File not found!");
  32.   }
  33.  
  34.   // You can add more logic or loops for continuous playback as needed
  35. }