mirror of
https://git.alemi.dev/cv-keyboard.git
synced 2024-11-24 01:14:48 +01:00
Now keyboard waits for MIDI clock and plays 2 notes per beat when in arpeggiator
This commit is contained in:
parent
96a4142c0f
commit
5bd20dab8c
1 changed files with 54 additions and 37 deletions
|
@ -15,7 +15,7 @@ typedef struct OctaveStatus { // This struct is for an octave status. Each boo
|
||||||
} octst;
|
} octst;
|
||||||
|
|
||||||
|
|
||||||
// PIN DECLARATIONS
|
// PIN DECLARATIONS
|
||||||
int note[12] = { // Pins used to read each note (C is 0, B is 11)
|
int note[12] = { // Pins used to read each note (C is 0, B is 11)
|
||||||
22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 };
|
22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 };
|
||||||
int octave[4] = { // Pins associated to each octave's contact bar
|
int octave[4] = { // Pins associated to each octave's contact bar
|
||||||
|
@ -25,7 +25,7 @@ int sendPin[3] = { // Pins used as sender for capacitive touch buttons
|
||||||
int receivePin[3] = { // Pins used as receiver for capacitive touch buttons
|
int receivePin[3] = { // Pins used as receiver for capacitive touch buttons
|
||||||
6, 3, 17 };
|
6, 3, 17 };
|
||||||
|
|
||||||
// GLOBAL SETTINGS
|
// GLOBAL SETTINGS
|
||||||
bool raw; // Signal is sent when key is detected
|
bool raw; // Signal is sent when key is detected
|
||||||
|
|
||||||
// PLACEHOLDERS
|
// PLACEHOLDERS
|
||||||
|
@ -36,7 +36,10 @@ unsigned long gate = 50; // ms of keypress if arpeggiator
|
||||||
unsigned long nextBeat = 0; // Used to keep track of beats. Useless if receiving MIDI clock.
|
unsigned long nextBeat = 0; // Used to keep track of beats. Useless if receiving MIDI clock.
|
||||||
|
|
||||||
// SYSTEM VARIABLES
|
// SYSTEM VARIABLES
|
||||||
int clock = 0; // Used if arp to cycle through notes
|
int arp = 0; // Keeps track of last played note if arpeggiating
|
||||||
|
int midiclock = 0; // Used to sync with MIDI clock
|
||||||
|
int semA = 0; // Basic semaphore implementation with global counter
|
||||||
|
int semB = 0;
|
||||||
int npressed; // Number of keys pressed, used to avoid doing anything when no keys are pressed
|
int npressed; // Number of keys pressed, used to avoid doing anything when no keys are pressed
|
||||||
bool kboard[49]; // Last status of keyboard
|
bool kboard[49]; // Last status of keyboard
|
||||||
bool bCapStat[3]; // Last status of Capacitive Buttons
|
bool bCapStat[3]; // Last status of Capacitive Buttons
|
||||||
|
@ -66,10 +69,11 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
sync();
|
||||||
|
|
||||||
for (int cButton = 0; cButton < 3; cButton++) {
|
for (int cButton = 0; cButton < 3; cButton++) {
|
||||||
bCapStat[cButton] = evalButton(bCap[cButton], bCapStat[cButton], DRUMNOTE + cButton);
|
bCapStat[cButton] = evalButton(bCap[cButton], bCapStat[cButton], DRUMNOTE + cButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
npressed = 0;
|
npressed = 0;
|
||||||
raw = digitalRead(2);
|
raw = digitalRead(2);
|
||||||
for (int cOctave = 0; cOctave < 4; cOctave++) {
|
for (int cOctave = 0; cOctave < 4; cOctave++) {
|
||||||
|
@ -79,15 +83,19 @@ void loop() {
|
||||||
}
|
}
|
||||||
if (raw) return;
|
if (raw) return;
|
||||||
if (npressed < 1) return;
|
if (npressed < 1) return;
|
||||||
if (Serial.read() == MIDICLOCK) {
|
|
||||||
clock++;
|
if (semA > 0) {
|
||||||
while (kboard[clock] == LOW) {
|
semA--;
|
||||||
clock++;
|
arp++;
|
||||||
if (clock == 49) clock = 0;
|
while (kboard[arp] == LOW) {
|
||||||
|
arp++;
|
||||||
|
if (arp == 49) arp = 0;
|
||||||
}
|
}
|
||||||
playNote(clock, HIGH);
|
playNote(arp, HIGH);
|
||||||
delay(gate);
|
}
|
||||||
playNote(clock, LOW);
|
if (semB > 0) {
|
||||||
|
semB--;
|
||||||
|
playNote(arp, LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,3 +154,12 @@ bool evalButton(CapacitiveSensor* b, bool value, byte note) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sync() {
|
||||||
|
if (Serial.available() && Serial.read() == MIDICLOCK) {
|
||||||
|
midiclock++;
|
||||||
|
if (midiclock == 11 && semA == 0) semA++;
|
||||||
|
else if (midiclock == 5 && semB == 0) semB++;
|
||||||
|
else if (midiclock == 12) midiclock = 0;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue