2018-10-31 17:18:50 +01:00
|
|
|
#define noteOffset 36
|
2019-03-09 15:23:18 +01:00
|
|
|
#define DRUMNOTE 60
|
2019-03-08 21:10:15 +01:00
|
|
|
#define MINUTE 60000
|
2019-03-09 15:23:18 +01:00
|
|
|
#define MIDICLOCK 0xf8
|
2018-10-31 17:18:50 +01:00
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
#include <CapacitiveSensor.h>
|
|
|
|
#include <MIDI.h>
|
|
|
|
#include <HID.h>
|
|
|
|
|
|
|
|
MIDI_CREATE_DEFAULT_INSTANCE();
|
2019-03-04 18:54:22 +01:00
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
typedef struct SequencerStep* link;
|
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
typedef struct OctaveStatus { // This struct is for an octave status. Each bool is for 1 note
|
2019-03-04 18:54:22 +01:00
|
|
|
bool stat[12];
|
|
|
|
int nOct;
|
|
|
|
} octst;
|
2018-10-31 17:18:50 +01:00
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
typedef struct SequencerStep {
|
|
|
|
int note;
|
|
|
|
link next;
|
|
|
|
} step;
|
2019-03-09 15:23:18 +01:00
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
// PIN DECLARATIONS
|
|
|
|
int note[12] = { // Pins used to read each note (C is 0, B is 11)
|
2019-03-09 15:23:18 +01:00
|
|
|
22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 };
|
2019-03-09 19:14:18 +01:00
|
|
|
int octave[4] = { // Pins associated to each octave's contact bar
|
2019-03-09 15:23:18 +01:00
|
|
|
12, 9, 8, 10 };
|
2019-03-09 19:14:18 +01:00
|
|
|
int sendPin[3] = { // Pins used as sender for capacitive touch buttons
|
|
|
|
5, 4, 16 };
|
|
|
|
int receivePin[3] = { // Pins used as receiver for capacitive touch buttons
|
|
|
|
6, 3, 17 };
|
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
// GLOBAL SETTINGS
|
2019-03-09 19:14:18 +01:00
|
|
|
bool raw; // Signal is sent when key is detected
|
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
// PLACEHOLDERS
|
2019-03-09 19:14:18 +01:00
|
|
|
byte velocity = 100; //
|
|
|
|
byte channel = 1; //
|
|
|
|
int bpm = 360; //
|
|
|
|
unsigned long gate = 50; // ms of keypress if arpeggiator
|
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
// SEQUENCER POINTERS
|
|
|
|
link head, tail, current;
|
|
|
|
|
|
|
|
// SYSTEM VARIABLES
|
|
|
|
int nstep = 0; // Keeps track of the sequencer steps
|
2019-03-09 19:14:18 +01:00
|
|
|
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
|
2019-03-09 15:23:18 +01:00
|
|
|
CapacitiveSensor* bCap[3];
|
|
|
|
|
2018-11-11 01:11:49 +01:00
|
|
|
|
2019-03-04 18:54:22 +01:00
|
|
|
void setup() {
|
2018-10-31 17:18:50 +01:00
|
|
|
for (int cOctave = 0; cOctave < 4; cOctave++) {
|
|
|
|
pinMode(octave[cOctave], OUTPUT);
|
|
|
|
}
|
|
|
|
for (int cNote = 0; cNote < 12; cNote++) {
|
|
|
|
pinMode(note[cNote], INPUT);
|
|
|
|
}
|
2019-03-09 19:14:18 +01:00
|
|
|
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
|
2019-03-09 15:23:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
for (int cStat = 0; cStat < 49; cStat++) kboard[cStat] = LOW; // All keyboard keys start LOW
|
2019-03-09 15:23:18 +01:00
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
MIDI.begin(MIDI_CHANNEL_OFF);
|
2018-10-31 17:18:50 +01:00
|
|
|
Serial.begin(115200);
|
2019-03-09 15:23:18 +01:00
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
pinMode(2, INPUT_PULLUP); // Used for RAW switch
|
2018-11-11 02:00:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:54:22 +01:00
|
|
|
void loop() {
|
2019-03-09 19:14:18 +01:00
|
|
|
sync();
|
|
|
|
|
2019-03-09 15:23:18 +01:00
|
|
|
for (int cButton = 0; cButton < 3; cButton++) {
|
|
|
|
bCapStat[cButton] = evalButton(bCap[cButton], bCapStat[cButton], DRUMNOTE + cButton);
|
|
|
|
}
|
2019-03-08 21:10:15 +01:00
|
|
|
npressed = 0;
|
|
|
|
raw = digitalRead(2);
|
|
|
|
for (int cOctave = 0; cOctave < 4; cOctave++) {
|
|
|
|
digitalWrite(octave[cOctave], HIGH);
|
|
|
|
npressed += eval(scan(cOctave));
|
|
|
|
digitalWrite(octave[cOctave], LOW);
|
|
|
|
}
|
|
|
|
if (raw) return;
|
2019-03-09 19:14:18 +01:00
|
|
|
|
|
|
|
if (semA > 0) {
|
|
|
|
semA--;
|
2019-03-10 02:33:59 +01:00
|
|
|
|
|
|
|
if (bCapStat[1]) {
|
|
|
|
checkInsert();
|
|
|
|
}
|
|
|
|
else if (bCapStat[2] && npressed > 0) {
|
|
|
|
checkReplace();
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
2019-03-10 02:33:59 +01:00
|
|
|
|
|
|
|
if (current != NULL && current->note != -1) playNote(current->note, HIGH);
|
2019-03-09 19:14:18 +01:00
|
|
|
}
|
|
|
|
if (semB > 0) {
|
|
|
|
semB--;
|
2019-03-10 02:33:59 +01:00
|
|
|
if (bCapStat[0] && bCapStat[2]) {
|
|
|
|
deleteStep();
|
|
|
|
}
|
|
|
|
if (current != NULL && current->note != -1) playNote(current->note, LOW);
|
|
|
|
nextStep();
|
2018-11-11 02:00:08 +01:00
|
|
|
}
|
2018-10-31 17:21:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:56:20 +01:00
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
octst scan(int nOct) { // This function reads the 12 note pins and returns a struct
|
2019-03-09 15:23:18 +01:00
|
|
|
int c; // with 1 bool for each note
|
2019-03-04 18:54:22 +01:00
|
|
|
octst output;
|
2018-10-31 17:18:50 +01:00
|
|
|
|
2019-03-04 18:54:22 +01:00
|
|
|
output.nOct = nOct;
|
2018-11-11 01:11:49 +01:00
|
|
|
|
2019-03-04 18:54:22 +01:00
|
|
|
for (c = 0; c < 12; c++) {
|
|
|
|
output.stat[c] = digitalRead(note[c]);
|
2018-11-11 01:11:49 +01:00
|
|
|
}
|
2019-03-04 18:54:22 +01:00
|
|
|
return output;
|
2018-11-11 01:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
int eval(octst input) {
|
|
|
|
int pressed = 0;
|
|
|
|
int snote = input.nOct * 12;
|
|
|
|
|
|
|
|
for (int c = 0; c < 12; c++) {
|
|
|
|
if (input.stat[c] ^ kboard[c + snote]) {
|
|
|
|
if (raw) playNote(c + snote, input.stat[c]);
|
|
|
|
kboard[c + snote] = input.stat[c];
|
|
|
|
}
|
|
|
|
if (kboard[c + snote] == HIGH) pressed++;
|
2018-10-31 17:18:50 +01:00
|
|
|
}
|
2019-03-08 21:10:15 +01:00
|
|
|
return pressed;
|
2018-11-11 01:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
void playNote(int c, bool status) {
|
|
|
|
byte n = c + noteOffset;
|
|
|
|
if (status == HIGH) {
|
|
|
|
MIDI.sendNoteOn(n, velocity, channel);
|
|
|
|
}
|
|
|
|
else if (status == LOW) {
|
|
|
|
MIDI.sendNoteOff(n, velocity, channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:23:18 +01:00
|
|
|
bool evalButton(CapacitiveSensor* b, bool value, byte note) {
|
|
|
|
long sensor = b->capacitiveSensor(1);
|
2019-03-04 18:56:20 +01:00
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
if (sensor > 15) {
|
|
|
|
if (value) return HIGH;
|
|
|
|
else {
|
2019-03-09 15:23:18 +01:00
|
|
|
MIDI.sendNoteOn(note, velocity, (byte)7);
|
2019-03-08 21:10:15 +01:00
|
|
|
return HIGH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!value) return LOW;
|
|
|
|
else {
|
2019-03-09 15:23:18 +01:00
|
|
|
MIDI.sendNoteOff(note, velocity, (byte)7);
|
2019-03-08 21:10:15 +01:00
|
|
|
return LOW;
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 19:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-03-10 02:33:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
link newStep() {
|
|
|
|
return (link)malloc(sizeof(struct SequencerStep));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool insertStep(int note) {
|
|
|
|
link newS = newStep();
|
|
|
|
if (newS == NULL) {
|
|
|
|
free(newS);
|
|
|
|
return LOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
newS->note = note;
|
|
|
|
if (nstep == 0) {
|
|
|
|
newS->next = newS;
|
|
|
|
current = newS;
|
|
|
|
head = newS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newS->next = current->next;
|
|
|
|
current->next = newS;
|
|
|
|
}
|
|
|
|
nstep++;
|
|
|
|
return HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nextStep() {
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool deleteStep() {
|
|
|
|
if (nstep < 1) return LOW;
|
|
|
|
|
|
|
|
if (nstep == 1) {
|
|
|
|
free(current);
|
|
|
|
head = NULL;
|
|
|
|
current = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
link buffer = current->next->next;
|
|
|
|
free(current->next);
|
|
|
|
current->next = buffer;
|
|
|
|
}
|
|
|
|
nstep--;
|
|
|
|
return HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkInsert() {
|
|
|
|
if (npressed < 1) insertStep(-1);
|
|
|
|
else {
|
|
|
|
arp++;
|
|
|
|
while (!kboard[arp]) {
|
|
|
|
arp++;
|
|
|
|
if (arp == 49) arp = 0;
|
|
|
|
}
|
|
|
|
insertStep(arp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void checkReplace() {
|
|
|
|
arp++;
|
|
|
|
while (!kboard[arp]) {
|
|
|
|
arp++;
|
|
|
|
if (arp == 49) arp = 0;
|
|
|
|
}
|
|
|
|
current->note = arp;
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|