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