//-----------------------------------------------------------------------
//-- Arduino DoorBell V1.1       01/2017
//-- 48 songs
//--  
//-- Modif texte en memoire pgm
//--
//--
//--
//-- Schoumacher P.
//-- Inspired by R.HUTIN MicroSysteme 9/1981 
//-- Arduino Duemilanove or Diecimila
//-- ATmega328
//------------------------------------------------------------------------


#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <EEPROM.h>

#define YES 1 
#define NO  0
#define MIN_SONG 0
#define MAX_SONG 47
#define SPEAKER_PIN_OUT PCINT20 
#define SERIAL_SPEED 115200
#define EXTERN_INTERRPT_PIN  PCINT18 //INT0 PD2
#define EEPROM_ADRESS        0


char CurrentSong=0;
char buffer[80];
volatile int Flg_Play_Song = NO;

#define SERIALDEBUG
#define WITHTITLE 

const char WAKE_UP[] PROGMEM = " ==>Wake-up";   
const char SLEEP_MODE[] PROGMEM =" ==>Sleep Mode \n";
const char EXTERN_INTERRUPT[] PROGMEM ="(->Extern_Interrupt)\n";

#ifdef WITHTITLE
const char  title_0[] PROGMEM ="LA MARSEILLAISE";
const char  title_1[] PROGMEM ="L'INTERNATIONALE";
const char  title_2[] PROGMEM ="GOD SAVE THE QUEEN";
const char  title_3[] PROGMEM ="LA TOULOUSAINE";
const char  title_4[] PROGMEM ="LE CHANT DES PARTISANS";
const char  title_5[] PROGMEM ="AVE MARIA";
const char  title_6[] PROGMEM ="ALLELUIA DE PAQUES";
const char  title_7[] PROGMEM ="MINUIT CHETIENS";
const char  title_8[] PROGMEM ="AH;VOUS DIRAI-JE MAMAN";
const char  title_9[] PROGMEM ="PLAISIR D'AMOUR";
const char  title_10[] PROGMEM ="SE CANTO";
const char  title_11[] PROGMEM ="LE TEMPS DES CERISES";
const char  title_12[] PROGMEM ="ENVOI DE FLEURS";
const char  title_13[] PROGMEM ="LA PAIMPLAISE";
const char  title_14[] PROGMEM ="O MAGALI";
const char  title_15[] PROGMEM ="CHANT DES ADIEUX";
const char  title_16[] PROGMEM ="AU CLAIR DE LA LUNE";
const char  title_17[] PROGMEM ="LA MERE MICHEL";
const char  title_18[] PROGMEM ="IL ETAIT UN PETIT NAVIRE";
const char  title_19[] PROGMEM ="LE ROI DAGOBERT";
const char  title_20[] PROGMEM ="MALBROUGH";
const char  title_21[] PROGMEM ="FRERES JACQUES";
const char  title_22[] PROGMEM ="MON BEAU SAPIN";
const char  title_23[] PROGMEM ="JOYEUX ANNIVERSAIRE";
const char  title_24[] PROGMEM ="PETIT PAPA NOEL";
const char  title_25[] PROGMEM ="LA MER";
const char  title_26[] PROGMEM ="LES FEUILLES MORTES";
const char  title_27[] PROGMEM ="PETITE FLEUR";
const char  title_28[] PROGMEM ="Y A D'LA JOIE";
const char  title_29[] PROGMEM ="TOUT VA TRES BIEN";
const char  title_30[] PROGMEM ="PARLEZ MOI D'AMOUR";
const char  title_31[] PROGMEM ="DE PROFONDIS MORPIONIBUS";
const char  title_32[] PROGMEM ="MARCHE DES SOLDATS DE FAUST";
const char  title_33[] PROGMEM ="MARCHE D'AIDA";
const char  title_34[] PROGMEM ="AIR DU TOREADOR DE CARMEN";
const char  title_35[] PROGMEM ="MARCHE NUPTIALE DU -SONGE D'UNE NUIT D'ETE -";
const char  title_36[] PROGMEM ="HYMNE A LA NUIT D'HIPPOLYTE ET ARICIE";
const char  title_37[] PROGMEM ="MARCHE DES ROIS DE L'ARLESIENNE";
const char  title_38[] PROGMEM ="CHOEUR DES FIANCAILLES DE LOHENGRIN";
const char  title_39[] PROGMEM ="HEURE EXQUISE DE LA VEUVE JOYEUSE";
const char  title_40[] PROGMEM ="MARCHE TURQUE - MOZART -";
const char  title_41[] PROGMEM ="HYMNE A LA JOIE - BEETHOVEN -";
const char  title_42[] PROGMEM ="ETUDE - DITE TRISTESSE - CHOPIN -";
const char  title_43[] PROGMEM ="REVERIE - SHUMANN -";
const char  title_44[] PROGMEM ="LA TRUITE - SCHUBERT -";
const char  title_45[] PROGMEM ="REVE D'AMOUR - LISZT -";
const char  title_46[] PROGMEM ="TAMBOURIN - RAMEAU -";
const char  title_47[] PROGMEM ="BERCEUSE - BRAHMS -";

