forked from sparkfun/MaKeyMaKey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakey_makey.ino
787 lines (708 loc) · 20.5 KB
/
makey_makey.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
************************************************
************** MAKEY MAKEY *********************
************************************************
/////////////////////////////////////////////////
/////////////HOW TO EDIT THE KEYS ///////////////
/////////////////////////////////////////////////
- Edit keys in the settings.h file
- that file should be open in a tab above (in Arduino IDE)
- more instructions are in that file
//////////////////////////////////////////////////
///////// MaKey MaKey FIRMWARE v1.4.1 ////////////
//////////////////////////////////////////////////
by: Eric Rosenbaum, Jay Silver, and Jim Lindblom
MIT Media Lab & Sparkfun
start date: 2/16/2012
current release: 7/5/2012
Heavy improvements by David Rieman, see: https://github.com/DavidRieman/MaKeyMaKey_Flexible
*/
/////////////////////////
// DEBUG DEFINITIONS ////
/////////////////////////
//#define DEBUG
//#define DEBUG2
//#define DEBUG3
//#define DEBUG_TIMING
//#define DEBUG_MOUSE
//#define DEBUG_TIMING2
////////////////////////
// DEFINED CONSTANTS////
////////////////////////
#define BUFFER_LENGTH 3 // 3 bytes gives us 24 samples
#define NUM_INPUTS 18 // 6 on the front + 12 on the back
#define OPTIONS_PER_INPUT 5
#define KEYS_PER_INPUT 3
//#define TARGET_LOOP_TIME 694 // (1/60 seconds) / 24 samples = 694 microseconds per sample
//#define TARGET_LOOP_TIME 758 // (1/55 seconds) / 24 samples = 758 microseconds per sample
#define TARGET_LOOP_TIME 744 // (1/56 seconds) / 24 samples = 744 microseconds per sample
// id numbers for mouse movement inputs (used in settings.h)
#define MOUSE_MOVE_UP -1
#define MOUSE_MOVE_DOWN -2
#define MOUSE_MOVE_LEFT -3
#define MOUSE_MOVE_RIGHT -4
#include "settings.h"
/////////////////////////
// STRUCT ///////////////
/////////////////////////
typedef struct {
byte pinNumber;
int keyCode[KEYS_PER_INPUT];
byte measurementBuffer[BUFFER_LENGTH];
boolean oldestMeasurement;
byte bufferSum;
boolean pressed;
boolean prevPressed;
boolean isMouseMotion;
boolean isMouseButton;
boolean isKeyboardKey;
boolean isReverseInput;
boolean isCapacitive;
}
MakeyMakeyInput;
MakeyMakeyInput inputs[NUM_INPUTS];
///////////////////////////////////
// VARIABLES //////////////////////
///////////////////////////////////
int bufferIndex = 0;
byte byteCounter = 0;
byte bitCounter = 0;
int mouseMovementCounter = 0; // for sending mouse movement events at a slower interval
int pressThreshold;
int releaseThreshold;
boolean inputChanged;
int mouseHoldCount[NUM_INPUTS]; // used to store mouse movement hold data
// Pin Numbers
// input pin numbers for kickstarter production board
int pinNumbers[NUM_INPUTS] = {
12, 8, 13, 15, 7, 6, // top of makey makey board (up, down, left, right, space, click)
5, 4, 3, 2, 1, 0, // left side of female header, KEYBOARD
23, 22, 21, 20, 19, 18 // right side of female header, MOUSE
};
// input status LED pin numbers
const int inputLED_a = 9;
const int inputLED_b = 10;
const int inputLED_c = 11;
const int outputK = 14;
const int outputM = 16;
byte ledCycleCounter = 0;
// timing
int loopTime = 0;
int prevTime = 0;
int loopCounter = 0;
///////////////////////////
// FUNCTIONS //////////////
///////////////////////////
void initializeArduino();
void initializeInputs();
void updateMeasurementBuffers();
void updateBufferSums();
void updateBufferIndex();
void updateInputStates();
void sendMouseButtonEvents();
void sendMouseMovementEvents();
void addDelay();
void cycleLEDs();
void danceLeds();
void updateOutLEDs();
//////////////////////
// SETUP /////////////
//////////////////////
void setup()
{
initializeArduino();
initializeInputs();
danceLeds();
}
////////////////////
// MAIN LOOP ///////
////////////////////
void loop()
{
updateMeasurementBuffers();
updateBufferSums();
updateBufferIndex();
updateInputStates();
sendMouseButtonEvents();
sendMouseMovementEvents();
cycleLEDs();
updateOutLEDs();
addDelay();
}
//////////////////////////
// INITIALIZE ARDUINO
//////////////////////////
void initializeArduino() {
#ifdef DEBUG
Serial.begin(9600); // Serial for debugging
#endif
/* Set up input pins
DEactivate the internal pull-ups, since we're using external resistors */
for (int i=0; i<NUM_INPUTS; i++)
{
pinMode(pinNumbers[i], INPUT);
digitalWrite(pinNumbers[i], LOW);
}
pinMode(inputLED_a, INPUT);
pinMode(inputLED_b, INPUT);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_a, LOW);
digitalWrite(inputLED_b, LOW);
digitalWrite(inputLED_c, LOW);
pinMode(outputK, OUTPUT);
pinMode(outputM, OUTPUT);
digitalWrite(outputK, LOW);
digitalWrite(outputM, LOW);
#ifdef DEBUG
delay(4000); // allow us time to reprogram in case things are freaking out
#endif
Keyboard.begin();
Mouse.begin();
}
///////////////////////////
// INITIALIZE INPUTS
///////////////////////////
void initializeInputs() {
float thresholdPerc = SWITCH_THRESHOLD_OFFSET_PERC;
float thresholdCenterBias = SWITCH_THRESHOLD_CENTER_BIAS/50.0;
float pressThresholdAmount = (BUFFER_LENGTH * 8) * (thresholdPerc / 100.0);
float thresholdCenter = ( (BUFFER_LENGTH * 8) / 2.0 ) * (thresholdCenterBias);
pressThreshold = int(thresholdCenter + pressThresholdAmount);
releaseThreshold = int(thresholdCenter - pressThresholdAmount);
for (int i=0; i<NUM_INPUTS; i++) {
inputs[i].pinNumber = pinNumbers[i];
//inputs[i].oldestMeasurement = 0;
//inputs[i].bufferSum = 0;
//inputs[i].pressed = false;
//inputs[i].prevPressed = false;
//inputs[i].isMouseMotion = false;
//inputs[i].isMouseButton = false;
for (int k=0; k<KEYS_PER_INPUT; k++) {
// Track all the key(s) that are to be triggered by the input 'i'.
int keyCode = keyCodesAndOptions[i][k];
inputs[i].keyCode[k] = keyCode;
// Classify the type of output now (whether mouse or keyboard, etc.)
// There may be a mix of mouse output and keyboard output, in which case several flags may become
// true for this one input.
if (keyCode < 0) {
inputs[i].isMouseMotion = true;
if (InputIsMouseButton(keyCode)) {
inputs[i].isMouseButton = true;
}
}
if (InputIsKeyboardKey(keyCode)) {
inputs[i].isKeyboardKey = true;
}
}
inputs[i].isReverseInput = keyCodesAndOptions[i][3] > 0;
inputs[i].isCapacitive = keyCodesAndOptions[i][4] > 0;
for (int j=0; j<BUFFER_LENGTH; j++) {
inputs[i].measurementBuffer[j] = 0;
}
}
}
//////////////////////////////
// UPDATE MEASUREMENT BUFFERS
//////////////////////////////
void updateMeasurementBuffers() {
for (int i=0; i<NUM_INPUTS; i++) {
// store the oldest measurement, which is the one at the current index,
// before we update it to the new one
// we use oldest measurement in updateBufferSums
byte currentByte = inputs[i].measurementBuffer[byteCounter];
inputs[i].oldestMeasurement = (currentByte >> bitCounter) & 0x01;
boolean newMeasurement;
if (!inputs[i].isCapacitive) {
// Make the new measurement, by reading the input directly.
newMeasurement = digitalRead(inputs[i].pinNumber);
} else {
// Make the new measurement using the capacitive sensing trick.
uint8_t res = readCapacitivePin(inputs[i].pinNumber);
newMeasurement = res > 1 ? LOW : HIGH;
}
// invert so that true means the switch is closed
newMeasurement = !newMeasurement;
// store it
if (newMeasurement) {
currentByte |= (1<<bitCounter);
}
else {
currentByte &= ~(1<<bitCounter);
}
inputs[i].measurementBuffer[byteCounter] = currentByte;
}
}
///////////////////////////
// UPDATE BUFFER SUMS
///////////////////////////
void updateBufferSums() {
// the bufferSum is a running tally of the entire measurementBuffer
// add the new measurement and subtract the old one
for (int i=0; i<NUM_INPUTS; i++) {
byte currentByte = inputs[i].measurementBuffer[byteCounter];
boolean currentMeasurement = (currentByte >> bitCounter) & 0x01;
if (currentMeasurement) {
inputs[i].bufferSum++;
}
if (inputs[i].oldestMeasurement) {
inputs[i].bufferSum--;
}
}
}
///////////////////////////
// UPDATE BUFFER INDEX
///////////////////////////
void updateBufferIndex() {
bitCounter++;
if (bitCounter == 8) {
bitCounter = 0;
byteCounter++;
if (byteCounter == BUFFER_LENGTH) {
byteCounter = 0;
}
}
}
int InputIsMouseButton(int inputCode) {
return inputCode == MOUSE_LEFT || inputCode == MOUSE_RIGHT;
}
int InputIsKeyboardKey(int inputCode) {
return inputCode > 0 && inputCode != MOUSE_LEFT && inputCode != MOUSE_RIGHT;
}
void PressKeyboard(int inputIndex) {
MakeyMakeyInput input = inputs[inputIndex];
for (int k=0; k<KEYS_PER_INPUT; k++)
{
int keyCode = input.keyCode[k];
if (InputIsKeyboardKey(keyCode))
{
if (!input.isReverseInput) {
Keyboard.press(keyCode);
} else {
Keyboard.release(keyCode);
}
}
}
}
void ReleaseKeyboard(int inputIndex)
{
MakeyMakeyInput input = inputs[inputIndex];
for (int k=0; k<KEYS_PER_INPUT; k++)
{
int keyCode = input.keyCode[k];
if (InputIsKeyboardKey(keyCode))
{
if (!input.isReverseInput) {
Keyboard.release(keyCode);
} else {
Keyboard.press(keyCode);
}
}
}
}
///////////////////////////
// UPDATE INPUT STATES
///////////////////////////
void updateInputStates() {
for (int i=0; i<NUM_INPUTS; i++) {
inputs[i].prevPressed = inputs[i].pressed; // store previous pressed state (only used for mouse buttons)
if (inputs[i].pressed) {
if (inputs[i].bufferSum < releaseThreshold) {
if (inputs[i].isKeyboardKey) {
inputs[i].pressed = false;
ReleaseKeyboard(i);
}
if (inputs[i].isMouseMotion) {
mouseHoldCount[i] = 0; // input becomes released, reset mouse hold
}
}
else if (inputs[i].isMouseMotion) {
mouseHoldCount[i]++; // input remains pressed, increment mouse hold
}
}
else if (!inputs[i].pressed) {
if (inputs[i].bufferSum > pressThreshold) { // input becomes pressed
if (inputs[i].isKeyboardKey) {
inputs[i].pressed = true;
PressKeyboard(i);
}
}
}
}
}
/*
///////////////////////////
// SEND KEY EVENTS (obsolete, used in versions with pro micro bootloader)
///////////////////////////
void sendKeyEvents() {
if (inputChanged) {
KeyReport report = {
0 };
for (int i=0; i<6; i++) {
report.keys[i] = 0;
}
int count = 0;
for (int i=0; i<NUM_INPUTS; i++) {
if (inputs[i].pressed && (count < 6)) {
report.keys[count] = inputs[i].keyCode;
#ifdef DEBUG3
Serial.println(report.keys[count]);
#endif
count++;
}
}
if (count > 0) {
report.modifiers = 0x00;
report.reserved = 1;
Keyboard.sendReport(&report);
}
else {
report.modifiers = 0x00;
report.reserved = 0;
Keyboard.sendReport(&report);
}
}
else {
// might need a delay here to compensate for the time it takes to send keyreport
}
}
*/
/////////////////////////////
// SEND MOUSE BUTTON EVENTS
/////////////////////////////
void sendMouseButtonEvents() {
if (inputChanged) {
for (int i=0; i<NUM_INPUTS; i++) {
if (inputs[i].isMouseButton) {
if (inputs[i].pressed) {
for (int k=0; k<KEYS_PER_INPUT; k++)
{
int keyCode = inputs[i].keyCode[k];
if (InputIsMouseButton(keyCode)) {
Mouse.press(keyCode);
}
}
}
else if (inputs[i].prevPressed) {
for (int k=0; k<KEYS_PER_INPUT; k++)
{
int keyCode = inputs[i].keyCode[k];
if (InputIsMouseButton(keyCode)) {
Mouse.release(keyCode);
}
}
}
}
}
}
}
//////////////////////////////
// SEND MOUSE MOVEMENT EVENTS
//////////////////////////////
void sendMouseMovementEvents() {
byte right = 0;
byte left = 0;
byte down = 0;
byte up = 0;
byte horizmotion = 0;
byte vertmotion = 0;
mouseMovementCounter++;
mouseMovementCounter %= MOUSE_MOTION_UPDATE_INTERVAL;
if (mouseMovementCounter == 0) {
for (int i=0; i<NUM_INPUTS; i++) {
#ifdef DEBUG_MOUSE
// Serial.println(inputs[i].isMouseMotion);
#endif
if (inputs[i].isMouseMotion) {
if (inputs[i].pressed) {
for (int k=0; k<KEYS_PER_INPUT; k++) {
int keyCode = inputs[i].keyCode[k];
// Update to 1 + a hold factor, constrained between 1 and mouse max movement speed
if (keyCode == MOUSE_MOVE_UP) {
up = constrain(1+mouseHoldCount[i]/MOUSE_RAMP_SCALE, 1, MOUSE_MAX_PIXELS);
}
if (keyCode == MOUSE_MOVE_DOWN) {
down = constrain(1+mouseHoldCount[i]/MOUSE_RAMP_SCALE, 1, MOUSE_MAX_PIXELS);
}
if (keyCode == MOUSE_MOVE_LEFT) {
left = constrain(1+mouseHoldCount[i]/MOUSE_RAMP_SCALE, 1, MOUSE_MAX_PIXELS);
}
if (keyCode == MOUSE_MOVE_RIGHT) {
right = constrain(1+mouseHoldCount[i]/MOUSE_RAMP_SCALE, 1, MOUSE_MAX_PIXELS);
}
}
}
}
}
// diagonal scrolling and left/right cancellation
if(left > 0)
{
if(right > 0)
{
horizmotion = 0; // cancel horizontal motion because left and right are both pushed
}
else
{
horizmotion = -left; // left yes, right no
}
}
else
{
if(right > 0)
{
horizmotion = right; // right yes, left no
}
}
if(down > 0)
{
if(up > 0)
{
vertmotion = 0; // cancel vertical motion because up and down are both pushed
}
else
{
vertmotion = down; // down yes, up no
}
}
else
{
if (up > 0)
{
vertmotion = -up; // up yes, down no
}
}
// now move the mouse
if( !((horizmotion == 0) && (vertmotion==0)) )
{
Mouse.move(horizmotion * PIXELS_PER_MOUSE_STEP, vertmotion * PIXELS_PER_MOUSE_STEP);
}
}
}
///////////////////////////
// ADD DELAY
///////////////////////////
void addDelay() {
loopTime = micros() - prevTime;
if (loopTime < TARGET_LOOP_TIME) {
int wait = TARGET_LOOP_TIME - loopTime;
delayMicroseconds(wait);
}
prevTime = micros();
}
///////////////////////////
// CYCLE LEDS
///////////////////////////
void cycleLEDs() {
pinMode(inputLED_a, INPUT);
pinMode(inputLED_b, INPUT);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_a, LOW);
digitalWrite(inputLED_b, LOW);
digitalWrite(inputLED_c, LOW);
ledCycleCounter++;
ledCycleCounter %= 6;
if ((ledCycleCounter == 0) && inputs[0].pressed) {
pinMode(inputLED_a, INPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, HIGH);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, LOW);
}
if ((ledCycleCounter == 1) && inputs[1].pressed) {
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, HIGH);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_c, LOW);
}
if ((ledCycleCounter == 2) && inputs[2].pressed) {
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, HIGH);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_c, LOW);
}
if ((ledCycleCounter == 3) && inputs[3].pressed) {
pinMode(inputLED_a, INPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, HIGH);
}
if ((ledCycleCounter == 4) && inputs[4].pressed) {
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, INPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, HIGH);
}
if ((ledCycleCounter == 5) && inputs[5].pressed) {
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, HIGH);
pinMode(inputLED_b, INPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, LOW);
}
}
///////////////////////////
// DANCE LEDS
///////////////////////////
void danceLeds()
{
int delayTime = 50;
int delayTime2 = 100;
// CIRCLE
for(int i=0; i<4; i++)
{
// UP
pinMode(inputLED_a, INPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, HIGH);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, LOW);
delay(delayTime);
// RIGHT
pinMode(inputLED_a, INPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, HIGH);
delay(delayTime);
// DOWN
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, HIGH);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_c, LOW);
delay(delayTime);
// LEFT
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, OUTPUT);
digitalWrite(inputLED_b, HIGH);
pinMode(inputLED_c, INPUT);
digitalWrite(inputLED_c, LOW);
delay(delayTime);
}
// WIGGLE
for(int i=0; i<4; i++)
{
// SPACE
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, HIGH);
pinMode(inputLED_b, INPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, LOW);
delay(delayTime2);
// CLICK
pinMode(inputLED_a, OUTPUT);
digitalWrite(inputLED_a, LOW);
pinMode(inputLED_b, INPUT);
digitalWrite(inputLED_b, LOW);
pinMode(inputLED_c, OUTPUT);
digitalWrite(inputLED_c, HIGH);
delay(delayTime2);
}
}
void updateOutLEDs()
{
boolean keyPressed = 0;
boolean mousePressed = 0;
for (int i=0; i<NUM_INPUTS; i++)
{
if (inputs[i].pressed)
{
if (inputs[i].isKeyboardKey)
{
keyPressed = 1;
#ifdef DEBUG
Serial.print("Key ");
Serial.print(i);
Serial.println(" pressed");
#endif
}
else
{
mousePressed = 1;
}
}
}
if (keyPressed)
{
digitalWrite(outputK, HIGH);
TXLED1;
}
else
{
digitalWrite(outputK, LOW);
TXLED0;
}
if (mousePressed)
{
digitalWrite(outputM, HIGH);
RXLED1;
}
else
{
digitalWrite(outputM, LOW);
RXLED0;
}
}
// Code by Arvid Jense 22/10/2012 at the Music As Material
// Workshop by Eric Rosenbaum and Micheal Smith-Welch in Arnhem
// This code has been removed but the credit is left!
//
// CapacitiveSensor tutorial from arduino http://www.arduino.cc/playground/Code/CapacitiveSensor
// readCapacitivePin
// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher
uint8_t readCapacitivePin(int pinToMeasure) {
// Variables used to translate from Arduino to AVR pin naming
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
byte bitmask;
port = portOutputRegister(digitalPinToPort(pinToMeasure));
ddr = portModeRegister(digitalPinToPort(pinToMeasure));
bitmask = digitalPinToBitMask(pinToMeasure);
pin = portInputRegister(digitalPinToPort(pinToMeasure));
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Make the pin an input with the internal pull-up on
*ddr &= ~(bitmask);
*port |= bitmask;
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) { cycles = 0;}
else if (*pin & bitmask) { cycles = 1;}
else if (*pin & bitmask) { cycles = 2;}
// Removed extra cycle checks through 16 to speed it up; don't need that precision. -DavidRieman
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}