tylercrumpton

Arduino Capacitive Touch Piano Debugging

Jun 26th, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. /*
  2.   Capacitive-Touch Arduino Keyboard Piano Debugging
  3.  
  4.   Displays capacitive touch sensor readings in the Serial Monitor.
  5.  
  6.   Created  18 May 2013
  7.   Modified 26 June 2013
  8.   by Tyler Crumpton and Nicholas Jones
  9.  
  10.   This code is released to the public domain. For information about the circuit,
  11.   visit the Instructable tutorial at http://www.instructables.com/id/Capacitive-Touch-Arduino-Keyboard-Piano/
  12. */
  13.  
  14. #include <CapacitiveSensor.h>
  15. #include "pitches.h"
  16.  
  17. #define COMMON_PIN      2    // The common 'send' pin for all keys
  18. #define BUZZER_PIN      A4   // The output pin for the piezo buzzer
  19. #define NUM_OF_SAMPLES  10   // Higher number whens more delay but more consistent readings
  20. #define CAP_THRESHOLD   150  // Capactive reading that triggers a note (adjust to fit your needs)
  21. #define NUM_OF_KEYS     8    // Number of keys that are on the keyboard
  22.  
  23. // This macro creates a capacitance "key" sensor object for each key on the piano keyboard:
  24. #define CS(Y) CapacitiveSensor(2, Y)
  25.  
  26. // Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
  27. int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5}; // C-Major scale
  28. //int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale
  29. //int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale
  30.  
  31. // Defines the pins that the keys are connected to:
  32. CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9), CS(10)};
  33.  
  34. void setup() {
  35.   // Start up the Serial interface:
  36.   Serial.begin(9600);
  37.   // Turn off autocalibrate on all channels:
  38.   for(int i=0; i<8; ++i) {
  39.     keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);
  40.   }
  41.   // Set the buzzer as an output:
  42.   pinMode(BUZZER_PIN, OUTPUT);
  43. }
  44.  
  45. void loop() {    
  46.   // Loop through each key and print the sensor values:
  47.   for (int i = 0; i < 8; ++i) {
  48.     Serial.print(i);
  49.     Serial.print(":");
  50.     Serial.print(keys[i].capacitiveSensor(NUM_OF_SAMPLES));
  51.     Serial.print("  ");
  52.   }
  53.   // Add a newline after all of the values have been printed:
  54.   Serial.println(" ");
  55. }
Add Comment
Please, Sign In to add comment