const char* const title_table[] PROGMEM = {title_0 , title_1 , title_2 , title_3 , title_4 , title_5 , title_6 , title_7 , title_8 , title_9 ,
                                            title_10, title_11, title_12, title_13, title_14, title_15, title_16, title_17, title_18, title_19,
                                            title_20, title_21, title_22, title_23, title_24, title_25, title_26, title_27, title_28, title_29,
                                            title_30, title_31, title_32, title_33, title_34, title_35, title_36, title_37, title_38, title_39,
                                            title_40, title_41, title_42, title_43, title_44, title_45, title_46, title_47
                                           };

#endif

const unsigned char  ArrayMusic[] PROGMEM = {  
   0x20,/* 0  LA MARSEILLAISE */
   0x89,0x07,0x89,0x16,0x89,0x07,0x66,0x27,
   0x66,0x27,0x5b,0x2c,0x5b,0x2c,0x44,0x58,
   0x51,0x18,0x66,0x27,0x3c,0x41,0x44,0xa1,
   0x51,0x0c,0x4c,0x27,0x5b,0x0b,0x66,0x75,
   
   0x20,/* 1 L'INTERNATIONALE */
   0x66,0x15,0x4c,0x57,0x51,0x1b,0x44,0x21,
   0x4c,0x1d,0x66,0x15,0x7a,0x12,0x5b,0x61,
   0x73,0x4d,0x44,0x61,0x4c,0x1d,0x51,0x1b,
   0x5b,0x18,0x66,0x15,0x73,0x13,0x7a,0x6e,
   
   0x20,/* 2 GOD SAVE THE QUEEN */
   0x4c,0x4a,0x4c,0x4a,0x44,0x53,0x51,0x69,
   0x4c,0x25,0x44,0x53,0x3c,0x5e,0x3c,0x5e,
   0x38,0x63,0x3c,0x8d,0x44,0x29,0x4c,0x4a,
   0x44,0x53,0x4c,0x4a,0x51,0x46,0x4c,0x95,
   
   0x1e,/* 3 LA TOULOUSAINE */
   0x4c,0x9c,0x51,0x25,0x5b,0x0b,0x66,0x62,
   0x7a,0x10,0x7a,0x18,0x7a,0x08,0x89,0x1d,
   0x5b,0x21,0x5b,0x0b,0x66,0x27,0x51,0x25,
   0x51,0x0c,0x4c,0x68,0x66,0x4e,
   
   0x1e,/* 4  LE CHANT DES PARTISANS */
   0x66,0x2d,0x4c,0x3c,0x4c,0x2d,0x44,0x11,
   0x3c,0x4c,0x3c,0x39,0x44,0x11,0x4c,0x3c,
   0x4c,0x2d,0x51,0x0e,0x5b,0x32,0x66,0x16,
   0x51,0x1c,0x5b,0x32,0x66,0x5a,
   
   0x1a,/* 5 AVE MARIA */
   0x66,0x35,0x4c,0x8f,0x4c,0x47,0x51,0x87,
   0x51,0x43,0x5b,0x3c,0x5b,0x3c,0x5b,0x3c,
   0x44,0xa0,0x51,0x43,0x4c,0x8f,0x4c,0x47,
   0x51,0x87,
   
   0x1a,/* 6 ALLELUIA DE PAQUES */
   0x4c,0x1d,0x4c,0x3a,0x44,0x20,0x4c,0x1d,
   0x51,0x1b,0x4c,0x1d,0x44,0x41,0x44,0x20,
   0x66,0x41,0x44,0x41,0x40,0x22,0x44,0x61,
   0x4c,0x57,
   
   0x16,/* 7 MINUIT CHETIENS */
   0x7a,0x1b,0x7a,0x29,0x66,0x10,0x66,0x62,
   0x66,0x20,0x5b,0x24,0x5b,0x24,0x73,0x2b,
   0x5b,0x12,0x4c,0x57,0x66,0x62,
   
   0x1c,/* 8 AH,VOUS DIRAI-JE MAMAN */
   0x9a,0x0e,0x9a,0x0e,0x66,0x15,0x66,0x15,
   0x5b,0x18,0x5b,0x18,0x66,0x2b,0x73,0x13,
   0x73,0x13,0x7a,0x12,0x7a,0x12,0x89,0x10,
   0x89,0x10,0x9a,0x1d,
   
   0x1e,/* 9 PLAISIR D'AMOUR */
   0x66,0x23,0x4c,0x8e,0x44,0xa0,0x3c,0xff,
   0x3c,0x3b,0x38,0x7e,0x38,0x3f,0x3c,0x3b,
   0x44,0x35,0x3c,0x3b,0x44,0xff,0x66,0x23,
   0x5b,0x78,0x51,0x86,0x4c,0x2f,
   
   0x18,/* 10 SE CANTO */
   0x89,0x24,0x66,0x31,0x66,0x31,0x51,0x1e,
   0x5b,0x1b,0x66,0x31,0x66,0x31,0x66,0x18,
   0x5b,0x1b,0x51,0x7b,0x51,0x3d,0x5b,0x6e,
   
   0x1e,/* 11  LE TEMPS DES CERISES   */
   0x89,0x10,0x66,0x16,0x66,0x16,0x66,0x16,
   0x66,0x2d,0x66,0x16,0x5b,0x19,0x5b,0x19,
   0x5b,0x19,0x5b,0x32,0x5b,0x19,0x51,0x1c,
   0x51,0x1c,0x51,0x1c,0x51,0x39,
   
   0x1e,/* 12 ENVOI DE FLEURS */
   0x66,0x15,0x66,0x15,0x5b,0x18,0x51,0x1b,
   0x4c,0x3a,0x44,0x20,0x4c,0x1d,0x51,0x1b,
   0x5b,0x18,0x66,0x2b,0x66,0x15,0x73,0x13,
   0x66,0x15,0x5b,0x18,0x7a,0x24,
   
   0x20,/* 13 LA PAIMPLAISE */
   0x44,0x50,0x4c,0x23,0x51,0x43,0x51,0x43,
   0x44,0x28,0x4c,0x23,0x51,0x43,0x51,0x43,
   0x66,0x1a,0x5b,0x1e,0x51,0x43,0x51,0x21,
   0x51,0x21,0x5b,0x1e,0x66,0x1a,0x5b,0x78,
   
   0x1c,/* 14 O MAGALI */
   0x51,0x1b,0x51,0x1b,0x51,0x1b,0x51,0x36,
   0x5b,0x18,0x44,0x41,0x4c,0x1d,0x51,0x1b,
   0x5b,0x18,0x66,0x15,0x5b,0x18,0x5b,0x18,
   0x5b,0x18,0x5b,0x30,
   
   0x1c,/* 15 CHANT DES ADIEUX */
   0x89,0x23,0x66,0x46,0x66,0x17,0x66,0x2e,
   0x51,0x3b,0x5b,0x4f,0x66,0x17,0x5b,0x34,
   0x51,0x3b,0x66,0x46,0x66,0x17,0x51,0x3b,
   0x44,0x46,0x3c,0xec,
   
   0x16,/* 16 AU CLAIR DE LA LUNE */
   0x4c,0x20,0x4c,0x20,0x4c,0x20,0x44,0x24,
   0x3c,0x52,0x44,0x49,0x4c,0x20,0x3c,0x29,
   0x44,0x24,0x44,0x24,0x4c,0x82,
   
   0x16,/* 17 LA MERE MICHEL  */
   0x4c,0x2d,0x4c,0x0f,0x4c,0x1e,0x4c,0x1e,
   0x4c,0x1e,0x66,0x16,0x4c,0x1e,0x44,0x21,
   0x3c,0x4c,0x44,0x43,0x4c,0x5a,
   
   0x1a,/* 18 IL ETAIT UN PETIT NAVIRE */
   0x3c,0x29,0x3c,0x29,0x3c,0x29,0x66,0x31,
   0x3c,0x52,0x38,0x2b,0x3c,0x29,0x3c,0x52,
   0x44,0x24,0x44,0x24,0x44,0x24,0x44,0x24,
   0x66,0x31,
   
   0x1c,/* 19 LE ROI DAGOBERT */
   0x3c,0x1b,0x3c,0x36,0x44,0x18,0x44,0x30,
   0x4c,0x15,0x4c,0x41,0x44,0x49,0x3c,0x1b,
   0x38,0x1d,0x3c,0x1b,0x44,0x18,0x4c,0x15,
   0x44,0x18,0x4c,0x41,
   
   0x20,/* 20 MALBROUGH */
   0x89,0x0b,0x51,0x27,0x51,0x13,0x51,0x27,
   0x5b,0x11,0x4c,0x3e,0x51,0x13,0x4c,0x14,
   0x51,0x13,0x5b,0x23,0x5b,0x11,0x5b,0x11,
   0x66,0x0f,0x5b,0x11,0x51,0x3b,0x66,0x1f,
   
   0x1c,/* 21 FRERES JACQUES */
   0x66,0x33,0x5b,0x39,0x51,0x40,0x66,0x33,
   0x66,0x33,0x5b,0x39,0x51,0x40,0x66,0x33,
   0x51,0x40,0x4c,0x44,0x44,0xa2,0x51,0x40,
   0x4c,0x44,0x44,0xa2,
   
   0x1e,/* 22 MON BEAU SAPIN */
   0x66,0x38,0x4c,0x38,0x4c,0x12,0x4c,0x4a,
   0x44,0x53,0x3c,0x46,0x3c,0x17,0x3c,0x8b,
   0x4c,0x25,0x44,0x29,0x3c,0x2f,0x38,0x63,
   0x51,0x46,0x44,0x53,0x4c,0x4a,
   
   0x18,/* 23 JOYEUX ANNIVERSAIRE */
   0x66,0x14,0x66,0x14,0x5b,0x2d,0x66,0x28,
   0x4c,0x36,0x51,0x66,0x66,0x14,0x66,0x14,
   0x5b,0x2d,0x66,0x28,0x44,0x3c,0x4c,0x6c,
   
   0x1a,/* 24 PETIT PAPA NOEL */
   0x66,0x2f,0x4c,0x3e,0x4c,0x3e,0x4c,0x3e,
   0x44,0x46,0x4c,0xbc,0x4c,0x1f,0x44,0x23,
   0x3c,0x4f,0x3c,0x4f,0x3c,0x4f,0x38,0x53,
   0x3c,0xed,
   
   0x20,/* 25 LA MER */
   0x66,0x23,0x4c,0xca,0x51,0x1f,0x4c,0x21,
   0x44,0x25,0x3c,0x7f,0x4c,0x21,0x44,0x4b,
   0x51,0x1f,0x5b,0x1c,0x66,0x19,0x3c,0xff,
   0x44,0x25,0x3c,0x2a,0x38,0x2c,0x32,0x4b,
   
   0x20,/* 26 LES FEUILLES MORTES */
   0x5b,0x11,0x51,0x1d,0x4c,0x0a,0x38,0xff,
   0x66,0x0f,0x5b,0x19,0x51,0x0a,0x3c,0xf0,
   0x73,0x0d,0x66,0x17,0x5b,0x08,0x44,0xd6,
   0x7a,0x0d,0x6d,0x16,0x61,0x08,0x4c,0x54,
   
   0x1e,/* 27 PETITE FLEUR */
   0x73,0x30,0x73,0x18,0x7a,0x9f,0x61,0x1c,
   0x51,0x22,0x44,0x28,0x38,0x30,0x3c,0x2d,
   0x4c,0xfc,0x7a,0x16,0x5b,0x24,0x51,0x22,
   0x4c,0x24,0x5b,0x1e,0x51,0xcc,
   
   0x1e,/* 28 Y A D'LA JOIE */
   0x51,0x16,0x44,0x1a,0x51,0x70,0x89,0x0d,
   0x7a,0x0f,0x6d,0x10,0x66,0x11,0x5b,0x14,
   0x51,0x16,0x4c,0x17,0x44,0x1a,0x44,0x1a,
   0x51,0x16,0x44,0x1a,0x51,0x70,
   
   0x1e,/* 29 TOUT VA TRES BIEN */
   0x44,0x1a,0x51,0x16,0x5b,0x14,0x66,0x35,
   0x51,0x16,0x5b,0x1e,0x66,0x09,0x6d,0x10,
   0x7a,0x0f,0x89,0x1a,0x89,0x28,0x44,0x1a,
   0x51,0x16,0x5b,0x14,0x66,0x23,
   
   0x1c,/* 30 PARLEZ MOI D'AMOUR */
   0x51,0x52,0x51,0x29,0x5b,0x49,0x5b,0x24,
   0x66,0xa3,0x66,0x20,0x5b,0x24,0x66,0x20,
   0x5b,0x24,0x51,0x29,0x5b,0x24,0x66,0x20,
   0x7a,0x52,0x7a,0x52,
   
   0x1e,/* 31 DE PROFONDIS MORPIONIBUS */
   0x7a,0x3c,0x66,0x23,0x5b,0x28,0x51,0xb3,
   0x3c,0x77,0x44,0x35,0x3c,0x3c,0x51,0xb3,
   0x5b,0x3c,0x51,0x16,0x4c,0x2f,0x5b,0x28,
   0x66,0x35,0x5b,0x14,0x51,0x59,
   
   0x20,/* 32 MARCHE DES SOLDATS DE FAUST */
   0x66,0x51,0x89,0x1e,0x66,0x28,0x5b,0x0f,
   0x51,0x11,0x66,0x0d,0x5b,0x1e,0x5b,0x0f,
   0x44,0x79,0x51,0x66,0x66,0x28,0x7a,0x22,
   0x6d,0x0c,0x66,0x0d,0x7a,0x0b,0x6d,0x19,
   
   0x1c,/* 33 MARCHE D'AIDA */
   0x89,0x20,0x66,0x82,0x5b,0x10,0x89,0x0a,
   0x5b,0x10,0x51,0x36,0x51,0x36,0x51,0x36,
   0x51,0x12,0x4c,0x13,0x66,0x0e,0x51,0x5b,
   0x5b,0x10,0x66,0x2b,
   
   0x1c,/* 34 AIR DU TOREADOR DE CARMEN */
   0x44,0x41,0x3c,0x36,0x44,0x10,0x51,0x36,
   0x51,0x36,0x51,0x29,0x5b,0x0c,0x51,0x29,
   0x4c,0x0e,0x51,0x6d,0x4c,0x3a,0x5b,0x24,
   0x44,0x10,0x51,0x6d,
   
   0x1c,/* 35 MARCHE NUPTIALE DU -SONGE D'UNE NUIT D'ETE -*/
   0x38,0x57,0x3c,0x3d,0x51,0x0f,0x44,0x24,
   0x4c,0x20,0x56,0x1d,0x66,0x18,0x73,0x15,
   0x7a,0x0f,0x73,0x05,0x66,0x18,0x9a,0x0c,
   0x66,0x06,0x5b,0x37,
   
   0x20,/* 36 HYMNE A LA NUIT D'HIPPOLYTE ET ARICIE */
   0x7a,0x42,0x89,0x75,0x66,0x4e,0x7a,0x42,
   0x9a,0x34,0x73,0x8b,0x7a,0x21,0x89,0x1d,
   0x7a,0x42,0x9a,0x68,0x4c,0x68,0x4c,0x34,
   0x51,0x31,0x5b,0x2c,0x66,0x27,0x6d,0x94,
   
   0x1a,/* 37 MARCHE DES ROIS DE L'ARLESIENNE */
   0x66,0x26,0x89,0x1c,0x66,0x3a,0x5b,0x16,
   0x56,0x22,0x5b,0x0a,0x56,0x22,0x66,0x0a,
   0x44,0x58,0x56,0x16,0x4c,0x34,0x44,0x3a,
   0x40,0x3e,
   
   0x1e,/* 38 CHOEUR DES FIANCAILLES DE LOHENGRIN */
   0x89,0x2e,0x66,0x2e,0x66,0x0f,0x66,0x7b,
   0x89,0x2e,0x5b,0x34,0x6d,0x0e,0x66,0x7b,
   0x89,0x2e,0x66,0x1e,0x4c,0x29,0x4c,0x52,
   0x51,0x27,0x5b,0x22,0x66,0x3d,
   
   0x1e,/* 39 HEURE EXQUISE DE LA VEUVE JOYEUSE*/
   0x89,0x33,0x66,0x22,0x5b,0x4c,0x51,0x2a,
   0x89,0x33,0x66,0x22,0x5b,0x4c,0x51,0x2a,
   0x4c,0x88,0x51,0x80,0x5b,0xe5,0x89,0x33,
   0x5b,0x26,0x51,0x55,0x4c,0x2d,
   
   0x1c,/* 40 MARCHE TURQUE - MOZART -*/
   0x89,0x08,0x9a,0x07,0xa4,0x06,0x9a,0x07,
   0x82,0x22,0x73,0x09,0x82,0x09,0x89,0x08,
   0x82,0x09,0x66,0x2b,0x61,0x0b,0x73,0x09,
   0x6d,0x0a,0x66,0x0b,
   
   0x1e,/* 41 HYMNE A LA JOIE - BEETHOVEN -*/
   0x51,0x1b,0x51,0x1b,0x4c,0x1d,0x44,0x20,
   0x44,0x20,0x4c,0x1d,0x51,0x1b,0x5b,0x18,
   0x66,0x15,0x66,0x15,0x5b,0x18,0x51,0x1b,
   0x51,0x29,0x5b,0x0c,0x5b,0x30,
   
   0x1a,/* 42 ETUDE - DITE TRISTESSE - CHOPIN - */
   0x89,0x28,0x66,0x6a,0x6d,0x32,0x66,0x35,
   0x5b,0xb4,0x51,0x43,0x51,0x43,0x5b,0x3c,
   0x51,0xca,0x4c,0x47,0x4c,0x47,0x51,0x43,
   0x3c,0xb3,
   
   0x1e,/* 43 REVERIE - SHUMANN - */
   0x9a,0x2d,0x73,0x5b,0x7a,0x1c,0x73,0x1e,
   0x5b,0x26,0x4c,0x2d,0x38,0x3c,0x38,0x79,
   0x3c,0x39,0x44,0x33,0x4c,0x2d,0x38,0x3c,
   0x66,0x22,0x5b,0x26,0x56,0x51,
   
   0x1e,/* 44 LA TRUITE - SCHUBERT -*/
   0x66,0x16,0x4c,0x1e,0x4c,0x1e,0x3c,0x26,
   0x3c,0x26,0x4c,0x3c,0x66,0x16,0x66,0x16,
   0x66,0x43,0x66,0x16,0x44,0x21,0x4c,0x1e,
   0x51,0x1c,0x5b,0x19,0x66,0x43,
   
   0x20,/* 45 REVE D'AMOUR - LISZT -*/
   0x89,0x15,0x51,0x69,0x51,0x69,0x51,0xb0,
   0x51,0x23,0x51,0x46,0x51,0x23,0x4c,0x4a,
   0x51,0x23,0x51,0x69,0x7a,0x2f,0x7a,0x17,
   0x7a,0x17,0x6d,0x1a,0x66,0x1c,0x51,0x46,
   
   0x1e,/* 46 TAMBOURIN - RAMEAU -*/
   0x89,0x09,0x7a,0x0a,0x73,0x0a,0x7a,0x0a,
   0x89,0x11,0x5b,0x1a,0x5b,0x1a,0x66,0x17,
   0x73,0x14,0x7a,0x13,0x89,0x09,0x7a,0x0a,
   0x73,0x0a,0x66,0x0b,0x5b,0x1a,
   
   0x1a,/* 47 BERCEUSE - BRAHMS -*/
   0x7a,0x1e,0x7a,0x0a,0x66,0x62,0x7a,0x1e,
   0x7a,0x0a,0x66,0x62,0x7a,0x14,0x66,0x18,
   0x4c,0x41,0x51,0x3d,0x5b,0x37,0x5b,0x37,
   0x66,0x31,
   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff 
   };

