/* GONG MELODY GENERATOR *Questo dispositivo consente di implementare la funzionalità "GONG" su una *qualsiasi centrale anti intrusione. Detta funzionalità consiste nella *emissione di una melodia ogni qualvolta un ingresso passa da chiuso *(verso GND) ad aperto. Gli ingressi previsti sono due: Door e Window. *Essi si riferiscono rispettivamente al sensore montato sul portoncino di *ingresso ed alla serie di sensori installati nella protezione perimetrale *delle finestre. La riproduzione della melodia associata all'ingresso *"Door" viene inibita per 30 minuti ogni volta che lo stesso passa da *riposo ad intervenuto. Il timer è del tipo retriggerabile. *La routine di gestione dei timer può gestire 4 timer ma ne è utilizzato *soltanto uno. La riproduzione della melodia prende spunto da un lavoro di *David Johnson-Davies - www.technoblogy.com - "Digital Music Box v2" 16/3/2016 */ // ATtiny85 Gong generator for anti intrusion system // Author: Giuseppe Fiordaliso // ATMEL ATTINY 85 // // +- \/-+ // RST non usato PB5 1|* |8 Vcc // Uscita LED(L) PB3 2| |7 PB2 Sensori Finestre // Uscita B.F. PB4 3| |6 PB1 Sensore Porta // GND 4| |5 PB0 Inserzione/Disins. // +-----+ // #include #define abl PB0 // PIN 5 Inserzione / Disinserzione dispositivo #define gate_pin PB1 // PIN 6 Sensore portoncino ingresso (NC) #define window_pin PB2 // PIN 7 Linea sensori Finestre (NC) #define LED PB3 // PIN 2 Uscita LOW durante il suono del Gong // utile per comandare lo stand-by // di un amplificatore tipo LM4819 #define speakerPin PB4 // PIN 3 Uscita bassa frequenza suono melodia // int Scale[] = { 680, 721, 764, 809, 857, 908, 962, 1020, 1080, 1144, 1212, 1284, 1361, 1442, 1528, 1618, 1715, 1817, 1925, 2039, 2160, 2289, 2425, 2569, 2722, 2884, 3055, 3237, 3429, 3633, 3849, 4078 }; const int Channels = 4; const int Tempo = 4; // 4 = 4 beats per second (3 = 0,125 sec, 4 = 0,25 sec, 5 = 0,5 sec, 6 = 1 sec - min = 0, max = 8) const int Decay = 10; // Length of note decay; max 10 volatile unsigned int Acc[Channels]; volatile unsigned int Freq[Channels]; volatile unsigned int Amp[Channels]; // /* // Play Big Ben // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000100000000000100000000000000, // F F 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b10000000000000000001000000000000, // G C 0b00000000000000000000000000000000, // 0b00000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b10000000000010000000000000000000, // C C 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b00000100000000000000010000000000, // A F 0b00000000000000000000000000000000, // 0b00000000000000000100000000000000, // F 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000100000000000000010000000000, // A F 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b10000000000000000100000000000000, // F C 0b00000000000000000000000000000000, // 0b00000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b10000000000010000000000000000000, // C C 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b00000100000000000000010000000000, // A F 0b00000000000000000000000000000000, // 0b00000000000000000100000000000000, // F 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune */ /* // Play Happy Birthday // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000001000000000000, 0b00000000000000000001000000000000, 0b10000000000000000000010000000000, 0b00000000000000000000000000000000, 0b00001001000000000001000000000000, 0b00000000000000000000000000000000, 0b00000000000000000000000010000000, 0b00000000000000000000000000000000, 0b00100000000000000000000100000000, 0b00000000000000000000000000000000, 0b00000101000000000000000000000000, 0b00000000000000000000000000000000, 0b00000000000000000001000000000000, 0b00000000000000000001000000000000, 0b10000000000000000000010000000000, 0b00000000000000000000000000000000, 0b00000101000000000001000000000000, 0b00000000000000000000000000000000, 0b00000000000000000000000000100000, 0b00000000000000000000000000000000, 0b10000000000000000000000010000000, 0b00000000000000000000000000000000, 0b00001001000000000000000000000000, 0b00000000000000000000000000000000, 0b00000000000000000001000000000000, 0b00000000000000000001000000000000, 0b10000000000000000000000000000001, 0b00000000000000000000000000000000, 0b00100000000000000000000000001000, 0b00000000000000000000000000000000, 0b00001000000000000000000010000000, 0b00000000000000000000000000000000, 0b00000100000000000000000100000000, 0b00000000000000000000000000000000, 0b00000000000000000000010000000000, 0b00000000000000000000000000000000, 0b00000000000000000000000000000100, 0b00000000000000000000000000000100, 0b00000001000000000000000000001000, 0b00000000000000000000000000000000, 0b00000000000000000000000010000000, 0b00000000000000000000000000000000, 0b00000101000000000000000000100000, 0b00000000000000000000000000000000, 0b10001001000000000000000010000000, 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune */ const uint32_t tuneDoor[] PROGMEM = { // Play Big Ben // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00001000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b10000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00100000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000100000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00000000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000010000000000000000000, // C 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune /* // Play close encounters melody // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000100000000, // B 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G< 0b00000000000000000000000000000000, // 0b00000000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000001000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C 0b00000000000000000000000000000000, // 0b10000000000000000000000000000000, // C< 0b00000000000000000000000000000000, // 0b00000001000000000000000000000000, // G< 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune // */ /* const uint32_t tuneWindow[] PROGMEM = { // Play per un pugno di dollari // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D> 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000001000000000000, // G 0b00000000000000000000000000000000, // 0b00000000000000100000000000000000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune */ /* * C O N T I N U A Z I O N E //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D 0b00000000000000000000000000001000, // E 0b00000000000000000000000000100000, // D 0b00000000000000000000000000100000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000001000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000000000100, // F 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // A 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000100000, // D 0b00000000000000000000000000001000, // E 0b00000000000000000000000000100000, // D 0b00000000000000000000000000100000, // D 0b00000000000000000000000000000000, // //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000000000010000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000010000000000, // //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0xFF}; // End of tune */ const uint32_t tuneWindow[] PROGMEM = { // Play Morricone clock melody // C -> DO, D -> RE, E -> MI, F -> FA, G ->SOL, A -> LA, B -> SI, //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000010000000000100000000, // C B 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000000001000010000000000, // E A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000010000000000100000000, // C B 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000000001000010000000000, // E A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // // //_*_*__*_*_*__*_*__*_*_*__*_*__*_ //C D EF G A BC D EF G A BC D EF G 0b00000000000010000000000100000000, // C B 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000000001000010000000000, // E A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000010000000000100000000, // C B 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000010000000, // C> 0b00000000000000000000000000000000, // 0b00000000000000001000010000000000, // E A 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000001000000000000000, // E 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // 0b00000000000000000000000000000000, // // 0xFF}; // End of tune // //Globals persist throughout tune int TunePtr = 0, Chan = 0; // #define abl PB0 // PIN 5 ATtiny85 #define door_pin PB1 // PIN 6 ATtiny85 #define window_pin PB2 // PIN 7 ATtiny85 #define LED PB3 // PIN 2 ATtiny85 #define speakerPin PB4 // PIN 3 ATtiny85 // RESET non collegato PIN 1 ATtiny85 // + 5 V PIN 8 ATtiny85 // GND PIN 4 ATtiny85 // // ************* DICHIARAZIONE VARIABILI PER LA GESTIONE DEI TIMER ************ // volatile boolean tickFlag; boolean ledState; const int NTIMER = 5; // Numero di timer che si vogliono gestire unsigned int timerpreset[NTIMER]; boolean startimer[NTIMER]; boolean runtimer[NTIMER]; boolean stoptimer[NTIMER]; boolean endtimer[NTIMER]; boolean abortimer[NTIMER]; unsigned int timercounter[NTIMER]; // // ************* DICHIARAZIONE VARIABILI DEL PROGRAMMA ************ // boolean Inserito; boolean Open_door; boolean Open_window; boolean prev_Open_door; boolean prev_Open_window; boolean playDoor_flag; boolean playWindow_flag; volatile boolean playMelody_flag; // // ************* DICHIARAZIONE VARIABILI DEL PLAYER ************ // const uint32_t *puntatore; // ************** S E T U P ************ void setup() { // Abilita il PLL a 64 MHz e lo usa come sorgente di clock per il Timer1 PLLCSR = 1<PB4 OCR1B = 128; // DDRB = 1< 0. * Quando timercounter[] arriva a zero viene settato endtimer[] * e viene resettato runtimer[]. E' a cura del programma * chiamante resettare poi endtimer[]. */ // void timermanager(){ // tickFlag viene settato dalla routine di servizio del WDT che scandisce anche la successione delle note // della melodia. Nel caso di "Tempo = 4" questo avviene 4 volte al secondo. Per l'esattezza in un minuto 54 volte if (tickFlag){ tickFlag = false; for (int i=0; i0) { // Se timercounter è maggiore di zero timercounter[i]--; // decrementalo di uno quindi verifica se è if (timercounter[i] == 0){ // arrivato a zero. In tal caso setta endtimer[] runtimer[i] = false; // e resetta runtimer[]. endtimer[i] = true; } } } } } } // **************** ROUTINE DI SERVIZIO ALL'INTERRUPT DEL WDT **************** // Watchdog interrupt plays notes ISR(WDT_vect) { sei(); // Abilita interrupt /* L'interruzione del watchdog ripristina automaticamente il timer del watchdog * in modo da provocare un reset al successivo interrupt; per evitare ciò è necessario * impostare il bit di interruzione prima dell'interruzione successiva: */ WDTCR |= 1<> 8; // L'onda quadra viene generata testando il bit superiore Mask = Temp >> 7; // dell'accumulatore per ciascun canale, Acc [c] // Mask è impostato su 0x00 se il bit è zero o 0xFF se è uno: Env = Amp[c] >> Decay; // Infine, la nota è impostata su + o - dell'ampiezza corrente dell'inviluppo, Env Note = (Env ^ Mask) + (Mask & 1); // Lo stesso che scrivere: "if (Mask != 0) Note = Env; else Note = - Env;" Sum = Sum + Note; } OCR1B = Sum + 128; // Compare register di TIMER 1 }