Skip to content

Commit b59dae0

Browse files
committed
adding a classic
1 parent 33dfdb6 commit b59dae0

File tree

6 files changed

+470
-0
lines changed

6 files changed

+470
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Circuit Playground Capacitive Touch Basic
3+
//
4+
// Print text messages for each touch pad.
5+
//
6+
// Author: Carter Nelson
7+
// MIT License (https://opensource.org/licenses/MIT)
8+
9+
#include <Adafruit_CircuitPlayground.h>
10+
11+
#define CAP_THRESHOLD 50
12+
#define DEBOUNCE 250
13+
14+
uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
15+
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
16+
17+
////////////////////////////////////////////////////////////////////////////
18+
void takeAction(uint8_t pad) {
19+
Serial.print("PAD "); Serial.print(pad); Serial.print(" says: ");
20+
switch (pad) {
21+
case 3:
22+
Serial.println("The goggles! They do nothing!");
23+
break;
24+
case 2:
25+
Serial.println("Dental plan!");
26+
break;
27+
case 0:
28+
Serial.println("Lisa needs braces.");
29+
break;
30+
case 1:
31+
Serial.println("I call the large one Bitey.");
32+
break;
33+
case 12:
34+
Serial.println("I'm Idaho!");
35+
break;
36+
case 6:
37+
Serial.println("Monorail!");
38+
break;
39+
case 9:
40+
Serial.println("I choo-choo choose you.");
41+
break;
42+
case 10:
43+
Serial.println("Maaaattlllock!");
44+
break;
45+
default:
46+
Serial.println("THIS SHOULD NEVER HAPPEN.");
47+
}
48+
}
49+
50+
////////////////////////////////////////////////////////////////////////////
51+
boolean capButton(uint8_t pad) {
52+
// Check if capacitive touch exceeds threshold.
53+
if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
54+
return true;
55+
} else {
56+
return false;
57+
}
58+
}
59+
60+
////////////////////////////////////////////////////////////////////////////
61+
void setup() {
62+
// Initialize serial.
63+
Serial.begin(9600);
64+
65+
// Initialize Circuit Playground library.
66+
CircuitPlayground.begin();
67+
}
68+
69+
////////////////////////////////////////////////////////////////////////////
70+
void loop() {
71+
// Loop over every pad.
72+
for (int i=0; i<numberOfPads; i++) {
73+
74+
// Check if pad is touched.
75+
if (capButton(pads[i])) {
76+
77+
// Do something.
78+
takeAction(pads[i]);
79+
80+
// But not too often.
81+
delay(DEBOUNCE);
82+
}
83+
}
84+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Circuit Playground Capacitive Touch Tones
3+
//
4+
// Send keyboard and mouse events for each touch pad.
5+
// https://www.arduino.cc/en/Reference/MouseKeyboard
6+
//
7+
// Author: Carter Nelson
8+
// MIT License (https://opensource.org/licenses/MIT)
9+
10+
#include <Adafruit_CircuitPlayground.h>
11+
#include <Mouse.h>
12+
#include <Keyboard.h>
13+
14+
#define CAP_THRESHOLD 50
15+
#define DEBOUNCE 250
16+
17+
uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
18+
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
19+
20+
boolean emulatorActive = false;
21+
22+
////////////////////////////////////////////////////////////////////////////
23+
void takeAction(uint8_t pad) {
24+
Serial.print("PAD "); Serial.println(pad);
25+
switch (pad) {
26+
case 3:
27+
sendKey(KEY_LEFT_ARROW);
28+
break;
29+
case 2:
30+
sendKey(KEY_UP_ARROW);
31+
break;
32+
case 0:
33+
sendKey(KEY_DOWN_ARROW);
34+
break;
35+
case 1:
36+
sendKey(KEY_RIGHT_ARROW);
37+
break;
38+
case 12:
39+
sendKey(' ');
40+
break;
41+
case 6:
42+
sendMouse(MOUSE_LEFT);
43+
break;
44+
case 9:
45+
sendMouse(MOUSE_MIDDLE);
46+
break;
47+
case 10:
48+
sendMouse(MOUSE_RIGHT);
49+
break;
50+
default:
51+
Serial.println("THIS SHOULD NEVER HAPPEN.");
52+
}
53+
}
54+
55+
////////////////////////////////////////////////////////////////////////////
56+
boolean capButton(uint8_t pad) {
57+
// Check if capacitive touch exceeds threshold.
58+
if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
59+
return true;
60+
} else {
61+
return false;
62+
}
63+
}
64+
65+
////////////////////////////////////////////////////////////////////////////
66+
void sendKey(char key) {
67+
Keyboard.press(key);
68+
Keyboard.releaseAll();
69+
}
70+
71+
////////////////////////////////////////////////////////////////////////////
72+
void sendMouse(char key) {
73+
Mouse.click(key);
74+
}
75+
76+
////////////////////////////////////////////////////////////////////////////
77+
void setup() {
78+
// Initialize serial.
79+
Serial.begin(9600);
80+
81+
// Initialize Circuit Playground library.
82+
CircuitPlayground.begin();
83+
}
84+
85+
////////////////////////////////////////////////////////////////////////////
86+
void loop() {
87+
// Indicate emulator status on red LED.
88+
CircuitPlayground.redLED(emulatorActive);
89+
90+
// Check slide switch.
91+
if (!CircuitPlayground.slideSwitch()) {
92+
93+
//-----------| EMULATOR OFF |-------------
94+
if (emulatorActive) {
95+
Keyboard.end();
96+
Mouse.end();
97+
emulatorActive = false;
98+
}
99+
100+
} else {
101+
102+
//-----------| EMULATOR ON |-------------
103+
if (!emulatorActive) {
104+
Keyboard.begin();
105+
Mouse.begin();
106+
emulatorActive = true;
107+
}
108+
109+
// Loop over every pad.
110+
for (int i=0; i<numberOfPads; i++) {
111+
112+
// Check if pad is touched.
113+
if (capButton(pads[i])) {
114+
115+
// Do something.
116+
takeAction(pads[i]);
117+
118+
// But not too often.
119+
delay(DEBOUNCE);
120+
}
121+
}
122+
}
123+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
// Circuit Playground Capacitive Touch USB MIDI Drums
3+
//
4+
// Send MIDI percussion commands over USB.
5+
// https://www.arduino.cc/en/Reference/MIDIUSB
6+
// https://en.wikipedia.org/wiki/General_MIDI#Percussion
7+
//
8+
// Author: Carter Nelson
9+
// MIT License (https://opensource.org/licenses/MIT)
10+
11+
#include <Adafruit_CircuitPlayground.h>
12+
#include "MIDIUSB.h"
13+
14+
#define CAP_THRESHOLD 50
15+
#define DEBOUNCE 100
16+
17+
uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
18+
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
19+
20+
////////////////////////////////////////////////////////////////////////////
21+
void takeAction(uint8_t pad) {
22+
Serial.print("PAD "); Serial.print(pad); Serial.print(". Sending MIDI: ");
23+
switch (pad) {
24+
case 3:
25+
Serial.println("36");
26+
drumHit(36);
27+
break;
28+
case 2:
29+
Serial.println("37");
30+
drumHit(37);
31+
break;
32+
case 0:
33+
Serial.println("38");
34+
drumHit(38);
35+
break;
36+
case 1:
37+
Serial.println("39");
38+
drumHit(39);
39+
break;
40+
case 12:
41+
Serial.println("40");
42+
drumHit(40);
43+
break;
44+
case 6:
45+
Serial.println("41");
46+
drumHit(41);
47+
break;
48+
case 9:
49+
Serial.println("42");
50+
drumHit(42);
51+
break;
52+
case 10:
53+
Serial.println("43");
54+
drumHit(43);
55+
break;
56+
default:
57+
Serial.println("THIS SHOULD NEVER HAPPEN.");
58+
}
59+
}
60+
61+
////////////////////////////////////////////////////////////////////////////
62+
boolean capButton(uint8_t pad) {
63+
// Check if capacitive touch exceeds threshold.
64+
if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
65+
return true;
66+
} else {
67+
return false;
68+
}
69+
}
70+
71+
////////////////////////////////////////////////////////////////////////////
72+
void noteOn(byte channel, byte pitch, byte velocity) {
73+
// Send a MIDI Note On command.
74+
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
75+
MidiUSB.sendMIDI(noteOn);
76+
}
77+
78+
////////////////////////////////////////////////////////////////////////////
79+
void noteOff(byte channel, byte pitch, byte velocity) {
80+
// Send a MIDI Note Off command.
81+
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
82+
MidiUSB.sendMIDI(noteOff);
83+
}
84+
85+
////////////////////////////////////////////////////////////////////////////
86+
void drumHit(byte drum) {
87+
// Send Note On/Off command for given drum (note) number.
88+
noteOn(9, drum, 125);
89+
MidiUSB.flush();
90+
noteOff(9, drum, 0);
91+
MidiUSB.flush();
92+
}
93+
94+
////////////////////////////////////////////////////////////////////////////
95+
void setup() {
96+
// Initialize serial.
97+
Serial.begin(9600);
98+
99+
// Initialize Circuit Playground library.
100+
CircuitPlayground.begin();
101+
}
102+
103+
////////////////////////////////////////////////////////////////////////////
104+
void loop() {
105+
// Loop over every pad.
106+
for (int i=0; i<numberOfPads; i++) {
107+
108+
// Check if pad is touched.
109+
if (capButton(pads[i])) {
110+
111+
// Do something.
112+
takeAction(pads[i]);
113+
114+
// But not too often.
115+
delay(DEBOUNCE);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)