int frequence[][2]=
{
  {0x32,784},
  {0x35,740},
  {0x38,698},
  {0x3C,659},
  {0x40,622},
  {0x44,587},
  {0x48,554},
  {0x4C,523},
  {0x51,494},
  {0x56,466},
  {0x5B,440},
  {0x61,415},
  {0x66,392},
  {0x6D,370},
  {0x73,349},
  {0x7A,330},
  {0x82,311},
  {0x89,294},
  {0x92,277},
  {0x9A,262},
  {0xA4,247},
  {0xAE,233},
  {0xB8,220},
  {0xC3,208},
  {0xC7,196},
  {0x00,  0},
  {-1  , -1}
};
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
int Get_Frequency(int val)
{
 int i=0;

  for(i=0;frequence[i][0]!= -1;i++)
  {
     if( frequence[i][0] == val ) 
          return(frequence[i][1]);
  }

  sprintf(buffer,"ERROR Get_Frequency=%x ",val);
  PrintSerial(buffer);
  
 return(0); 
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
unsigned char music( int off ) 
{
  return( pgm_read_byte_near( ArrayMusic + off ) );
}


#ifdef WITHTITLE
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Get_Tittle(short off)
{
 char buff[80];
 strcpy_P(buff, (char*)pgm_read_word(&(title_table[off]))); 
 
  sprintf(buffer,"****[ %s ]****",(buff)); 

}
#endif

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
int Walk_Music(char song)
{
 int offset=0;
 int fnd=song;
  
  for(fnd=0;fnd < song;fnd++)
      offset = offset + music(offset) + 1;
      
 return(++offset); 
}
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
int Get_Offset_Of_Song_Start(char song)
{
  int offset=0;
  
   offset = Walk_Music(song);
  
   sprintf(buffer,"Get_Offset_Of_Song_Start=%d ",offset);
   PrintSerial(buffer);
   
 return(offset);
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
int Get_Number_Of_Music_Note(char  song)
{
 int len=0;
  
 int offset = Walk_Music(song);
 len = music( --offset );  

   sprintf(buffer,"Get_Number_Of_Music_Note=%x ",len);
   PrintSerial(buffer);
 
 return(len);
}


/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Play_Song() 
{
   
  if( CurrentSong < MIN_SONG || CurrentSong > MAX_SONG )
      CurrentSong = MIN_SONG;

 sprintf(buffer,"Play_Song=(%d) ",CurrentSong);
 PrintSerial(buffer);
  
#ifdef WITHTITLE
  Get_Tittle(CurrentSong);
  PrintSerial(buffer);
#endif
  
    
  Read_Song(CurrentSong); 
 
  
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Set_Next_Song()
{
  CurrentSong++; 
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Read_Song( char  song )
{
  int offset=0;
  int maxnote=0;
  int  frq; //frequency
  int  lgt; //length frequency
  
  
  if( song < MIN_SONG || song > MAX_SONG )
      song = MIN_SONG;

  offset  = Get_Offset_Of_Song_Start( song );
  maxnote = Get_Number_Of_Music_Note( song );

   //sprintf(buffer,"offset=%x maxnote=%x",offset,maxnote);
   //PrintSerial(buffer);
   
  if( maxnote > 0 ) 
  {
     do{

         sprintf(buffer,"song(%d) => %x,%x ",song,music(offset),music(offset+1));
         PrintSerial(buffer);    
                
         frq = Get_Frequency( music(offset) );
         lgt = music(offset+1);
    
         offset+=2;
    
         tone(SPEAKER_PIN_OUT,frq);
         delay( lgt * 8);   

         maxnote = maxnote -2;
         
     }while( maxnote > 0 );
  }

  noTone(SPEAKER_PIN_OUT);
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Extern_Interrupt()
{
  noInterrupts();
  
 if( Flg_Play_Song == NO ) 
     Flg_Play_Song = YES;
    
  interrupts(); 
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Save_Current_Song()
{

  if( CurrentSong < MIN_SONG || CurrentSong > MAX_SONG )
      CurrentSong = MIN_SONG;
      
  EEPROM.write(EEPROM_ADRESS,CurrentSong);  
}

/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void Load_Current_Song()
{
  CurrentSong = EEPROM.read(EEPROM_ADRESS);  
  
  if( CurrentSong < MIN_SONG || CurrentSong > MAX_SONG )
  {
      CurrentSong = MIN_SONG;
      Save_Current_Song();
  }
}
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void setup() 
{
  // put your setup code here, to run once:
 Serial.begin(SERIAL_SPEED);

 Load_Current_Song(); 

 PrintSerial("DoorBell V1.0: Schoumacher Patrice 2017/01");
 PrintSerial("             : Inspired by R.HUTIN MicroSysteme 9/1981");

 sprintf(buffer,"Taille EEPROM=%d",EEPROM.length());
 PrintSerial(buffer);
 
 sprintf(buffer,"   ->Current_Song=%d ",CurrentSong);
 PrintSerial(buffer); 
 
 sprintf(buffer,"   SPEAKER_PIN_OUT=%d ",SPEAKER_PIN_OUT);
 PrintSerial(buffer);
  sprintf(buffer,"   EXTERN_INTERRPT_PIN=%d ",EXTERN_INTERRPT_PIN);
 PrintSerial(buffer);
 
 pinMode(LED_BUILTIN, OUTPUT);
 Intern_Led(LOW);
 
 pinMode(SPEAKER_PIN_OUT, OUTPUT); // no song HP
 pinMode(EXTERN_INTERRPT_PIN, INPUT_PULLUP); // External interrupt Pin 2 
 
 attachInterrupt(digitalPinToInterrupt(EXTERN_INTERRPT_PIN), Extern_Interrupt, FALLING);
 
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // Set sleep Mode => save battery
 
}

void Intern_Led(int val)
{
  digitalWrite(LED_BUILTIN, val);  
}
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void PrintSerial(const char *text)
{ 
#ifdef SERIALDEBUG  
    Serial.println(text); 
    Serial.flush();   // while (!(UCSR0A & _BV(TXC0))); // wait for empty serial print bufferer  
 #endif     
}

/*
****--------------------------------------------------------------------
For decrease memory impact with string 
****--------------------------------------------------------------------
*/
void PrintSerialF(const char *text)
{ 
#ifdef SERIALDEBUG  
    Serial.print((const __FlashStringHelper *) text); 
    Serial.flush();   
 #endif     
}
/*
****--------------------------------------------------------------------

****--------------------------------------------------------------------
*/
void loop() {
 
    Load_Current_Song();   
    
    if( Flg_Play_Song == YES )
    {  
        Intern_Led(HIGH);
        Flg_Play_Song = NO;
        
        Play_Song();
      
        Set_Next_Song();
      
        Save_Current_Song();
        Intern_Led(LOW);
    }    

    PrintSerialF(SLEEP_MODE);
   
    sleep_mode(); // Sleep mode activ until external interrupt
    sleep_disable();
    
    PrintSerialF(EXTERN_INTERRUPT);        
    
    PrintSerialF(WAKE_UP);
    
}