cv-keyboard/cvkeyboard.ino

177 lines
3.3 KiB
Arduino
Raw Normal View History

2018-10-31 17:18:50 +01:00
#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
2018-10-31 17:44:56 +01:00
#define offCounter 0
2018-11-11 01:11:49 +01:00
#define MINUTE 60000
2018-10-31 17:18:50 +01:00
#include <MIDI.h>
#include <HID.h>
MIDI_CREATE_DEFAULT_INSTANCE();
int note[12] = {
2018-11-11 01:11:49 +01:00
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B }; // Note Pins above
2018-10-31 17:18:50 +01:00
int octave[4] = {
2018-11-11 01:11:49 +01:00
Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above
2018-10-31 17:18:50 +01:00
int noteCounter[49] = { 0 };
2018-11-11 01:11:49 +01:00
boolean status[49] = { LOW };
2018-10-31 17:18:50 +01:00
boolean flip[49] = { LOW };
2018-11-11 01:11:49 +01:00
boolean buffer = LOW;
int octBuffer;
2018-10-31 17:18:50 +01:00
byte noteBuffer;
2018-11-11 01:11:49 +01:00
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
2018-11-11 01:11:49 +01:00
unsigned long nextBeat = 0;
2018-11-11 01:11:49 +01:00
int step = 0;
int lastStep = 0;
boolean notePlayed = LOW;
2018-11-11 01:11:49 +01:00
2018-10-31 17:18:50 +01:00
void setup()
{
for (int cOctave = 0; cOctave < 4; cOctave++) {
pinMode(octave[cOctave], OUTPUT);
}
for (int cNote = 0; cNote < 12; cNote++) {
pinMode(note[cNote], INPUT);
}
MIDI.begin(MIDI_CHANNEL_OFF);
Serial.begin(115200);
2018-11-11 01:11:49 +01:00
nextBeat = millis() + (MINUTE / bpm);
2018-10-31 17:18:50 +01:00
}
void loop() {
2018-11-11 01:11:49 +01:00
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;
2018-10-31 17:18:50 +01:00
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;
}
2018-10-31 17:21:02 +01:00
}
void send() {
for (int c = 48; c >= 0; c--) {
if (flip[c] == HIGH) {
flip[c] = LOW;
2018-10-31 17:44:56 +01:00
if (noteCounter[c] > 0) {
noteCounter[c]--;
2018-10-31 17:21:02 +01:00
}
2018-10-31 17:44:56 +01:00
else {
noteCounter[c] = offCounter;
noteBuffer = c + noteOffset;
if (status[c] == HIGH) {
2018-11-11 01:11:49 +01:00
MIDI.sendNoteOn(noteBuffer, velocity, channel);
2018-10-31 17:44:56 +01:00
}
else if (status[c] == LOW) {
2018-11-11 01:11:49 +01:00
MIDI.sendNoteOff(noteBuffer, velocity, channel);
2018-10-31 17:44:56 +01:00
}
2018-10-31 17:21:02 +01:00
}
}
}
2018-10-31 17:18:50 +01:00
}
2018-11-11 01:11:49 +01:00
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++;
2018-11-11 01:11:49 +01:00
while (step < 49 && status[step] == LOW) {
step++;
}
if (step == 49) {
step = 0;
}
else {
playNote(lastStep, LOW);
2018-11-11 01:11:49 +01:00
playNote(step, HIGH);
lastStep = step;
notePlayed = HIGH;
2018-11-11 01:11:49 +01:00
}
return;
}
2018-10-31 17:18:50 +01:00
void scan() {
for (int cOctave = 0; cOctave < 4; cOctave++) {
2018-10-31 17:44:56 +01:00
octBuffer = 12 * cOctave;
2018-10-31 17:18:50 +01:00
digitalWrite(octave[cOctave], HIGH);
for (int cNote = 0; cNote < 12; cNote++) {
2018-11-11 01:11:49 +01:00
buffer = digitalRead(note[cNote]);
2018-10-31 17:18:50 +01:00
2018-11-11 01:11:49 +01:00
if (buffer ^ status[cNote + octBuffer]) {
status[cNote + octBuffer] = buffer;
flip[cNote + octBuffer] = HIGH;
}
else {
flip[cNote + octBuffer] = LOW;
}
2018-10-31 17:18:50 +01:00
}
digitalWrite(octave[cOctave], LOW);
}
2018-11-11 01:11:49 +01:00
}
int nPressed() {
int c, n = 0;
for (c = 0; c < 49; c++) {
if (status[c] == HIGH) {
n++;
}
}
2018-10-31 17:18:50 +01:00
}