Guest User

Untitled

a guest
Feb 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <Wire.h>
  3. #include "Adafruit_MPR121.h"
  4. #ifndef _BV
  5. #define _BV(bit) (1 << (bit))
  6. #endif
  7.  
  8. // You can have up to 4 on one i2c bus but one is enough for testing!
  9. Adafruit_MPR121 cap = Adafruit_MPR121();
  10.  
  11. // Keeps track of the last pins touched
  12. // so we know when buttons are 'released'
  13. uint16_t lasttouched = 0;
  14. uint16_t currtouched = 0;
  15. int baseNote = 48; // C4, used as the base note
  16.  
  17. SoftwareSerial midiSerial(2, 3); // digital pins that we'll use for soft serial RX & TX
  18.  
  19. void setup() {
  20. Serial.begin(9600);
  21. midiSerial.begin(31250);
  22.  
  23. while (!Serial) { // needed to keep leonardo/micro from starting too fast!
  24. delay(10);
  25. }
  26.  
  27. Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
  28.  
  29. // Default address is 0x5A, if tied to 3.3V its 0x5B
  30. // If tied to SDA its 0x5C and if SCL then 0x5D
  31. if (!cap.begin(0x5A)) {
  32. Serial.println("MPR121 not found, check wiring?");
  33. while (1);
  34. }
  35. Serial.println("MPR121 found!");
  36. }
  37.  
  38. void loop() {
  39. // Get the currently touched pads
  40. currtouched = cap.touched();
  41. //Serial.println(currtouched);
  42. for (uint8_t i = 0; i < 12; i++) {
  43. // it if *is* touched and *wasnt* touched before, alert!
  44. int thisCommand = 0;
  45. int thisNote = i + baseNote; // calculate note
  46.  
  47. if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
  48. //
  49. Serial.print(i); Serial.println(" touched");
  50. thisCommand = 0x90;
  51. midiCommand(thisCommand, thisNote, 127); // play or stop the note
  52. // Serial.println(thisNote, HEX); // print note
  53.  
  54. }
  55.  
  56. // if it *was* touched and now *isnt*, alert!
  57. if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
  58. Serial.print(i); Serial.println(" released");
  59. thisCommand = 0x80;
  60. midiCommand(thisCommand, thisNote, 127); // play or stop the note
  61. // Serial.println(thisNote, HEX); // print note
  62. }
  63. }
  64.  
  65. // reset our state
  66. lasttouched = currtouched;
  67. delay(100);
  68. }
  69.  
  70. void midiCommand(byte cmd, byte data1, byte data2) {
  71. Serial.print("sending midi note: ");
  72. Serial.print(cmd);
  73. Serial.print(" ");
  74. Serial.print(data1); //note
  75. Serial.print(" ");
  76. Serial.println(data2); //speed
  77.  
  78. midiSerial.write(cmd); // command byte (should be > 127)
  79. midiSerial.write(data1); // data byte 1 (should be < 128)
  80. midiSerial.write(data2); // data byte 2 (should be < 128)
  81. delay(50);
  82. }
Add Comment
Please, Sign In to add comment