2019-06-27 18:58:37 +02:00
|
|
|
#define NOTEOffset 36
|
|
|
|
#define drumOffset 60
|
2019-03-08 21:10:15 +01:00
|
|
|
#define MINUTE 60000
|
2019-03-09 15:23:18 +01:00
|
|
|
#define MIDICLOCK 0xf8
|
2019-06-27 18:58:37 +02:00
|
|
|
#define MAXKEYS 48
|
|
|
|
#define MAXDPAD 3
|
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-06-27 18:58:37 +02: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 {
|
2019-06-27 18:58:37 +02:00
|
|
|
int NOTE;
|
|
|
|
bool kboard_s[MAXKEYS]
|
|
|
|
bool dpad_s[MAXDPAD]
|
2019-03-10 02:33:59 +01:00
|
|
|
link next;
|
|
|
|
} step;
|
2019-03-09 15:23:18 +01:00
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
// PIN DECLARATIONS
|
2019-06-27 18:58:37 +02:00
|
|
|
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-06-27 18:58:37 +02: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-06-27 18:58:37 +02:00
|
|
|
int SEND[3] = { // Pins used as sender for capacitive touch buttons
|
2019-03-09 19:14:18 +01:00
|
|
|
5, 4, 16 };
|
2019-06-27 18:58:37 +02:00
|
|
|
int RECEIVE[3] = { // Pins used as receiver for capacitive touch buttons
|
2019-03-09 19:14:18 +01:00
|
|
|
6, 3, 17 };
|
2019-06-27 18:58:37 +02:00
|
|
|
int OW = 2; // Pin used for overwrite switch
|
|
|
|
int DEL = -1; // Pin used for delete button
|
2019-06-28 00:14:44 +02:00
|
|
|
int ADD = 14; // Pin used for add button
|
2019-03-09 19:14:18 +01:00
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
// GLOBAL SETTINGS
|
2019-06-28 00:14:44 +02:00
|
|
|
//bool overwrite; // Step content is overwritten with pressed keys, could not be needed
|
2019-03-09 19:14:18 +01:00
|
|
|
|
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; //
|
2019-06-27 18:58:37 +02:00
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
|
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-06-27 18:58:37 +02:00
|
|
|
int arp = 0; // Keeps track of last played NOTE if arpeggiating
|
2019-03-09 19:14:18 +01:00
|
|
|
int midiclock = 0; // Used to sync with MIDI clock
|
2019-06-27 18:58:37 +02:00
|
|
|
int sem_beat = 0; // Basic semaphore used to sync with MIDI beat
|
|
|
|
int sem_gate = 0; // Basic semaphore used for gate timing
|
|
|
|
unsigned long last_gate = 0; // Gate start time for last sequencer step
|
|
|
|
unsigned long gate_length = 200; // ms of keypress if arpeggiator
|
|
|
|
bool dpadhit = LOW; // If any drum pad has been hit in this cycle, this is true
|
2019-03-09 19:14:18 +01:00
|
|
|
int npressed; // Number of keys pressed, used to avoid doing anything when no keys are pressed
|
2019-06-27 18:58:37 +02:00
|
|
|
bool kboard[MAXKEYS]; // Last status of keyboard
|
|
|
|
bool dpad[MAXDPAD]; // Last status of Capacitive Buttons
|
|
|
|
CapacitiveSensor* bCap[MAXDPAD];
|
2019-03-09 15:23:18 +01:00
|
|
|
|
2018-11-11 01:11:49 +01:00
|
|
|
|
2019-03-04 18:54:22 +01:00
|
|
|
void setup() {
|
2019-06-27 18:58:37 +02:00
|
|
|
for (int cOCTAVE = 0; cOCTAVE < 4; cOCTAVE++) {
|
|
|
|
pinMode(OCTAVE[cOCTAVE], OUTPUT);
|
2018-10-31 17:18:50 +01:00
|
|
|
}
|
2019-06-27 18:58:37 +02:00
|
|
|
for (int cNOTE = 0; cNOTE < 12; cNOTE++) {
|
|
|
|
pinMode(NOTE[cNOTE], INPUT);
|
2018-10-31 17:18:50 +01:00
|
|
|
}
|
2019-06-27 18:58:37 +02:00
|
|
|
for (int cButton = 0; cButton < MAXDPAD; cButton++) { // Capacitive Buttons configuration
|
|
|
|
bCap[cButton] = new CapacitiveSensor(SEND[cButton], RECEIVE[cButton]); // Initialized
|
2019-03-09 19:14:18 +01:00
|
|
|
bCap[cButton]->set_CS_AutocaL_Millis(0xFFFFFFFF); // No recalibration
|
|
|
|
bCap[cButton]->set_CS_Timeout_Millis(200); // Timeout set to 200ms (instead of 2s)
|
2019-06-27 18:58:37 +02:00
|
|
|
dpad[cButton] = LOW; // Button starts LOW
|
2019-03-09 15:23:18 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
for (int cStat = 0; cStat < MAXKEYS; 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-06-27 18:58:37 +02:00
|
|
|
pinMode(2, INPUT_PULLUP); // Used for overwrite 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-06-27 18:58:37 +02:00
|
|
|
if (sem_beat > 0) {
|
|
|
|
sem_beat--;
|
|
|
|
if (sem_gate > 0) { // If step was shorter than gate, close all open notes before next step
|
|
|
|
sem_gate--;
|
|
|
|
for (i=0; i<MAXKEYS; i++) if (current->kboard_s[c]) playNOTE(i, !current->kboard_s[c]);
|
|
|
|
for (i=0; i<MAXDPAD; i++) if (current->dpad_s[c]) playDrum(i, !current->dpad_s[c]);
|
|
|
|
}
|
2019-06-28 00:14:44 +02:00
|
|
|
if (digitalRead(ADD) && digitalRead(OW)) insertStep();
|
|
|
|
if (digitalRead(ADD) && !digitalRead(OW)) deleteStep(); // Placeholder because I miss a button
|
2019-06-27 18:58:37 +02:00
|
|
|
nextStep();
|
|
|
|
if (current != NULL) { // Play all step notes and begin counting for gate
|
|
|
|
for (i=0; i<MAXKEYS; i++) if (current->kboard_s[c]) playNOTE(i, current->kboard_s[c]);
|
|
|
|
for (i=0; i<MAXDPAD; i++) if (current->dpad_s[c]) playDrum(i, current->dpad_s[c]);
|
|
|
|
last_gate = millis();
|
|
|
|
sem_gate++;
|
|
|
|
}
|
2019-03-09 15:23:18 +01:00
|
|
|
}
|
2019-06-27 18:58:37 +02:00
|
|
|
if (sem_gate > 0 && (millis() - last_gate) > gate_length) {
|
|
|
|
sem_gate--;
|
|
|
|
for (i=0; i<MAXKEYS; i++) if (current->kboard_s[c]) playNOTE(i, !current->kboard_s[c]);
|
|
|
|
for (i=0; i<MAXDPAD; i++) if (current->dpad_s[c]) playDrum(i, !current->dpad_s[c]);
|
|
|
|
}
|
|
|
|
dpadhit = LOW;
|
|
|
|
for (int cButton = 0; cButton < MAXDPAD; cButton++) {
|
|
|
|
dpad[cButton] = evalButton(bCap[cButton], dpad[cButton], cButton);
|
|
|
|
dpadhit = (dpad[cButton] || dpadhit)
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
2019-03-10 02:33:59 +01:00
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
npressed = 0;
|
|
|
|
for (int cOCTAVE = 0; cOCTAVE < 4; cOCTAVE++) {
|
|
|
|
digitalWrite(OCTAVE[cOCTAVE], HIGH);
|
|
|
|
npressed += eval(scan(cOCTAVE));
|
|
|
|
digitalWrite(OCTAVE[cOCTAVE], LOW);
|
2019-03-09 19:14:18 +01:00
|
|
|
}
|
2019-06-28 00:14:44 +02:00
|
|
|
|
|
|
|
if (digitalRead(OW)) {
|
2019-06-27 18:58:37 +02:00
|
|
|
if (npressed > 0) current->kboard_s = kboard
|
|
|
|
if (dpadhit) current->dpad_s = dpad
|
2018-11-11 02:00:08 +01:00
|
|
|
}
|
2018-10-31 17:21:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
// Hardware specific functions
|
2019-03-04 18:56:20 +01:00
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
octst scan(int nOct) { // This function reads the 12 NOTE pins and returns a struct
|
|
|
|
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++) {
|
2019-06-27 18:58:37 +02:00
|
|
|
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-06-27 18:58:37 +02:00
|
|
|
bool evalButton(CapacitiveSensor* b, bool value, int note_number) {
|
|
|
|
long sensor = b->capacitiveSensor(1);
|
|
|
|
|
|
|
|
if (sensor > 15) {
|
|
|
|
if (value) return HIGH;
|
|
|
|
else {
|
|
|
|
playDrum(note_number, HIGH);
|
|
|
|
return HIGH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!value) return LOW;
|
|
|
|
else {
|
|
|
|
playDrum(note_number, LOW);
|
|
|
|
return LOW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE Functions
|
|
|
|
|
2019-03-08 21:10:15 +01:00
|
|
|
int eval(octst input) {
|
|
|
|
int pressed = 0;
|
2019-06-27 18:58:37 +02:00
|
|
|
int sNOTE = input.nOct * 12;
|
2019-03-08 21:10:15 +01:00
|
|
|
|
|
|
|
for (int c = 0; c < 12; c++) {
|
2019-06-27 18:58:37 +02:00
|
|
|
if (input.stat[c] ^ kboard[c + sNOTE]) {
|
|
|
|
playNOTE(c + sNOTE, input.stat[c]);
|
|
|
|
kboard[c + sNOTE] = input.stat[c];
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
2019-06-27 18:58:37 +02:00
|
|
|
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-06-27 18:58:37 +02:00
|
|
|
void playNOTE(int c, bool status) {
|
|
|
|
byte n = c + NOTEOffset;
|
2019-03-08 21:10:15 +01:00
|
|
|
if (status == HIGH) {
|
2019-06-27 18:58:37 +02:00
|
|
|
MIDI.sendNOTEOn(n, velocity, channel);
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
|
|
|
else if (status == LOW) {
|
2019-06-27 18:58:37 +02:00
|
|
|
MIDI.sendNOTEOff(n, velocity, channel);
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
void playDrum(int c, bool status) {
|
|
|
|
byte n = c + drumOffset;
|
|
|
|
if (status == HIGH) {
|
|
|
|
MIDI.sendNOTEOn(n, velocity, (byte)7);
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
2019-06-27 18:58:37 +02:00
|
|
|
else if (status == LOW) {
|
|
|
|
MIDI.sendNOTEOff(n, velocity, (byte)7);
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|
2019-03-09 19:14:18 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
// Sync functions
|
|
|
|
|
2019-03-09 19:14:18 +01:00
|
|
|
void sync() {
|
|
|
|
if (Serial.available() && Serial.read() == MIDICLOCK) {
|
|
|
|
midiclock++;
|
2019-06-27 18:58:37 +02:00
|
|
|
if (midiclock == 0 && sem_beat == 0) sem_beat++;
|
|
|
|
else if (midiclock == 24) midiclock = 0;
|
2019-03-09 19:14:18 +01:00
|
|
|
}
|
2019-03-10 02:33:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
// List management functions
|
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
link newStep() {
|
|
|
|
return (link)malloc(sizeof(struct SequencerStep));
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
bool insertStep() {
|
2019-03-10 02:33:59 +01:00
|
|
|
link newS = newStep();
|
|
|
|
if (newS == NULL) {
|
|
|
|
free(newS);
|
|
|
|
return LOW;
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:58:37 +02:00
|
|
|
newS->kboard_s = kboard;
|
|
|
|
newS->dpad_s = dpad
|
|
|
|
|
2019-03-10 02:33:59 +01:00
|
|
|
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;
|
2019-03-08 21:10:15 +01:00
|
|
|
}
|