#include CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil /* CapacitiveSensor cs_5_2 = CapacitiveSensor(5,2); // big resistor between pins 5 & 2, pin 2 is sensor pin, add wire, foil CapacitiveSensor cs_6_2 = CapacitiveSensor(6,2); // big resistor between pins 6 & 2, pin 2 is sensor pin, add wire, foil */ #define LED 9 // LED pilotato in PWM - digital pin 9 #define BUZZ 8 // uscita cuffia - digital pin 8 #define PASSOLOOP 100 // L'intervallo base di rilevazione degli input - ritardo in millisecondi inserito nel loop() #define MAXNOTE 60 long CsValori[4]= {100,200,500,3000}; // Vettore di confronto per Cs : accordatore /* ______________________________________________________ * * Stato dinamico del sistema */ byte tensione=0; // La tensione con cui pilotare il duty cicle - il PWM va da 0 a 255 unsigned int nota=0; // La nota con cui suonare la cuffia (0= nessun suono) void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example /* cs_5_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // set autocalibrate on channel 2 - 20sec cs_6_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // set autocalibrate on channel 3 - 20sec */ pinMode(LED,OUTPUT); pinMode(BUZZ,OUTPUT); Serial.begin(115200); } void loop() { long start = millis(); long total1 = cs_4_2.capacitiveSensor(100); /* long total2 = cs_5_2.capacitiveSensor(100); long total3 = cs_6_2.capacitiveSensor(100); */ if (total1CsValori[3]) { tensione = 255; nota = MAXNOTE-1; } else { tensione= (float)(total1)/(CsValori[3])*255; nota= (float)(total1)/(CsValori[3])*(MAXNOTE-1); } } Serial.print(millis() - start); // check on performance in milliseconds Serial.print("\t"); // tab character for debug window spacing Serial.print(total1); // print sensor output 1 /* Serial.print("\t"); // tab character for debug window spacing Serial.print(total2); // print sensor output 1 Serial.print("\t"); // tab character for debug window spacing Serial.print(total3); // print sensor output 1Serial.print(tensione); // print tensione PWM applicata a LED */ Serial.print("\t"); // tab character for debug window spacing Serial.print(tensione); // print tensione PWM applicata a LED Serial.print("\t"); // tab character for debug window spacing Serial.print(nota); // print nota applicata Serial.println(); analogWrite(LED,tensione); Nota(BUZZ,nota); // parametri attuali delay(PASSOLOOP); // arbitrary delay to limit data to serial port } /* * Nota() * * Suona dalla I alla V ottava * La voce umana va dalla II alla V ottava * Il pianoforte ha 7 ottave di estensione * * */ int Nota(byte pinOut,byte semitono) { // parametri formali const int TONEOFF=0; const int TONEON=1; const int ERROR_RV=-1; const byte tonomax=60; unsigned int note[60]= { 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98,104,110,117,123, 131,139,147,156,165,175,185,196,208,220,233,247, 262,277,294,311,330,349,370,392,415,440,466,494, 523,554,587,622,659,698,740,784,831,880,932,988 }; if (semitono == 0) { noTone(pinOut); return TONEOFF; } else { if (semitono>tonomax) { noTone(pinOut); return ERROR_RV; } else { tone(pinOut, note[semitono-1]); return TONEON; } } }