cv-keyboard/cvkeyboard.ino

130 lines
2.7 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
//#include <MIDI.h>
//#include <HID.h>
//MIDI_CREATE_DEFAULT_INSTANCE();
typedef struct OctaveStatus {
bool stat[12];
int nOct;
} octst;
2018-10-31 17:18:50 +01:00
int note[12] = {
2019-03-04 19:04:13 +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] = {
2019-03-04 19:04:13 +01:00
Oct1, Oct2, Oct3, Oct4 }; // Octave Pins above
int ledPins[12]{
19, 18, 17, 16, 15, 14, 2, 3, 4, 5, 6, 0 };
2018-11-11 01:11:49 +01:00
2019-03-04 19:04:13 +01:00
int clock = 0; // Keeps track of current octave
octst buff;
2018-11-11 01:11:49 +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);
}
for (int cLed = 0; cLed < 12; cLed++) {
pinMode(ledPins[cLed], OUTPUT);
}
// MIDI.begin(MIDI_CHANNEL_OFF);
2018-10-31 17:18:50 +01:00
Serial.begin(115200);
// nextBeat = millis() + (MINUTE / bpm);
for (int cLed = 0; cLed < 12; cLed++) {
digitalWrite(ledPins[cLed], HIGH);
delay(100);
}
for (int cLed = 0; cLed < 12; cLed++) {
digitalWrite(ledPins[cLed], LOW);
delay(100);
}
pinMode(50, OUTPUT);
}
void loop() {
for (clock = 0; clock < 4; clock++) {
digitalWrite(octave[clock], HIGH);
buff = scan(clock);
digitalWrite(octave[clock], LOW);
debug(buff);
serialDebug(buff);
}
2018-10-31 17:21:02 +01:00
}
2019-03-04 18:56:20 +01:00
2019-03-04 19:04:13 +01:00
octst scan(int nOct) { // This function reads the 12 note pins and returns a struct
int c; // with 1 bool for each note
octst output;
2018-10-31 17:18:50 +01:00
output.nOct = nOct;
2018-11-11 01:11:49 +01:00
for (c = 0; c < 12; c++) {
output.stat[c] = digitalRead(note[c]);
2018-11-11 01:11:49 +01:00
}
return output;
2018-11-11 01:11:49 +01:00
}
2019-03-04 19:04:13 +01:00
void debug(octst input) { // Lights up 12 LEDs used to control the readings
int c;
for (c = 0; c < 12; c++) {
digitalWrite(ledPins[c], input.stat[c]);
}
delay(5);
for (c = 0; c < 12; c++) {
digitalWrite(ledPins[c], LOW);
2018-10-31 17:18:50 +01:00
}
2018-11-11 01:11:49 +01:00
}
2019-03-04 19:04:13 +01:00
void serialDebug(octst input) { // Prints on the Serial Monitor the 12 bits just read
for (int c = 0; c < 12; c++) {
Serial.print(input.stat[c]);
2018-11-11 01:11:49 +01:00
}
Serial.println("");
2018-10-31 17:18:50 +01:00
}
2019-03-04 19:04:13 +01:00
bool debouncedRead(int pin) { // Should clear readings from false positives but doesn't work
2019-03-04 18:56:20 +01:00
if (digitalRead(pin) == HIGH) {
if (digitalRead(pin) == HIGH) {
if (digitalRead(pin) == HIGH) {
if (digitalRead(pin) == HIGH) {
if (digitalRead(pin) == HIGH) {
return HIGH;
}
}
}
}
}
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;
}
2019-03-04 18:56:20 +01:00
return output;
}*/