#include <CapacitiveSensor.h>
#define buzzer 44
#define sensi 100
#define limit 1000
// Set the Send Pin & Receive Pin.
CapacitiveSensor   cs_2_3 = CapacitiveSensor(2, 3);
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2, 4);
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2, 5);
CapacitiveSensor   cs_2_6 = CapacitiveSensor(2, 6);
CapacitiveSensor   cs_2_7 = CapacitiveSensor(2, 7);
CapacitiveSensor   cs_2_8 = CapacitiveSensor(2, 8);
CapacitiveSensor   cs_2_9 = CapacitiveSensor(2, 9);
CapacitiveSensor   cs_2_10 = CapacitiveSensor(2, 10);
CapacitiveSensor   cs_2_11 = CapacitiveSensor(2, 11);

int melody[] = {784, 659, 659, 0, 784};
int noteDurations[] = {12, 20, 20, 12, 12};

boolean muted = false;
//const int mute = 21;



void setup()
{
  // set serial baud rate
  Serial.begin(9600);


  for (int thisNote = 0; thisNote < sizeof(melody)/sizeof(int); thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(buzzer , melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration *1.30;
    delay(pauseBetweenNotes);
    noTone(buzzer);
  }

}






void loop()
{

  // Set the sensitivity of the sensors.
  long sense1 =  cs_2_3.capacitiveSensor(sensi);
  long sense2 =  cs_2_4.capacitiveSensor(sensi);
  long sense3 =  cs_2_5.capacitiveSensor(sensi);
  long sense4 =  cs_2_6.capacitiveSensor(sensi);
  long sense5 =  cs_2_7.capacitiveSensor(sensi);
  long sense6 =  cs_2_8.capacitiveSensor(sensi);
  long sense7 =  cs_2_9.capacitiveSensor(sensi);
  long sense8 =  cs_2_10.capacitiveSensor(sensi);
  long mute =  cs_2_11.capacitiveSensor(sensi);


  if (mute > limit) {

    delay(300);
    Serial.println("Muted / Unmuted");
    muted = !muted;

  }


  // When we touched the sensor, the buzzer will produce a tone.


  if (muted == false) {
    if (sense1 > limit) {
      tone(buzzer, 400);
      Serial.println("Key 1");
    }
    if (sense2 > limit) {
      tone(buzzer, 270);
      Serial.println("Key 2");
    }
    if (sense3 > limit) {
      tone(buzzer, 650);
      Serial.println("Key 3");
    }
    if (sense4 > limit) {
      tone(buzzer, 900);
      Serial.println("Key 4");
    }
    if (sense5 > limit) {
      tone(buzzer, 1100);
      Serial.println("Key 5");
    }
    if (sense6 > limit) {
      tone(buzzer, 1300);
      Serial.println("Key 6");
    }
    if (sense7 > limit) {
      tone(buzzer, 1670);
      Serial.println("Key 7");
    }
    if (sense8 > limit) {
      tone(buzzer, 2000);
      Serial.println("Key 8");
    }
  }
  else {
    
    Serial.println("Piano Is Muted!");
    }
  // When we didn't touch it, no tone is produced.
  if (sense1 <= limit  &  sense2 <= limit  &  sense3 <= limit & sense4 <= limit  &  sense5 <= limit  &  sense6 <= limit &  sense7 <= limit &  sense8 <= limit)
    noTone(buzzer);
  delay(10);
}
