]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_2_keyboard/piezo_2_keyboard.ino
ae3aabdb6de5a226f9c480472478ef9168d1891c
[sketchbook_andrea] / piezo / piezo_2_keyboard / piezo_2_keyboard.ino
1 /*
2  Melody keyboard with Input Pullup Serial
3  Plays a pitch that changes based on 3 digital inputs
4  
5  This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a 
6  digital input on pin 2 and prints the results to the serial monitor.
7  
8  Thecircuit:
9  * 3 buttons in pin 2,3,4 with no resistors
10  * Piezo on digital pin 9
11  * Serial debug is available
12  
13  This example code is in the public domain
14  
15  Circuit: http://lab.piffa.net/schemi/piezo_2_keyboard_bb.png
16  
17  */
18
19
20 int input[]= {
21   1,2,3};
22 int notes[] = {
23   262, 392,880 }; // suona una prima, quinta, ottava in C4
24
25 // Carica un file di esempio con tutte le note
26 // #include "pitches.h";
27 // int notes[] = {NOTE_C4, NOTE_G4,NOTE_A5 }; // suona una prima, quinta, ottava
28
29 void setup(){
30   //start serial connection
31   Serial.begin(9600);
32   //configure pin2/3/4 as an input and enable the internal pull-up resistor
33   pinMode(2, INPUT_PULLUP);
34   pinMode(3, INPUT_PULLUP);
35   pinMode(4, INPUT_PULLUP);
36   pinMode(9, OUTPUT); 
37 }
38
39 void loop(){
40   for (int thisSensor = 2; thisSensor < 5; thisSensor++) {
41     int sensorReading = digitalRead(thisSensor);
42     if (sensorReading == LOW) {
43       Serial.println(thisSensor);
44       tone(9, notes[thisSensor -2], 50); // Notes array is translated
45     }
46     //delay(2); // eventuale delay
47   }
48 }
49 /* Domande
50  1. Modificare le note suonate utilizzando come riferimento il file pitches.h:
51  suonare una prima, terza, quinta (C-E-G)
52  2. Includere il file pitches.h . Come si potrebbe scrivere una melodia da far suonare
53  autnomamente ad Arduino?
54  */
55
56