Components and supplies
17k Resistor
Wooden block
87k resistor
Aluminium foil
Speaker: 0.25W, 8 ohms
Arduino Nano R3
Apps and platforms
Arduino IDE
Project description
Code
Arduino Based Continuous Touch Piano
arduino
1//connect a speaker to pin number 12, 2//connect a common resistor pin to pin 4 but with series in 17k resistor 3 4 5#include <CapacitiveSensor.h> 6 7float N_min=48; //here left most plate have frequency is 48th key of standard keyboard you can change it as per requirement 8float N_max=70; //here right most plate have frequency is 70th key of standard keyboard you can change it as per requirement 9int smooth=2; // value of smoothing (recomendded value 1-4) 10float N[20]; 11float key, freq,kt; 12float k[52],m; 13int t4=0; 14 15long x[20]; 16 17CapacitiveSensor i20 = CapacitiveSensor(4,A6); //KEY 1 TO A6 18CapacitiveSensor i13 = CapacitiveSensor(4,13); //KEY 2 TO 13 19CapacitiveSensor i19 = CapacitiveSensor(4,A5); //KEY 3 TO A5 20CapacitiveSensor i18 = CapacitiveSensor(4,A4); //KEY 4 TO A4 21CapacitiveSensor i17 = CapacitiveSensor(4,A3); //KEY 5 TO A3 22CapacitiveSensor i16 = CapacitiveSensor(4,A2); //KEY 6 TO A2 23CapacitiveSensor i15 = CapacitiveSensor(4,A1); //KEY 7 TO A1 24CapacitiveSensor i14 = CapacitiveSensor(4,A0); //KEY 8 TO A7 25 26CapacitiveSensor i2 = CapacitiveSensor(4,2); //KEY 9 TO 2 27CapacitiveSensor i3 = CapacitiveSensor(4,3); //KEY 10 TO 3 28CapacitiveSensor i5 = CapacitiveSensor(4,5); //KEY 11 TO 5 29CapacitiveSensor i6 = CapacitiveSensor(4,6); //KEY 12 TO 6 30CapacitiveSensor i7 = CapacitiveSensor(4,7); //KEY 13 TO 7 31CapacitiveSensor i8 = CapacitiveSensor(4,8); //KEY 14 TO 8 32CapacitiveSensor i9 = CapacitiveSensor(4,9); //KEY 15 TO 9 33CapacitiveSensor i10 = CapacitiveSensor(4,10);//KEY 16 TO 10 34CapacitiveSensor i11 = CapacitiveSensor(4,11);//KEY 17 TO 11 35 36 37 38void setup() 39{ 40 Serial.begin(250000); 41 N_map(); //map range of frequency as per min and max value 42} 43 44void loop() 45{ 46 47 48read_val(); //read raw data from touch plates 49print_raw(); // print raw value for debugging 50//Serial.println(t4); //key number: to check key you touch for debugging 51 52key_calc(); // detect location of touch 53avg_key(); // take average of few keys 54freq_calc(); // calculate value of frequency as per key 55tone(12,freq,1000); //generate tone of that frequency 56} 57 58//=================================END===========================================================================================// 59 60 61//=================================FUNCTION=====================================================================================// 62 63 64 65//----------------------------------------------------------------------------------------------------------------------------------// 66void N_map(){ 67 float j=0; 68 for(int i=1;i<19;i++){j=j+1; 69 N[i]=N_min+((j/18)*(N_max-N_min)); //map keys linearly for 18 keys 70 } 71 } 72 73 74//----------------------------------------------------------------------------------------------------------------------------------// 75void read_val(){ 76 long start = millis(); 77 x[1] = i20.capacitiveSensor(30); 78 x[2] = i13.capacitiveSensor(30); 79 x[3] = i19.capacitiveSensor(30); 80 x[4] = i18.capacitiveSensor(30); 81 x[5] = i17.capacitiveSensor(30); 82 x[6] = i16.capacitiveSensor(30); 83 x[7] = i15.capacitiveSensor(30); 84 x[8] = i14.capacitiveSensor(30); 85 x[9] = i2.capacitiveSensor(30); // using capacitance library for data read 86 x[10] = i3.capacitiveSensor(30); 87 x[11] = i5.capacitiveSensor(30); 88 x[12] = i6.capacitiveSensor(30); 89 x[13] = i7.capacitiveSensor(30); 90 x[14] = i8.capacitiveSensor(30); 91 x[15] = i9.capacitiveSensor(30); 92 x[16] = i10.capacitiveSensor(30); 93 x[17] = i11.capacitiveSensor(30); 94 x[18] =0; // i12.capacitiveSensor(30); 95} 96 97 98 99 100//----------------------------------------------------------------------------------------------------------------------------------// 101void key_calc(){ 102 float t1,z1,z2=0; 103 m=0; 104 for(int z=1;z<19;z++){ 105 // if(x[z]>80){m=1;} 106 if(x[z]>t1){t1=x[z];t4=z;} //determine plate with max reading of touch 107 } 108 for(int z=1;z<19;z++){ 109 if(x[z]>40){m=1;} // setting a variable 'm' it is 1 only if proper touch is done other wise it is zero which set sound zero too. 110 } 111 112kt=((x[t4-1]*N[t4-1])+(x[t4]*N[t4])+(x[t4+1]*N[t4+1]))/(x[t4-1]+x[t4]+x[t4+1]); //interpolating touch position using 1 nearby key on both side(total 3), from max touch o/p key 113/*for(int i=1;i<19;i++){ 114 z1=z1+(x[i]*N[i]); // using all key value for interpolation of touch position (noise problem) 115 z2=z2+x[i]; 116} 117kt=z1/z2;*/ 118 119} 120 121 122 123//----------------------------------------------------------------------------------------------------------------------------------// 124void avg_key(){ 125 int smm=smooth; 126 for(int s=smm; s>1;s--){ //taking average of last 'smm' keys 127 k[s]=k[s-1]; 128 } k[1]=kt; 129float stt=0; 130for(int r=1;r<smm+1;r++){ 131 stt=stt+k[r]; 132 } 133key=stt/smm; 134} 135 136 137 138//----------------------------------------------------------------------------------------------------------------------------------// 139void freq_calc(){ 140 float n; 141 n=(key-49)/12; //calculating frequency for key value 142 freq=m*220*pow(2,n); 143} 144 145 146 147 148//----------------------------------------------------------------------------------------------------------------------------------// 149void print_raw(){ 150 151 Serial.print(x[1]); Serial.print("\ "); 152 Serial.print(x[2]); Serial.print("\ "); 153 Serial.print(x[3]); Serial.print("\ "); 154 Serial.print(x[4]); Serial.print("\ "); 155 Serial.print(x[5]); Serial.print("\ "); 156 Serial.print(x[6]); Serial.print("\ "); 157 Serial.print(x[7]); Serial.print("\ "); 158 Serial.print(x[8]); Serial.print("\ "); 159 Serial.print(x[9]); Serial.print("\ "); 160 Serial.print(x[10]); Serial.print("\ "); 161 Serial.print(x[11]); Serial.print("\ "); 162 Serial.print(x[12]); Serial.print("\ "); 163 Serial.print(x[13]); Serial.print("\ "); 164 Serial.print(x[14]); Serial.print("\ "); 165 Serial.print(x[15]); Serial.print("\ "); 166 Serial.print(x[16]); Serial.print("\ "); 167 Serial.print(x[17]); Serial.print("\ "); 168 Serial.print(x[18]); Serial.print("\ 169"); 170} 171//======================================================================================================================================//
Comments
Only logged in users can leave comments
abhilashpatel121
6 Followers
•9 Projects
3
0