I'm driving a 7-segment LED from a constant-current driver that matches the audio file being played. Excuse my code..
Code: Select all
/***************************************************
This is an example for the Adafruit VS1053 Codec Breakout
Designed specifically to work with the Adafruit VS1053 Codec Breakout
----> https://www.adafruit.com/products/1381
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// define the pins used
//#define CLK 13 // SPI Clock, shared with SD card
//#define MISO 12 // Input data, from VS1053/SD card
//#define MOSI 11 // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"
// These can be any pins:
//#define RESET 9 // VS1053 reset pin (output)
//#define CS 10 // VS1053 chip select pin (output)
//#define DCS 8 // VS1053 Data/command select pin (output)
//#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
//#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
// above was original
// below is from kerns_JW on Adafruit https://forums.adafruit.com/viewtopic.php?f=25&t=53543&p=270725#p270725
//Define MP3 Shield Setup
//#define CLK 13 // SPI Clock, shared with SD card
//#define MISO 12 // Input data, from VS1053/SD card
//#define MOSI 11 // Output data, to VS1053/SD card
#define RESET 31 // VS1053 reset pin (output)
#define CS 30//7 // VS1053 chip select pin (output)
#define DCS 34//6 // VS1053 Data/command select pin (output)
#define DREQ 3 // VS1053 Data request pin (into Arduino)
#define CARDCS 32//4 // Card chip select pin
const int ledData = 44;//4;
const int ledClk=46;//5;
const int ledOe=5;//6;
const int ledLat=6;//7;
const int aseg=B10000000;
const int bseg=B1000000;
const int cseg=B100000;
const int dseg=B10000;
const int eseg=B1000;
const int fseg=B100;
const int gseg=B10;
char testdigit=0;
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);
void setup() {
pinMode(ledData,OUTPUT);
pinMode(ledClk,OUTPUT);
pinMode(ledOe,OUTPUT);
pinMode(ledLat,OUTPUT);
digitalWrite(ledOe,1);
Serial.begin(9600);
Serial.println("Adafruit VS1053 Simple Test");
musicPlayer.begin(); // initialise the music player
SD.begin(CARDCS); // initialise the SD card
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20,20);
// Timer interrupts are not suggested, better to use DREQ interrupt!
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
// Play one file, don't return until complete
musicPlayer.playFullFile("1.wav");
delay(3000);
musicPlayer.playFullFile("2.wav");
//delay(3000); musicPlayer.playFullFile("firstservice.output.wav");
//delay(3000); musicPlayer.playFullFile("goback.output.wav");
// Play another file in the background, REQUIRES interrupts!
//musicPlayer.startPlayingFile("track002.mp3");
}
void loop() {
byte tempnumber;
byte number;
char filename[6]="1.wav";
testdigit++;
if(testdigit>9){
testdigit=0;
}
digitalWrite(ledOe,1);
number=0;
for(tempnumber=0;tempnumber <testdigit;tempnumber ++)
{
number+=1<<tempnumber;
}
writeByte(maskDigit(testdigit));
delayMicroseconds(1);
digitalWrite(ledLat,1);
delayMicroseconds(1);
digitalWrite(ledLat,0);
delayMicroseconds(1);
digitalWrite(ledOe,0);
// File is playing in the background
if (musicPlayer.stopped()) {
//delay(500);
filename[0]=testdigit + 48;//converts to ascii numeral
musicPlayer.playFullFile(filename); // Serial.println("Done playing music");
//musicPlayer.startPlayingFile(filename);
//while (1);
}
if (Serial.available()) {
char c = Serial.read();
// if we get an 's' on the serial console, stop!
if (c == 's') {
musicPlayer.stopPlaying();
}
// if we get an 'p' on the serial console, pause/unpause!
if (c == 'p') {
if (! musicPlayer.paused()) {
Serial.println("Paused");
musicPlayer.pausePlaying(true);
} else {
Serial.println("Resumed");
musicPlayer.pausePlaying(false);
}
}
}
delay(100);
}
void writeByte(byte mydata)
{
uint8_t bitMask;
for (bitMask = 0x01; bitMask; bitMask <<= 1)
{
digitalWrite(ledData, (bitMask & mydata)?1:0);
delayMicroseconds(1);
digitalWrite(ledClk,1);
delayMicroseconds(1);
digitalWrite(ledClk,0);
}
//Serial.println();
}
byte maskDigit(byte mydigit)
{
byte temp;
switch (mydigit) {
case 1:
temp=bseg | cseg;
break;
case 2:
temp=aseg | bseg | gseg | eseg | dseg;
break;
case 3:
temp=aseg | bseg | cseg | gseg | dseg;
break;
case 4:
temp=fseg | gseg | bseg | cseg;
break;
case 5:
temp=aseg | fseg | gseg | cseg | dseg;
break;
case 6:
temp = aseg | fseg | eseg | dseg | cseg | gseg;
break;
case 7:
temp=aseg | bseg | cseg;
break;
case 8:
temp =aseg | bseg | cseg | dseg | eseg | fseg | gseg;
break;
case 9:
temp =aseg | bseg | cseg | dseg | fseg | gseg;
break;
case 0:
temp =aseg | bseg | cseg | dseg | eseg | fseg ;
break;
default:
temp=0;
}
//Serial.print(" ");
//Serial.print( temp,DEC);
return temp;
}