Advertisement
Guest User

MakeyMakey Capacitive

a guest
Oct 25th, 2012
1,949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. // Code by Arvid Jense 22/10/2012 at the Music As Material
  2. // Workshop by Eric Rosenbaum and Micheal Smith-Welch in Arnhem
  3.  
  4.  
  5. // CapacitiveSensor tutorial from arduino http://www.arduino.cc/playground/Code/CapacitiveSensor
  6. //
  7. // readCapacitivePin
  8. // Input: Arduino pin number
  9. // Output: A number, from 0 to 17 expressing
  10. // how much capacitance is on the pin
  11. // When you touch the pin, or whatever you have
  12. // attached to it, the number will get higher
  13. #include "pins_arduino.h" // Arduino pre-1.0 needs this
  14. uint8_t readCapacitivePin(int pinToMeasure) {
  15. // Variables used to translate from Arduino to AVR pin naming
  16. volatile uint8_t* port;
  17. volatile uint8_t* ddr;
  18. volatile uint8_t* pin;
  19. // Here we translate the input pin number from
  20. // Arduino pin number to the AVR PORT, PIN, DDR,
  21. // and which bit of those registers we care about.
  22. byte bitmask;
  23. port = portOutputRegister(digitalPinToPort(pinToMeasure));
  24. ddr = portModeRegister(digitalPinToPort(pinToMeasure));
  25. bitmask = digitalPinToBitMask(pinToMeasure);
  26. pin = portInputRegister(digitalPinToPort(pinToMeasure));
  27. // Discharge the pin first by setting it low and output
  28. *port &= ~(bitmask);
  29. *ddr |= bitmask;
  30. delay(1);
  31. // Make the pin an input with the internal pull-up on
  32. *ddr &= ~(bitmask);
  33. *port |= bitmask;
  34.  
  35. // Now see how long the pin to get pulled up. This manual unrolling of the loop
  36. // decreases the number of hardware cycles between each read of the pin,
  37. // thus increasing sensitivity.
  38. uint8_t cycles = 17;
  39. if (*pin & bitmask) { cycles = 0;}
  40. else if (*pin & bitmask) { cycles = 1;}
  41. else if (*pin & bitmask) { cycles = 2;}
  42. else if (*pin & bitmask) { cycles = 3;}
  43. else if (*pin & bitmask) { cycles = 4;}
  44. else if (*pin & bitmask) { cycles = 5;}
  45. else if (*pin & bitmask) { cycles = 6;}
  46. else if (*pin & bitmask) { cycles = 7;}
  47. else if (*pin & bitmask) { cycles = 8;}
  48. else if (*pin & bitmask) { cycles = 9;}
  49. else if (*pin & bitmask) { cycles = 10;}
  50. else if (*pin & bitmask) { cycles = 11;}
  51. else if (*pin & bitmask) { cycles = 12;}
  52. else if (*pin & bitmask) { cycles = 13;}
  53. else if (*pin & bitmask) { cycles = 14;}
  54. else if (*pin & bitmask) { cycles = 15;}
  55. else if (*pin & bitmask) { cycles = 16;}
  56.  
  57. // Discharge the pin again by setting it low and output
  58. // It's important to leave the pins low if you want to
  59. // be able to touch more than 1 sensor at a time - if
  60. // the sensor is left pulled high, when you touch
  61. // two sensors, your body will transfer the charge between
  62. // sensors.
  63. *port &= ~(bitmask);
  64. *ddr |= bitmask;
  65.  
  66. return cycles;
  67. }
  68. // End of capacitive sensing code
  69.  
  70.  
  71. void setup(){
  72. Keyboard.begin();
  73. }
  74.  
  75. int bounceTimes = 6; // setting this higher, will allow you to make the MM less susseptible for false triggers and make it less bouncy (but also less responsive)
  76.  
  77. int touchZero=0;
  78. int touchOne=0;
  79. int touchTwo=0;
  80. int touchThree=0;
  81. int touchFour=0;
  82. int touchFive=0;
  83. int touchSix=0;
  84. int touchSeven=0;
  85. int touchEight=0;
  86. int touchNine=0;
  87. int touchTen=0;
  88. int touchEleven=0;
  89.  
  90. void loop() {
  91.  
  92. if (readCapacitivePin(0)>1){ //the rest state of the pin is 1, so if its any higher, its touched
  93. if(touchZero> bounceTimes){ //we're counting the number of misfires
  94. Keyboard.print("g"); //using pressed/released will work better, but would make the code a bit more complex
  95. touchZero=0;
  96. }
  97. }
  98. else{
  99. touchZero++;
  100. }
  101.  
  102. if (readCapacitivePin(1)>1){
  103. if(touchOne> bounceTimes){
  104. Keyboard.print("f");
  105. touchOne=0;
  106. }
  107. }
  108. else{
  109. touchOne++;
  110. }
  111.  
  112. if (readCapacitivePin(2)>1){
  113. if(touchTwo> bounceTimes){
  114. Keyboard.print("d");
  115. touchTwo=0;
  116. }
  117. }
  118. else{
  119. touchTwo++;
  120. }
  121.  
  122. if (readCapacitivePin(3)>1){
  123. if(touchThree> bounceTimes){
  124. Keyboard.print("s");
  125. touchThree=0;
  126. }
  127. }
  128. else{
  129. touchThree++;
  130. }
  131.  
  132. if (readCapacitivePin(4)>1){
  133. if(touchFour> bounceTimes){
  134. Keyboard.print("a");
  135. touchFour=0;
  136. }
  137. }
  138. else{
  139. touchFour++;
  140. }
  141.  
  142. if (readCapacitivePin(5)>1){
  143. if(touchFive> bounceTimes){
  144. Keyboard.print("w");
  145. touchFive=0;
  146. }
  147. }
  148. else{
  149. touchFive++;
  150. }
  151.  
  152. if (readCapacitivePin(6)>1){
  153. if(touchSix> bounceTimes){
  154. Keyboard.print("z");
  155. touchSix=0;
  156. }
  157. }
  158. else{
  159. touchSix++;
  160. }
  161. if (readCapacitivePin(7)>1){
  162. if(touchSeven> bounceTimes){
  163. Keyboard.print("x");
  164. touchSeven=0;
  165. }
  166. }
  167. else{
  168. touchSeven++;
  169. }
  170. if (readCapacitivePin(8)>1){
  171. if(touchEight> bounceTimes){
  172. Keyboard.print("c");//down
  173. touchEight=0;
  174. }
  175. }
  176. else{
  177. touchEight++;
  178. }
  179. if (readCapacitivePin(12)>1){
  180. if(touchNine> bounceTimes){
  181. Keyboard.print("v"); //up
  182. touchNine=0;
  183. }
  184. }
  185. else{
  186. touchNine++;
  187. }
  188. if (readCapacitivePin(13)>1){
  189. if(touchTen> bounceTimes){
  190. Keyboard.print("b");//left
  191. touchTen=0;
  192. }
  193. }
  194. else{
  195. touchTen++;
  196. }
  197. if (readCapacitivePin(15)>1){
  198. if(touchEleven> bounceTimes){
  199. Keyboard.print("n"); //rightff
  200. touchEleven=0;
  201. }
  202. }
  203. else{
  204. touchEleven++;
  205. }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement