Created
October 24, 2016 18:31
-
-
Save darkwave/539535a7560195c5e85c48d0b3941819 to your computer and use it in GitHub Desktop.
Arduino Leonardo as Keyboard (like Makey Makey) using Capacitive Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CapacitiveSensor.h> | |
#include <Wire.h> | |
#include "rgb_lcd.h" | |
#include "Keyboard.h" | |
#define NUMBER_OF_KEYS 4 | |
#define KEYBOARD 1 | |
#define TEST 0 | |
int currentMode = 0; | |
CapacitiveSensor sensors[] = {CapacitiveSensor(6,7), CapacitiveSensor(6,8), CapacitiveSensor(6,9), CapacitiveSensor(6,10)};//, CapacitiveSensor(6,11)}; | |
long threshold[NUMBER_OF_KEYS]; | |
rgb_lcd lcd; | |
void setup() { | |
lcd.begin(16, 2); | |
lcd.setRGB(127, 255, 0); | |
// Print a message to the LCD. | |
lcd.print("Welcome"); | |
delay(800); | |
lcd.clear(); | |
lcd.print("Setup - Step 1"); | |
lcd.setCursor(0, 1); | |
lcd.print("Don't touch any key"); | |
for (int i = 0; i < NUMBER_OF_KEYS; i++) { | |
//sensors[i] = CapacitiveSensor(6,i + 5) | |
long max = 0; | |
for (int j = 0; j < 100; j++) { | |
long currentValue = sensors[i].capacitiveSensor(30); | |
if (max < currentValue) | |
max = currentValue; | |
} | |
threshold[i] = max; | |
lcd.clear(); | |
lcd.print("K"); | |
lcd.print(i); | |
lcd.print(" "); | |
lcd.print(threshold[i]); | |
delay(50); | |
Keyboard.begin(); | |
} | |
//cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example | |
// Serial.begin(9600); | |
//Keyboard | |
} | |
void loop() | |
{ | |
long currentValues[NUMBER_OF_KEYS]; | |
int margin = analogRead(A0); //variable margin | |
margin *= 2; | |
for (int i = 0; i < NUMBER_OF_KEYS; i++) { | |
//sensors[i] = CapacitiveSensor(6,i + 5); | |
long sum = 0; | |
for (int j = 0; j < 8; j++) { | |
sum += sensors[i].capacitiveSensor(30); | |
} | |
currentValues[i] = sum / 8; | |
//lcd.clear(); | |
lcd.setCursor((i * 5) % 10, (i * 5) / 10); | |
lcd.print("K"); | |
lcd.print(i); | |
lcd.print(" "); | |
if (currentValues[i] - threshold[i] > margin) { | |
lcd.print("_"); | |
if (currentMode == KEYBOARD) { | |
if (i == 0) | |
Keyboard.press(32); | |
else | |
Keyboard.press(KEY_RIGHT_ARROW + i); | |
} | |
} | |
else | |
lcd.print("- "); | |
} | |
Keyboard.releaseAll(); | |
lcd.setCursor(11, 1); | |
lcd.print(margin); | |
if (currentMode == KEYBOARD) { | |
lcd.print(" K"); | |
} else | |
lcd.print(" T"); | |
int buttonReading = digitalRead(4); | |
if (currentMode == TEST && buttonReading == 1) | |
currentMode = KEYBOARD; | |
else if (currentMode == KEYBOARD && buttonReading == 1) | |
currentMode = TEST; | |
delay(10); | |
/* | |
//long start = millis(); | |
long sum = 0; | |
for (int i = 0; i < 32; i++) { | |
long total1 = cs_4_2.capacitiveSensor(30); | |
sum += total1; | |
} | |
long result = sum / 32; | |
*/ | |
/* | |
long total2 = cs_4_6.capacitiveSensor(30); | |
long total3 = cs_4_8.capacitiveSensor(30); | |
*/ | |
//lcd.clear(); | |
//lcd.print(result); | |
/* | |
Serial.print(millis() - start); // check on performance in milliseconds | |
Serial.print("\t"); // tab character for debug windown spacing | |
Serial.print(total1); // print sensor output 1 | |
Serial.print("\t"); | |
Serial.print(total2); // print sensor output 2 | |
Serial.print("\t"); | |
Serial.println(total3); // print sensor output 3 | |
*/ | |
delay(50); // arbitrary delay to limit data to serial port | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment