]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_4_mario_tune/piezo_4_mario_tune.ino
Clean up multitasking, bottoni con pooling e interrupts
[sketchbook_andrea] / piezo / piezo_4_mario_tune / piezo_4_mario_tune.ino
1 /*
2   Arduino Mario Bros Tunes
3   With Piezo Buzzer and PWM
4   by: Dipto Pratyaksa
5   last updated: 31/3/13
6 */
7 #include "pitches.h"
8 //#include "/home/utente/sketchbook-andrea/piezo/piezo_mario_tune/pitches.h";
9 #define melodyPin 9
10 //Mario main theme melody
11 int melody[] = {
12   NOTE_E7, NOTE_E7, 0, NOTE_E7, 
13   0, NOTE_C7, NOTE_E7, 0,
14   NOTE_G7, 0, 0,  0,
15   NOTE_G6, 0, 0, 0, 
16
17   NOTE_C7, 0, 0, NOTE_G6, 
18   0, 0, NOTE_E6, 0, 
19   0, NOTE_A6, 0, NOTE_B6, 
20   0, NOTE_AS6, NOTE_A6, 0, 
21
22   NOTE_G6, NOTE_E7, NOTE_G7, 
23   NOTE_A7, 0, NOTE_F7, NOTE_G7, 
24   0, NOTE_E7, 0,NOTE_C7, 
25   NOTE_D7, NOTE_B6, 0, 0,
26
27   NOTE_C7, 0, 0, NOTE_G6, 
28   0, 0, NOTE_E6, 0, 
29   0, NOTE_A6, 0, NOTE_B6, 
30   0, NOTE_AS6, NOTE_A6, 0, 
31
32   NOTE_G6, NOTE_E7, NOTE_G7, 
33   NOTE_A7, 0, NOTE_F7, NOTE_G7, 
34   0, NOTE_E7, 0,NOTE_C7, 
35   NOTE_D7, NOTE_B6, 0, 0
36 };
37 //Mario main them tempo
38 int tempo[] = {
39   12, 12, 12, 12, 
40   12, 12, 12, 12,
41   12, 12, 12, 12,
42   12, 12, 12, 12, 
43
44   12, 12, 12, 12,
45   12, 12, 12, 12, 
46   12, 12, 12, 12, 
47   12, 12, 12, 12, 
48
49   9, 9, 9,
50   12, 12, 12, 12,
51   12, 12, 12, 12,
52   12, 12, 12, 12,
53
54   12, 12, 12, 12,
55   12, 12, 12, 12,
56   12, 12, 12, 12,
57   12, 12, 12, 12,
58
59   9, 9, 9,
60   12, 12, 12, 12,
61   12, 12, 12, 12,
62   12, 12, 12, 12,
63 };
64
65 //
66
67 //Underworld melody
68 int underworld_melody[] = {
69   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 
70   NOTE_AS3, NOTE_AS4, 0,
71   0,
72   NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4, 
73   NOTE_AS3, NOTE_AS4, 0,
74   0,
75   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
76   NOTE_DS3, NOTE_DS4, 0,
77   0,
78   NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
79   NOTE_DS3, NOTE_DS4, 0,
80   0, NOTE_DS4, NOTE_CS4, NOTE_D4,
81   NOTE_CS4, NOTE_DS4, 
82   NOTE_DS4, NOTE_GS3,
83   NOTE_G3, NOTE_CS4,
84   NOTE_C4, NOTE_FS4,NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
85   NOTE_GS4, NOTE_DS4, NOTE_B3,
86   NOTE_AS3, NOTE_A3, NOTE_GS3,
87   0, 0, 0
88 };
89 //Underwolrd tempo
90 int underworld_tempo[] = {
91   12, 12, 12, 12, 
92   12, 12, 6,
93   3,
94   12, 12, 12, 12, 
95   12, 12, 6,
96   3,
97   12, 12, 12, 12, 
98   12, 12, 6,
99   3,
100   12, 12, 12, 12, 
101   12, 12, 6,
102   6, 18, 18, 18,
103   6, 6,
104   6, 6,
105   6, 6,
106   18, 18, 18,18, 18, 18,
107   10, 10, 10,
108   10, 10, 10,
109   3, 3, 3
110 };
111
112 void setup(void)
113 {
114    pinMode(melodyPin, OUTPUT);//buzzer
115    pinMode(13, OUTPUT);//led indicator when singing a note
116
117 }
118 void loop()
119 {
120 //sing the tunes
121   sing(1);
122   sing(1);
123   sing(2);
124 }
125 int song = 0;
126
127 void sing(int s){      
128    // iterate over the notes of the melody:
129    song = s;
130    if(song==2){
131      Serial.println(" 'Underworld Theme'");
132      int size = sizeof(underworld_melody) / sizeof(int);
133      for (int thisNote = 0; thisNote < size; thisNote++) {
134
135        // to calculate the note duration, take one second
136        // divided by the note type.
137        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
138        int noteDuration = 1000/underworld_tempo[thisNote];
139
140        buzz(melodyPin, underworld_melody[thisNote],noteDuration);
141
142        // to distinguish the notes, set a minimum time between them.
143        // the note's duration + 30% seems to work well:
144        int pauseBetweenNotes = noteDuration * 1.30;
145        delay(pauseBetweenNotes);
146
147        // stop the tone playing:
148        buzz(melodyPin, 0,noteDuration);
149
150     }
151
152    }else{
153
154      Serial.println(" 'Mario Theme'");
155      int size = sizeof(melody) / sizeof(int);
156      for (int thisNote = 0; thisNote < size; thisNote++) {
157
158        // to calculate the note duration, take one second
159        // divided by the note type.
160        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
161        int noteDuration = 1000/tempo[thisNote];
162
163        buzz(melodyPin, melody[thisNote],noteDuration);
164
165        // to distinguish the notes, set a minimum time between them.
166        // the note's duration + 30% seems to work well:
167        int pauseBetweenNotes = noteDuration * 1.30;
168        delay(pauseBetweenNotes);
169
170        // stop the tone playing:
171        buzz(melodyPin, 0,noteDuration);
172
173     }
174   }
175 }
176
177 void buzz(int targetPin, long frequency, long length) {
178   digitalWrite(13,HIGH);
179   long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
180   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
181   //// there are two phases to each cycle
182   long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
183   //// multiply frequency, which is really cycles per second, by the number of seconds to 
184   //// get the total number of cycles to produce
185   for (long i=0; i < numCycles; i++){ // for the calculated length of time...
186     digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
187     delayMicroseconds(delayValue); // wait for the calculated delay value
188     digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
189     delayMicroseconds(delayValue); // wait again or the calculated delay value
190   }
191   digitalWrite(13,LOW);
192
193 }