]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_3_toneMelody/piezo_3_toneMelody.ino
ezo e RGB
[sketchbook_andrea] / piezo / piezo_3_toneMelody / piezo_3_toneMelody.ino
1 /*
2   Melody
3  
4  Plays a melody 
5  
6  circuit:
7  * 8-ohm speaker on digital pin 8
8  
9  created 21 Jan 2010
10  modified 30 Aug 2011
11  by Tom Igoe 
12
13 This example code is in the public domain.
14  
15  http://arduino.cc/en/Tutorial/Tone
16  
17  */
18  #include "pitches.h"
19
20 int piezoPin = 9 ;
21 // notes in the melody:
22 int melody[] = {
23   NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
24
25 // note durations: 4 = quarter note, 8 = eighth note, etc.:
26 int noteDurations[] = {
27   4, 8, 8, 4,4,4,4,4 };
28
29 void setup() {
30   // iterate over the notes of the melody:
31   for (int thisNote = 0; thisNote < 8; thisNote++) {
32
33     // to calculate the note duration, take one second 
34     // divided by the note type.
35     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
36     int noteDuration = 1000/noteDurations[thisNote];
37     tone(piezoPin, melody[thisNote],noteDuration);
38
39     // to distinguish the notes, set a minimum time between them.
40     // the note's duration + 30% seems to work well:
41     int pauseBetweenNotes = noteDuration * 1.30;
42     delay(pauseBetweenNotes);
43     // stop the tone playing:
44     noTone(piezoPin);
45   }
46 }
47
48 void loop() {
49   // no need to repeat the melody.
50   // RESET to play again
51 }
52
53 /* Domande
54 1. Codificare Twinkle twinkle little star
55 */