Add firware example for playing sound from sd card

This commit is contained in:
Klemens Schölhorn 2018-06-08 19:43:23 +02:00
parent da39c106dc
commit eb487a0944
1 changed files with 65 additions and 0 deletions

65
firmware/firmware.ino Normal file
View File

@ -0,0 +1,65 @@
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 5 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
void setup(){
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
//Complimentary Output or Dual Speakers:
//pinMode(10,OUTPUT); Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
Serial.begin(9600);
//pinMode(13,OUTPUT); //LED Connected to analog pin 0
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
return; // don't do anything more if not
}
else{
Serial.println("SD ok");
}
tmrpcm.volume(2);
tmrpcm.play("coffee.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
}
void loop(){
//blink the LED manually to demonstrate music playback is independant of main loop
if(tmrpcm.isPlaying() && millis() - time > 50 ) {
//digitalWrite(13,!digitalRead(13));
// time = millis();
}else
if(millis() - time > 500){
//digitalWrite(13,!digitalRead(13));
//time = millis();
}
if(Serial.available()){
switch(Serial.read()){
case 'c': tmrpcm.play("coffee.wav"); break;
case 'p': tmrpcm.pause(); break;
case '?': if(tmrpcm.isPlaying()){ Serial.println("A wav file is being played");} else{Serial.println("No wav file is being played");} break;
case 'S': tmrpcm.stopPlayback(); break;
case '0': tmrpcm.volume(0); break;
case '1': tmrpcm.volume(1); break;
case '2': tmrpcm.volume(2); break;
case '3': tmrpcm.volume(3); break;
case '4': tmrpcm.volume(4); break;
case '5': tmrpcm.volume(5); break;
case '6': tmrpcm.volume(6); break;
case '7': tmrpcm.volume(7); break;
case '=': tmrpcm.quality(0); break;
case '-': tmrpcm.quality(1); break;
default: break;
}
}
}