Created
March 31, 2015 19:13
-
-
Save pbochenski/6a77521e7cebe0baaa9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//private methods: | |
void readKeyboard(); | |
void resetButtons(); | |
void advanceButtons(); | |
void pushToSerial(); | |
byte getPitch(int i); | |
void midi_note_off(byte key); | |
void midi_note_on(byte key); | |
void midi_command(byte command, byte channel, byte param1, byte param2); | |
//UCY74151 connection | |
const int DATA_SELECT1 = 6; | |
const int DATA_SELECT2 = 7; | |
const int DATA_SELECT3 = 8; | |
const int OUTPUT_Y = 4; | |
const int OUTPUT_Y_2 = 5; | |
//keys | |
const int NR_OF_BUTTONS_ON_MPLEX = 8; | |
const int NR_OF_MPLEX = 2; | |
boolean buttons[NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX] = { }; | |
boolean previousButtons[NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX] = { }; | |
long debounceTimes[NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX] = { }; | |
//TODO: connect buttons to correct input in multiplexer | |
//TODO: check once again midi numbers for fl keys | |
byte pitch[] = | |
{ 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83, 84, 86 }; // see pitches at: http://newt.phys.unsw.edu.au/jw/notes.html | |
unsigned const long int DEBOUNCE = 7; | |
void debugTable(boolean* table, int size) { | |
for (int i = 0; i < size; i++) { | |
Serial.print(table[i]); | |
Serial.print(";"); | |
} | |
Serial.print("\n"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(DATA_SELECT1, OUTPUT); | |
pinMode(DATA_SELECT2, OUTPUT); | |
pinMode(DATA_SELECT3, OUTPUT); | |
pinMode(OUTPUT_Y, INPUT); | |
pinMode(OUTPUT_Y_2, INPUT); | |
resetButtons(); | |
} | |
void loop() { | |
readKeyboard(); | |
pushToSerial(); | |
advanceButtons(); | |
} | |
void readKeyboard() { | |
for (byte i = 0; i < NR_OF_BUTTONS_ON_MPLEX; ++i) { | |
digitalWrite(DATA_SELECT1, (bool) (i & 0b00000001)); | |
digitalWrite(DATA_SELECT2, (bool) (i & 0b00000010)); | |
digitalWrite(DATA_SELECT3, (bool) (i & 0b00000100)); | |
buttons[i] = !digitalRead(OUTPUT_Y); | |
buttons[i + NR_OF_BUTTONS_ON_MPLEX] = !digitalRead(OUTPUT_Y_2); | |
} | |
} | |
void resetButtons() { | |
for (int i = 0; i < NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX; i++) { | |
buttons[i] = false; | |
previousButtons[i] = false; | |
debounceTimes[i] = 0; | |
} | |
} | |
void advanceButtons() { | |
for (int i = 0; i < NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX; i++) { | |
previousButtons[i] = buttons[i]; | |
buttons[i] = false; | |
} | |
} | |
void pushToSerial() { | |
for (int i = 0; i < NR_OF_BUTTONS_ON_MPLEX * NR_OF_MPLEX; i++) { | |
if (buttons[i] != previousButtons[i]) { | |
if( millis() - debounceTimes[i] > DEBOUNCE){ | |
debounceTimes[i] = millis(); | |
byte pitch = getPitch(i); | |
if (buttons[i] == true) { | |
midi_note_on(pitch); | |
} else { | |
midi_note_off(pitch); | |
} | |
} | |
} | |
} | |
} | |
byte getPitch(int i) { | |
return pitch[i]; | |
} | |
//midi | |
const byte MIDI_NOTE_OFF = 0x80; | |
const byte MIDI_NOTE_ON = 0x90; | |
const byte MIDI_VELOCITY = 127; | |
const byte MIDI_CHANNEL = 0; | |
void midi_note_off(byte key) { | |
midi_command(MIDI_NOTE_OFF, MIDI_CHANNEL, key, MIDI_VELOCITY); | |
} | |
void midi_note_on(byte key) { | |
midi_command(MIDI_NOTE_ON, MIDI_CHANNEL, key, MIDI_VELOCITY); | |
} | |
void midi_command(byte command, byte channel, byte param1, byte param2) { | |
Serial.write(command | (channel & 0x0F)); | |
Serial.write(param1 & 0x7F); | |
Serial.write(param2 & 0x7F); | |
// Serial.print(command); | |
// Serial.print(" "); | |
// Serial.println(param1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment