flora_game_controller.ino
1 // SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries 2 // SPDX-FileCopyrightText: 2019 Becky Stern for Adafruit Industries 3 // 4 // SPDX-License-Identifier: MIT 5 6 /* 7 Example code for a Flora game controller with capacitive touch sensing! Full tutorial and video: 8 http://learn.adafruit.com/plush-game-controller/ 9 Uses Modern Device's Capacitive Sensing library: https://github.com/moderndevice/CapSense 10 Adafruit invests time and resources providing this open source code, 11 please support Adafruit and open-source hardware by purchasing 12 products from Adafruit! 13 Written by Limor Fried & Becky Stern for Adafruit Industries. 14 BSD license, all text above must be included in any redistribution 15 16 */ 17 #include <CapPin.h> 18 #include <Keyboard.h> 19 20 CapPin cPin_10 = CapPin(10); // read pin 10 (D10 on Flora) - connect to NES B 21 CapPin cPin_9 = CapPin(9); // read pin 9 (D9 on Flora) - connect to NES A 22 CapPin cPin_6 = CapPin(6); // read pin 6 (D6 on Flora) - connect to NES Start 23 CapPin cPin_12 = CapPin(12); // read pin 12 (D12 on Flora) - connect to NES Select 24 CapPin cPin_1 = CapPin(1); // read pin 1 (TX on Flora) - connect to NES right 25 CapPin cPin_0 = CapPin(0); // read pin 0 (RX on Flora) - connect to NES up 26 CapPin cPin_2 = CapPin(2); // read pin 2 (SDA on Flora) - connect to NES left 27 CapPin cPin_3 = CapPin(3); // read pin 3 (SCL on Flora) - connect to NES down 28 29 CapPin pins[] = {cPin_10, cPin_9, cPin_6, cPin_12, cPin_1, cPin_0, cPin_2, cPin_3}; 30 // check http://arduino.cc/en/Reference/KeyboardModifiers for more info on unique keys 31 32 // WASD D-pad, select = Return, start = Space, LeftButton = z, RightButton = x 33 //char Keys[] = { 'x', 'z', ' ', KEY_RETURN, 'd', 'w', 'a', 's'}; 34 35 // arrow D-pad, select = Return, start = Space, LeftButton = b, RightButton = a 36 char Keys[] = { 'a', 'b', ' ', KEY_RETURN, KEY_RIGHT_ARROW, KEY_UP_ARROW, KEY_LEFT_ARROW, KEY_DOWN_ARROW}; 37 38 boolean currentPressed[] = {false, false, false, false, false, false, false, false}; 39 40 // Capactive touch threashhold, you might want to mess with this if you find its too 41 // sensitive or not sensitive enough 42 #define THRESH 500 43 44 float smoothed[8] = {0,0,0,0,0,0,0,0}; 45 46 void setup() 47 { 48 //while (!Serial) 49 Serial.begin(115200); 50 Serial.println("start"); 51 Keyboard.begin(); 52 } 53 54 55 void loop() 56 { 57 for (int i=0;i<8;i++) { 58 delay(1); 59 long total1 = 0; 60 long start = millis(); 61 long total = pins[i].readPin(2000); 62 63 // check if we are sensing that a finger is touching the pad 64 // and that it wasnt already pressed 65 if ((total > THRESH) && (! currentPressed[i])) { 66 Serial.print("Key pressed #"); Serial.print(i); 67 Serial.print(" ("); Serial.print(Keys[i]); Serial.println(")"); 68 currentPressed[i] = true; 69 70 Keyboard.press(Keys[i]); 71 } 72 else if ((total <= THRESH) && (currentPressed[i])) { 73 // key was released (no touch, and it was pressed before) 74 Serial.print("Key released #"); Serial.print(i); 75 Serial.print(" ("); Serial.print(Keys[i]); Serial.println(")"); 76 currentPressed[i] = false; 77 78 Keyboard.release(Keys[i]); 79 } 80 81 /* 82 // simple lowpass filter to take out some of the jitter 83 // change parameter (0 is min, .99 is max) or eliminate to suit 84 smoothed[i] = smooth(total, .8, smoothed[i]); 85 Serial.print(i); Serial.print(": "); 86 Serial.print( millis() - start); // time to execute in mS 87 Serial.print("ms \t"); 88 Serial.print(total); // raw total 89 Serial.print("\t->\t"); 90 Serial.println((int) smoothed[i]); // smoothed 91 */ 92 delay(5); 93 } 94 } 95 96 // simple lowpass filter 97 // requires recycling the output in the "smoothedVal" param 98 int smooth(int data, float filterVal, float smoothedVal){ 99 100 if (filterVal > 1){ // check to make sure param's are within range 101 filterVal = .999999; 102 } 103 else if (filterVal <= 0){ 104 filterVal = 0; 105 } 106 107 smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal); 108 109 return (int)smoothedVal; 110 }