Cleaned the code, receiving clock from MIDI

Commented, removed many #define and many general code improvements. Arpeggiator waits for MIDI clock.
This commit is contained in:
əlemi 2019-03-10 02:33:40 +01:00 committed by GitHub
parent 4f6486bc75
commit b89ce7c72a

View file

@ -1,50 +1,50 @@
#define C 22
#define Db 24
#define D 26
#define Eb 28
#define E 30
#define F 32
#define Gb 34
#define G 36
#define Ab 38
#define A 40
#define Bb 42
#define B 44
#define testLed 13
#define Oct1 12
#define Oct2 9
#define Oct3 8
#define Oct4 10
#define noteOffset 36 #define noteOffset 36
#define DRUMNOTE 60
#define MINUTE 60000 #define MINUTE 60000
#define MIDICLOCK 0xf8
#include <CapacitiveSensor.h>
#include <MIDI.h> #include <MIDI.h>
#include <HID.h> #include <HID.h>
MIDI_CREATE_DEFAULT_INSTANCE(); MIDI_CREATE_DEFAULT_INSTANCE();
typedef struct OctaveStatus { typedef struct OctaveStatus { // This struct is for an octave status. Each bool is for 1 note
bool stat[12]; bool stat[12];
int nOct; int nOct;
} octst; } octst;
int note[12] = {
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B }; // Note Pins above
int octave[4] = {
Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above
int clock = 0; // Used if arp to cycle through notes // PIN DECLARATIONS
octst buff; int note[12] = { // Pins used to read each note (C is 0, B is 11)
bool kboard[49]; 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 };
bool raw; // Global Settings. RAW = signal is sent when key is detected int octave[4] = { // Pins associated to each octave's contact bar
byte velocity = 100; 12, 9, 8, 10 };
byte channel = 1; int sendPin[3] = { // Pins used as sender for capacitive touch buttons
int bpm = 360; 5, 4, 16 };
unsigned long nextBeat = 0; int receivePin[3] = { // Pins used as receiver for capacitive touch buttons
unsigned long gate = 50; //ms of keypress if arpeggiator 6, 3, 17 };
int npressed;
// GLOBAL SETTINGS
bool raw; // Signal is sent when key is detected
// PLACEHOLDERS
byte velocity = 100; //
byte channel = 1; //
int bpm = 360; //
unsigned long gate = 50; // ms of keypress if arpeggiator
unsigned long nextBeat = 0; // Used to keep track of beats. Useless if receiving MIDI clock.
// SYSTEM VARIABLES
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
bool kboard[49]; // Last status of keyboard
bool bCapStat[3]; // Last status of Capacitive Buttons
CapacitiveSensor* bCap[3];
void setup() { void setup() {
for (int cOctave = 0; cOctave < 4; cOctave++) { for (int cOctave = 0; cOctave < 4; cOctave++) {
@ -53,17 +53,27 @@ void setup() {
for (int cNote = 0; cNote < 12; cNote++) { for (int cNote = 0; cNote < 12; cNote++) {
pinMode(note[cNote], INPUT); pinMode(note[cNote], INPUT);
} }
for (int cButton = 0; cButton < 3; cButton++) { // Capacitive Buttons configuration
bCap[cButton] = new CapacitiveSensor(sendPin[cButton], receivePin[cButton]); // Initialized
bCap[cButton]->set_CS_AutocaL_Millis(0xFFFFFFFF); // No recalibration
bCap[cButton]->set_CS_Timeout_Millis(200); // Timeout set to 200ms (instead of 2s)
bCapStat[cButton] = LOW; // Button starts LOW
}
for (int cStat = 0; cStat < 49; cStat++) kboard[cStat] = LOW; // All keyboard keys start LOW
MIDI.begin(MIDI_CHANNEL_OFF); MIDI.begin(MIDI_CHANNEL_OFF);
Serial.begin(115200); Serial.begin(115200);
nextBeat = millis() + (MINUTE / bpm); pinMode(2, INPUT_PULLUP); // Used for RAW switch
pinMode(2, INPUT_PULLUP);
for (int cStat = 0; cStat < 49; cStat++) kboard[cStat] = LOW;
} }
void loop() { void loop() {
sync();
for (int cButton = 0; cButton < 3; 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++) {
@ -71,27 +81,27 @@ void loop() {
npressed += eval(scan(cOctave)); npressed += eval(scan(cOctave));
digitalWrite(octave[cOctave], LOW); digitalWrite(octave[cOctave], LOW);
} }
if (raw) { if (raw) return;
nextBeat = millis();
return;
}
if (npressed < 1) return; if (npressed < 1) return;
if (millis() >= nextBeat) {
nextBeat += (MINUTE / bpm); if (semA > 0) {
clock++; semA--;
while (kboard[clock] == LOW) { arp++;
clock++; while (kboard[arp] == LOW) {
if (clock == 49) clock = 0; arp++;
if (arp == 49) arp = 0;
} }
playNote(clock, HIGH); playNote(arp, HIGH);
delay(gate); }
playNote(clock, LOW); if (semB > 0) {
semB--;
playNote(arp, LOW);
} }
} }
octst scan(int nOct) { // This function reads the 12 note pins and returns a struct octst scan(int nOct) { // This function reads the 12 note pins and returns a struct
int c; // with 1 bool for each note int c; // with 1 bool for each note
octst output; octst output;
output.nOct = nOct; output.nOct = nOct;
@ -125,3 +135,31 @@ void playNote(int c, bool status) {
MIDI.sendNoteOff(n, velocity, channel); MIDI.sendNoteOff(n, velocity, channel);
} }
} }
bool evalButton(CapacitiveSensor* b, bool value, byte note) {
long sensor = b->capacitiveSensor(1);
if (sensor > 15) {
if (value) return HIGH;
else {
MIDI.sendNoteOn(note, velocity, (byte)7);
return HIGH;
}
}
else {
if (!value) return LOW;
else {
MIDI.sendNoteOff(note, velocity, (byte)7);
return LOW;
}
}
}
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;
}
}