Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
abhilash_patel
Published © GPL3+

DIY: Arduino Based Continuous Touch Piano

Tutorial on making a continuous touch keyboard that can play any frequency between the specified range.

IntermediateFull instructions provided2,474
DIY: Arduino Based Continuous Touch Piano

Things used in this project

Hardware components

Wooden block
length: 200mm, width: 80mm, thickness: 5-10 mm (these are only reference value)
×1
Arduino Nano R3
Arduino Nano R3
×1
Aluminium foil
×1
87k resistor
×20
17k Resistor
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Arduino Based Continuous Touch Piano

Arduino
//connect a speaker to pin number 12,
//connect a common resistor pin to pin 4 but with series in 17k resistor


#include <CapacitiveSensor.h>

float N_min=48; //here left most plate have frequency is 48th  key of standard keyboard you can change it as per requirement
float N_max=70; //here right most plate have frequency is 70th  key of standard keyboard you can change it as per requirement
int smooth=2;     // value of smoothing (recomendded value 1-4)
float N[20];
float key, freq,kt;
float  k[52],m;
int t4=0;

long x[20];

CapacitiveSensor   i20 = CapacitiveSensor(4,A6);  //KEY 1 TO A6
CapacitiveSensor   i13 = CapacitiveSensor(4,13);  //KEY 2 TO 13
CapacitiveSensor   i19 = CapacitiveSensor(4,A5);  //KEY 3 TO A5
CapacitiveSensor   i18 = CapacitiveSensor(4,A4);  //KEY 4 TO A4
CapacitiveSensor   i17 = CapacitiveSensor(4,A3);  //KEY 5 TO A3
CapacitiveSensor   i16 = CapacitiveSensor(4,A2);  //KEY 6 TO A2
CapacitiveSensor   i15 = CapacitiveSensor(4,A1);  //KEY 7 TO A1
CapacitiveSensor   i14 = CapacitiveSensor(4,A0);  //KEY 8 TO A7

CapacitiveSensor   i2 = CapacitiveSensor(4,2);  //KEY 9 TO 2
CapacitiveSensor   i3 = CapacitiveSensor(4,3);  //KEY 10 TO 3
CapacitiveSensor   i5 = CapacitiveSensor(4,5);  //KEY 11 TO 5
CapacitiveSensor   i6 = CapacitiveSensor(4,6);  //KEY 12 TO 6
CapacitiveSensor   i7 = CapacitiveSensor(4,7);  //KEY 13 TO 7
CapacitiveSensor   i8 = CapacitiveSensor(4,8);  //KEY 14 TO 8
CapacitiveSensor   i9 = CapacitiveSensor(4,9);  //KEY 15 TO 9
CapacitiveSensor   i10 = CapacitiveSensor(4,10);//KEY 16 TO 10
CapacitiveSensor   i11 = CapacitiveSensor(4,11);//KEY 17 TO 11



void setup()                    
{
   Serial.begin(250000);
   N_map();   //map range of frequency as per min and max value
}

void loop()                    
{


read_val();   //read raw data from touch plates
print_raw();  // print raw value for debugging
//Serial.println(t4);   //key number: to check key you touch for debugging

key_calc();     // detect location of touch
avg_key();      // take average of few keys
freq_calc();   // calculate value of frequency as per key
tone(12,freq,1000);   //generate tone of that frequency
}

//=================================END===========================================================================================//


//=================================FUNCTION=====================================================================================//



//----------------------------------------------------------------------------------------------------------------------------------//
void N_map(){
  float j=0;
  for(int i=1;i<19;i++){j=j+1;
    N[i]=N_min+((j/18)*(N_max-N_min));   //map keys linearly for 18 keys
      }
  }


//----------------------------------------------------------------------------------------------------------------------------------//
void read_val(){
  long start = millis();
    x[1] =  i20.capacitiveSensor(30);
    x[2] =  i13.capacitiveSensor(30);
    x[3] =  i19.capacitiveSensor(30);
    x[4] =  i18.capacitiveSensor(30);
    x[5] =  i17.capacitiveSensor(30);
    x[6] =  i16.capacitiveSensor(30);
    x[7] =  i15.capacitiveSensor(30);
    x[8] =  i14.capacitiveSensor(30);
    x[9] =  i2.capacitiveSensor(30);    // using capacitance library for data read
    x[10] =  i3.capacitiveSensor(30);
    x[11] =  i5.capacitiveSensor(30);
    x[12] =  i6.capacitiveSensor(30);
    x[13] =  i7.capacitiveSensor(30);
    x[14] =  i8.capacitiveSensor(30);
    x[15] =  i9.capacitiveSensor(30);
    x[16] =  i10.capacitiveSensor(30);
    x[17] =  i11.capacitiveSensor(30);
    x[18] =0; // i12.capacitiveSensor(30);
}




//----------------------------------------------------------------------------------------------------------------------------------//
void key_calc(){
 float t1,z1,z2=0;
 m=0;
 for(int z=1;z<19;z++){
 // if(x[z]>80){m=1;}
  if(x[z]>t1){t1=x[z];t4=z;}       //determine plate with max reading of touch 
 }
 for(int z=1;z<19;z++){
  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.
 }
  
kt=((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
/*for(int i=1;i<19;i++){
  z1=z1+(x[i]*N[i]);       // using all key value for interpolation of touch position (noise problem)
  z2=z2+x[i];
}
kt=z1/z2;*/
 
}



//----------------------------------------------------------------------------------------------------------------------------------//
void avg_key(){
  int smm=smooth;            
  for(int s=smm; s>1;s--){   //taking average of last 'smm' keys
    k[s]=k[s-1]; 
  } k[1]=kt;
float stt=0;               
for(int r=1;r<smm+1;r++){
   stt=stt+k[r];
 }
key=stt/smm;
}



//----------------------------------------------------------------------------------------------------------------------------------//
void freq_calc(){
  float n;
  n=(key-49)/12;          //calculating frequency for key value
  freq=m*220*pow(2,n);
}




//----------------------------------------------------------------------------------------------------------------------------------//
void print_raw(){
      
    Serial.print(x[1]); Serial.print("\t");
    Serial.print(x[2]); Serial.print("\t");
    Serial.print(x[3]); Serial.print("\t");
    Serial.print(x[4]); Serial.print("\t");
    Serial.print(x[5]); Serial.print("\t");
    Serial.print(x[6]); Serial.print("\t");
    Serial.print(x[7]); Serial.print("\t");
    Serial.print(x[8]); Serial.print("\t");
    Serial.print(x[9]); Serial.print("\t");
    Serial.print(x[10]); Serial.print("\t");
    Serial.print(x[11]); Serial.print("\t");
    Serial.print(x[12]); Serial.print("\t"); 
    Serial.print(x[13]); Serial.print("\t");
    Serial.print(x[14]); Serial.print("\t");
    Serial.print(x[15]); Serial.print("\t");
    Serial.print(x[16]); Serial.print("\t");
    Serial.print(x[17]); Serial.print("\t");
    Serial.print(x[18]); Serial.print("\n");
}
//======================================================================================================================================//

Credits

abhilash_patel
0 projects • 26 followers
Contact

Comments

Please log in or sign up to comment.