/******DELOREAN SOUND******/
#include <SoftwareSerial.h>
#define ARDUINO_RX 2//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[6] = {0} ;
/************Command byte**************************/
/*basic commands*/
#define CMD_PLAY 0X01
#define CMD_PAUSE 0X02
#define CMD_NEXT_SONG 0X03
#define CMD_PREV_SONG 0X04
#define CMD_VOLUME_UP 0X05
#define CMD_VOLUME_DOWN 0X06
#define CMD_FORWARD 0X0A // >>
#define CMD_REWIND 0X0B // <<
#define CMD_STOP 0X0E
#define CMD_STOP_INJECT 0X0F//stop interruptting with a song, just stop the interlude
/*5 bytes commands*/
#define CMD_SEL_DEV 0X35
#define DEV_TF 0X01
#define CMD_IC_MODE 0X35
#define CMD_SLEEP 0X03
#define CMD_WAKE_UP 0X02
#define CMD_RESET 0X05
/*6 bytes commands*/
#define CMD_PLAY_W_INDEX 0X41
#define CMD_PLAY_FILE_NAME 0X42
#define CMD_INJECT_W_INDEX 0X43
/*Special commands*/
#define CMD_SET_VOLUME 0X31
#define CMD_PLAY_W_VOL 0X31
#define CMD_SET_PLAY_MODE 0X33
#define ALL_CYCLE 0X00
#define SINGLE_CYCLE 0X01
#define CMD_PLAY_COMBINE 0X45//can play combination up to 15 songs
/********DELOREAN LED********/
#define LED 3 // led3 en patte 6
float cli = 0;
float nivcli = 0;
int t = 0;
int a = 0;
float niv = 255;
int rnd = 0;
int start = 0;
float timeoff = 0;
int phare = 0;
int casecli = 0;
float nivphare = 0;
int nivpharetarget = 100;
float flx = 0.01;
float vtf =0.01;
float vtftarget =0.01 ;
float flxlumv = 20;
int flxlum = 0 ;
int chplu = 1;
int chss = 1;
int cht = 1;
int boom = 0;
int boomi = 0;
/******DELOREAN SOUND******/
void sendCommand(int8_t command, int16_t dat );
void setup()
{
myMP3.begin(9600);
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(200);//wait for 200ms
setVolume(10); // Volume max 15
delay(200);
cyclePlay(1);
/********DELOREAN LED********/
pinMode(4, OUTPUT); // led est en sortie
pinMode(5, OUTPUT); // led est en sortie
pinMode(6, OUTPUT); // led est en sortie
pinMode(7, OUTPUT); // led est en sortie
pinMode(8, OUTPUT); // led est en sortie
pinMode(9, OUTPUT); // led est en sortie
pinMode(10, OUTPUT); // led est en sortie
pinMode(11, OUTPUT); // led est en sortie
pinMode(12, OUTPUT); // led est en sortie
pinMode(13, OUTPUT); // led est en sortie
}
/******DELOREAN SOUND******/
void loop()
{
}
void setVolume(int8_t vol)
{
mp3_5bytes(CMD_SET_VOLUME, vol);
}
void playWithVolume(int16_t dat)
{
mp3_6bytes(CMD_PLAY_W_VOL, dat);
}
/*cycle play with an index*/
void cyclePlay(int16_t index)
{
mp3_6bytes(CMD_SET_PLAY_MODE,index);
}
void setCyleMode(int8_t AllSingle)
{
mp3_5bytes(CMD_SET_PLAY_MODE,AllSingle);
}
void playCombine(int8_t song[][2], int8_t number)
{
if(number > 15) return;//number of songs combined can not be more than 15
uint8_t nbytes;//the number of bytes of the command with starting byte and ending byte
nbytes = 2*number + 4;
int8_t Send_buf[nbytes];
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = nbytes - 2; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = CMD_PLAY_COMBINE;
for(uint8_t i=0; i < number; i++)//
{
Send_buf[i*2+3] = song[i][0];
Send_buf[i*2+4] = song[i][1];
}
Send_buf[nbytes - 1] = 0xef;
sendBytes(nbytes);
}
void sendCommand(int8_t command, int16_t dat = 0)
{
delay(20);
if((command == CMD_PLAY_W_VOL)||(command == CMD_SET_PLAY_MODE)||(command == CMD_PLAY_COMBINE))
return;
else if(command < 0x10)
{
mp3Basic(command);
}
else if(command < 0x40)
{
mp3_5bytes(command, dat);
}
else if(command < 0x50)
{
mp3_6bytes(command, dat);
}
else return;
}
void mp3Basic(int8_t command)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x02; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command;
Send_buf[3] = 0xef; //
sendBytes(4);
}
void mp3_5bytes(int8_t command, uint8_t dat)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x03; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command;
Send_buf[3] = dat; //
Send_buf[4] = 0xef; //
sendBytes(5);
}
void mp3_6bytes(int8_t command, int16_t dat)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x04; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command;
Send_buf[3] = (int8_t)(dat >> 8);//datah
Send_buf[4] = (int8_t)(dat); //datal
Send_buf[5] = 0xef; //
sendBytes(6);
}
void sendBytes(uint8_t nbytes)
{
for(uint8_t i=0; i < nbytes; i++)//
{
myMP3.write(Send_buf[i]) ;
}
/********DELOREAN LED********/
if (start == 0)
{
// reset
casecli = 0; cli=0;
for (int res = 0; res<13; res++)
{
analogWrite(res,0);
}
start = 1;
delay(50);
// phare demarage
while (phare == 0)
{
for (int ph = 0 ; ph<5; ph++)
{
// rien encore
phare = 1;
}
}
// phare demarre !!
analogWrite(5,100);
delay(2);
for (int phh=100 ; phh>0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(1);
for (int phh=100 ; phh>0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(2);
for (int phh=100 ; phh>=0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(1000);
for (int phh=100 ; phh>0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(1);
for (int phh=100 ; phh>0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(2);
for (int phh=100 ; phh>0; phh--)
{
analogWrite(5,phh);
delay(1);
}
delay(10);
analogWrite(5,200);
delay(100);
// allumage code porte
analogWrite(13,128);
delay(1000);
analogWrite(4,128);
delay(1000);
analogWrite(14,128);
delay(500);
analogWrite(10,90);// plutonium vue et tb
analogWrite(9,00);
analogWrite(11,0);
}
// DEBUT BOUCLE
// Clignotant
if (casecli==0)
{
nivcli = nivcli + 0.8;
analogWrite(6, nivcli);
if (nivcli >250) { casecli = 1; }
}
if (casecli==1)
{
cli=cli+0.1;
if (cli>70) { casecli = 2; cli=0; }
}
if (casecli==2)
{
nivcli = nivcli - 0.6;
analogWrite(6, nivcli);
if (nivcli <1) { casecli = 3; }
}
if (casecli==3)
{
cli=cli+0.1;
if (cli>70) { casecli = 0; cli=0; }
}
// PHARE
if (nivphare<nivpharetarget) { nivphare=nivphare +0.3; }
if (nivphare>nivpharetarget) { nivphare=nivphare -0.1; }
if (random(200)==1) { nivpharetarget = random(20,200); }
analogWrite(5, nivphare);
// Flux Capacitor !!!
flxlumv = 17-vtf*120;
// ==== Allumage Bande Bleu ====
if (vtf>0.11 && boom==0) {analogWrite(11,255); boom=1 ;}
if (vtf<0.07 && boom==1) {analogWrite(11,0); boom=0 ; }
if (vtf>0.11 ) { niv=255 ;}
if (flxlum < flxlumv) // reglage intensité lumiere plus grand est le chiffre plus c'est sombre
{
flxlum ++; analogWrite (8,127); analogWrite (7,127); analogWrite (12,127);
}
else
{
if (vtf<vtftarget) { vtf=vtf +0.00005; }
if (vtf>= vtftarget) { vtf=vtf -0.00005; }
if (random(250)==1) { vtftarget = random(1,30); vtftarget = vtftarget/120; }
flx = flx + vtf;
if (flx >0 && flx<1) { analogWrite (8,128); analogWrite (7,127); analogWrite (12,127);}
if (flx >1 && flx<2) { analogWrite (8,127); analogWrite (7,128); analogWrite (12,127);}
if (flx >2 && flx<3) { analogWrite (8,127); analogWrite (7,127); analogWrite (12,128);}
if (flx >3 && flx<4) { analogWrite (8,127); analogWrite (7,127); analogWrite (12,127);}
if (flx >4) { flx=0;}
flxlum =0;
}
// chambre Plutonium
if (cht>50) { chplu = chplu + chss; cht =0; }
if (chplu>120) { chss = -1; }
if (chplu<10) { chss = 1; }
analogWrite (9,chplu);
cht++;
timeoff = timeoff +0.1 ;
if (timeoff>50000)
{
timeoff=0;
start = 0;
}
}