Implemented an arpeggiator and then broke it (should wait for MIDI clock). Added a basic implementation of 3 capacitive buttons (for drum pads)

This commit is contained in:
əlemi 2019-03-08 21:10:15 +01:00
parent eadbbbf9eb
commit 7c7b0cf116

View file

@ -18,10 +18,13 @@
#define Oct4 10 #define Oct4 10
#define noteOffset 36 #define noteOffset 36
#define MINUTE 60000
//#include <MIDI.h> #include <CapacitiveSensor.h>
//#include <HID.h> #include <MIDI.h>
//MIDI_CREATE_DEFAULT_INSTANCE(); #include <HID.h>
MIDI_CREATE_DEFAULT_INSTANCE();
typedef struct OctaveStatus { typedef struct OctaveStatus {
bool stat[12]; bool stat[12];
@ -32,11 +35,24 @@ int note[12] = {
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B }; // Note Pins above C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B }; // Note Pins above
int octave[4] = { int octave[4] = {
Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above
int ledPins[12]{
19, 18, 17, 16, 15, 14, 2, 3, 4, 5, 6, 0 };
int clock = 0; // Keeps track of current octave int clock = 0; // Used if arp to cycle through notes
octst buff; octst buff;
bool kboard[49];
bool raw; // Global Settings. RAW = signal is sent when key is detected
byte velocity = 100;
byte channel = 1;
byte midi_clock = 0xf8;
byte dataIn;
int bpm = 360;
unsigned long nextBeat = 0;
unsigned long gate = 50; //ms of keypress if arpeggiator
int npressed;
bool bu1, bu2, bu3;
CapacitiveSensor b1 = CapacitiveSensor(5, 6);
CapacitiveSensor b2 = CapacitiveSensor(4, 3);
CapacitiveSensor b3 = CapacitiveSensor(16, 17);
void setup() { void setup() {
for (int cOctave = 0; cOctave < 4; cOctave++) { for (int cOctave = 0; cOctave < 4; cOctave++) {
@ -45,30 +61,43 @@ 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 cLed = 0; cLed < 12; cLed++) { MIDI.begin(MIDI_CHANNEL_OFF);
pinMode(ledPins[cLed], OUTPUT);
}
// MIDI.begin(MIDI_CHANNEL_OFF);
Serial.begin(115200); Serial.begin(115200);
// nextBeat = millis() + (MINUTE / bpm); nextBeat = millis() + (MINUTE / bpm);
for (int cLed = 0; cLed < 12; cLed++) { pinMode(2, INPUT_PULLUP);
digitalWrite(ledPins[cLed], HIGH); for (int cStat = 0; cStat < 49; cStat++) kboard[cStat] = LOW;
delay(100); nextBeat = 0;
}
for (int cLed = 0; cLed < 12; cLed++) { b1.set_CS_AutocaL_Millis(0xFFFFFFFF);
digitalWrite(ledPins[cLed], LOW); b2.set_CS_AutocaL_Millis(0xFFFFFFFF);
delay(100); b3.set_CS_AutocaL_Millis(0xFFFFFFFF);
} bu1 = LOW;
pinMode(50, OUTPUT); bu2 = LOW;
bu3 = LOW;
} }
void loop() { void loop() {
for (clock = 0; clock < 4; clock++) { scanButtons();
digitalWrite(octave[clock], HIGH);
buff = scan(clock); npressed = 0;
digitalWrite(octave[clock], LOW); raw = digitalRead(2);
debug(buff); for (int cOctave = 0; cOctave < 4; cOctave++) {
serialDebug(buff); digitalWrite(octave[cOctave], HIGH);
npressed += eval(scan(cOctave));
digitalWrite(octave[cOctave], LOW);
}
if (raw) return;
if (npressed < 1) return;
dataIn = Serial.read();
if (dataIn == midi_clock) {
clock++;
while (kboard[clock] == LOW) {
clock++;
if (clock == 49) clock = 0;
}
playNote(clock, HIGH);
delay(gate);
playNote(clock, LOW);
} }
} }
@ -85,15 +114,18 @@ octst scan(int nOct) { // This function reads the 12 note pins and returns a
return output; return output;
} }
void debug(octst input) { // Lights up 12 LEDs used to control the readings int eval(octst input) {
int c; int pressed = 0;
for (c = 0; c < 12; c++) { int snote = input.nOct * 12;
digitalWrite(ledPins[c], input.stat[c]);
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];
} }
delay(5); if (kboard[c + snote] == HIGH) pressed++;
for (c = 0; c < 12; c++) {
digitalWrite(ledPins[c], LOW);
} }
return pressed;
} }
void serialDebug(octst input) { // Prints on the Serial Monitor the 12 bits just read void serialDebug(octst input) { // Prints on the Serial Monitor the 12 bits just read
@ -103,28 +135,81 @@ void serialDebug(octst input) { // Prints on the Serial Monitor the 12 bits ju
Serial.println(""); Serial.println("");
} }
bool debouncedRead(int pin) { // Should clear readings from false positives but doesn't work void playNote(int c, bool status) {
if (digitalRead(pin) == HIGH) { byte n = c + noteOffset;
if (digitalRead(pin) == HIGH) { if (status == HIGH) {
if (digitalRead(pin) == HIGH) { MIDI.sendNoteOn(n, velocity, channel);
if (digitalRead(pin) == HIGH) { }
if (digitalRead(pin) == HIGH) { else if (status == LOW) {
MIDI.sendNoteOff(n, velocity, channel);
}
}
void scanButtons() {
long sensor1 = b1.capacitiveSensor(1);
long sensor2 = b2.capacitiveSensor(1);
long sensor3 = b3.capacitiveSensor(1);
if (sensor1 > 10) {
if (!bu1) {
MIDI.sendNoteOn(95, velocity, 7);
bu1 = HIGH;
}
}
else {
if (bu1) {
MIDI.sendNoteOff(95, velocity, 7);
bu1 = LOW;
}
}
if (sensor2 > 10) {
if (!bu2) {
MIDI.sendNoteOn(97, velocity, 7);
bu2 = HIGH;
}
}
else {
if (bu2) {
MIDI.sendNoteOff(97, velocity, 7);
bu2 = LOW;
}
}
if (sensor3 > 10) {
if (!bu3) {
MIDI.sendNoteOn(99, velocity, 7);
bu3 = HIGH;
}
}
else {
if (bu3) {
MIDI.sendNoteOff(99, velocity, 7);
bu3 = LOW;
}
}
/*bu1 = evalButton(b1, bu1, 95);
bu2 = evalButton(b2, bu2, 97);
bu3 = evalButton(b3, bu3, 99);*/
}
bool evalButton(CapacitiveSensor b, bool value, int note) {
long sensor = b.capacitiveSensor(1);
if (sensor > 15) {
if (value) return HIGH;
else {
MIDI.sendNoteOn(note, velocity, 7);
return HIGH; return HIGH;
} }
} }
} else {
} if (!value) return LOW;
} else {
MIDI.sendNoteOff(note, velocity, 7);
return LOW; return LOW;
} }
/*octst clearOct(octst o1, octst o2, octst o3, octst o4, octst o5) {
octst output;
output.nOct = o1.nOct;
for (int c = 0; c < 12; +c++) {
if (o1.stat[c] && o2.stat[c] && o3.stat[c] && o4.stat[c] && o5.stat[c]) output.stat[c] = HIGH;
else output.stat[c] = LOW;
} }
return output; }
}*/