Commited previous testing branch

Completely rewriting my code to make it more modular and debuggable. Pushing to master previous status because the code currently in master is just way too old
This commit is contained in:
əlemi 2019-03-04 18:53:16 +01:00 committed by GitHub
parent 1a37d52310
commit c3ec7b1bb0

View file

@ -18,24 +18,37 @@
#define Oct4 10 #define Oct4 10
#define noteOffset 36 #define noteOffset 36
#define offCounter 100 #define offCounter 0
#define MINUTE 60000
#include <MIDI.h> #include <MIDI.h>
#include <HID.h> #include <HID.h>
MIDI_CREATE_DEFAULT_INSTANCE(); MIDI_CREATE_DEFAULT_INSTANCE();
int note[12] = { int note[12] = {
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B }; // Pin delle note : 0 -> C , 11 -> B 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 }; // Pin delle ottave : 0 -> 2 , 3 -> 5 Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above
int noteCounter[49] = { 0 }; int noteCounter[49] = { 0 };
boolean status[49] = { LOW }; // Array di stato, aggiornato durante ogni ciclo. 0 -> C2 , 11 -> C3 , 23 -> C4 -> , 35 -> C5 , 48 -> C6 boolean status[49] = { LOW };
boolean flip[49] = { LOW }; boolean flip[49] = { LOW };
boolean buffer = LOW; // Usato come buffer per lo stato di ogni pin, per non chiamare una lettura ogni volta. boolean buffer = LOW;
int octBuffer; // Usato per non ripetere l'aritmetica ad ogni accesso all'array di stato.
int octBuffer;
byte noteBuffer; byte noteBuffer;
byte velocity = 100; // Placeholder.
int channel = 7; // Placeholder.
byte velocity = 100; // Placeholder. Will need something to change it
int channel = 7; // Placeholder. Will need something to change it
int bpm = 120; // Placeholder. Will need something to change it
int gate = 300; // Placeholder. Will need something to change it
unsigned long nextBeat = 0;
int step = 0;
int lastStep = 0;
boolean notePlayed = LOW;
void setup() void setup()
@ -48,11 +61,86 @@ void setup()
} }
MIDI.begin(MIDI_CHANNEL_OFF); MIDI.begin(MIDI_CHANNEL_OFF);
Serial.begin(115200); Serial.begin(115200);
nextBeat = millis() + (MINUTE / bpm);
} }
void loop() { void loop() {
if (millis() < nextBeat) return;
notePlayed = LOW;
while (notePlayed == LOW) {
cleanScan();
arp();
}
nextBeat += (MINUTE / bpm);
}
void cleanScan() {
int c;
for (c = 0; c < 49; c++) noteCounter[c] = 0;
scan(); scan();
for (c = 0; c < 49; c++) {
if (status[c] == HIGH) noteCounter[c]++;
}
scan();
for (c = 0; c < 49; c++) {
if (status[c] == HIGH) noteCounter[c]++;
}
scan();
for (c = 0; c < 49; c++) {
if (status[c] == HIGH) noteCounter[c]++;
}
for (c = 0; c < 49; c++) {
if (noteCounter[c] == 3) status[c] = HIGH;
else status[c] = LOW;
}
}
void send() {
for (int c = 48; c >= 0; c--) {
if (flip[c] == HIGH) {
flip[c] = LOW;
if (noteCounter[c] > 0) {
noteCounter[c]--;
}
else {
noteCounter[c] = offCounter;
noteBuffer = c + noteOffset;
if (status[c] == HIGH) {
MIDI.sendNoteOn(noteBuffer, velocity, channel);
}
else if (status[c] == LOW) {
MIDI.sendNoteOff(noteBuffer, velocity, channel);
}
}
}
}
}
void playNote(int c, boolean status) {
if (status == HIGH) {
MIDI.sendNoteOn(c + noteOffset, velocity, channel);
}
else if (status == LOW) {
MIDI.sendNoteOff(c + noteOffset, velocity, channel);
}
}
void arp() {
step++;
while (step < 49 && status[step] == LOW) {
step++;
}
if (step == 49) {
step = 0;
}
else {
playNote(lastStep, LOW);
playNote(step, HIGH);
lastStep = step;
notePlayed = HIGH;
}
return;
} }
void scan() { void scan() {
@ -62,31 +150,27 @@ void scan() {
for (int cNote = 0; cNote < 12; cNote++) { for (int cNote = 0; cNote < 12; cNote++) {
if (noteCounter[cNote + octBuffer] > 0) { buffer = digitalRead(note[cNote]);
noteCounter[cNote + octBuffer]--;
if (buffer ^ status[cNote + octBuffer]) {
status[cNote + octBuffer] = buffer;
flip[cNote + octBuffer] = HIGH;
} }
else { else {
noteCounter[cNote + octBuffer] = offCounter; flip[cNote + octBuffer] = LOW;
buffer = digitalRead(note[cNote]);
if (buffer ^ status[cNote + octBuffer]) {
status[cNote + octBuffer] = buffer;
flip[cNote + octBuffer] = HIGH;
noteBuffer = cNote + octBuffer + noteOffset;
if (buffer == HIGH) {
MIDI.sendNoteOn(noteBuffer, velocity, 1);
}
if (buffer == LOW) {
MIDI.sendNoteOff(noteBuffer, velocity, 1);
}
}
else {
flip[cNote + octBuffer] = LOW;
}
} }
} }
digitalWrite(octave[cOctave], LOW); digitalWrite(octave[cOctave], LOW);
} }
}
int nPressed() {
int c, n = 0;
for (c = 0; c < 49; c++) {
if (status[c] == HIGH) {
n++;
}
}
} }