Advertisement
lukaszfelski

Untitled

Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <CapacitiveSensor.h>
  2. #include "pitches.h"
  3.  
  4. #define COMMON_PIN      2    // The common 'send' pin for all keys
  5. #define BUZZER_PIN      13   // The output pin for the piezo buzzer
  6. #define NUM_OF_SAMPLES  10   // Higher number whens more delay but more consistent readings
  7. #define CAP_THRESHOLD   150  // Capactive reading that triggers a note (adjust to fit your needs)
  8. #define NUM_OF_KEYS     8    // Number of keys that are on the keyboard
  9.  
  10. // This macro creates a capacitance "key" sensor object for each key on the piano keyboard:
  11. #define CS(Y) CapacitiveSensor(2, Y)
  12.  
  13. // Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
  14.  int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; // C-Major scale
  15.  //int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale
  16.  //int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale
  17.  
  18. // Defines the pins that the keys are connected to:
  19. CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9), CS(10)};
  20. const int trigPin = 12;
  21. const int echoPin = 13;
  22. long duration;
  23. int distance;
  24.  
  25. void setup() {
  26.   // Turn off autocalibrate on all channels:
  27.   for(int i=0; i<8; ++i) {
  28.     keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);
  29.   }
  30.   // Set the buzzer as an output:
  31.   pinMode(BUZZER_PIN, OUTPUT);
  32. }
  33.  
  34. void loop() {    
  35.   // Loop through each key:
  36.   for (int i = 0; i < 8; ++i) {
  37.     // If the capacitance reading is greater than the threshold, play a note:
  38.     if(keys[i].capacitiveSensor(NUM_OF_SAMPLES) > CAP_THRESHOLD) {
  39.       tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed
  40.     }
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement