Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @codetinkerhack codetinkerhack revised this gist Jan 21, 2013. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions midiKeyboard_VelocityAftertouch.c
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    const int rowPin[] = { 6, 7, 8, 5 };

    // FSRs connected to Analog
    const int potentPin[] = { 2, 3, 4, 5 };
    const int fsrPin[] = { 2, 3, 4, 5 };

    // The 74HC595 uses a serial communication
    // link which has three pins
    @@ -20,7 +20,7 @@ int notesChannel[4] = { 0, 0, 0, 0 };

    // use prepared bit vectors instead of shifting bit left everytime
    int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };
    int potentValue[4];
    int fsrValue[4];

    unsigned long currentMillis;
    unsigned long previousMillisChannel[4] = { 0, 0, 0, 0 };
    @@ -104,17 +104,17 @@ void loop() {
    void sendChannelExpression() {
    for (int c = 0; c < 4; c++) {
    if (currentMillis - previousMillisChannel[c] > expressionDelay) {
    midiChannelExpression(c, potentValue[c]);
    midiChannelExpression(c, fsrValue[c]);
    }
    }
    }


    void readChannelPressure(int channel) {
    int sensor = analogRead(potentPin[channel]);
    int sensor = analogRead(fsrPin[channel]);

    if (sensor > 0) {
    potentValue[channel] = map(sensor, 0, 900, 10, 127); //log(sensor2)*30+abs(sensor2);
    fsrValue[channel] = map(sensor, 0, 900, 10, 127); //log(sensor2)*30+abs(sensor2);
    }
    }

    @@ -126,7 +126,7 @@ void sendChannelNotes() {
    if (notesChannel[i] > 0 && (currentMillis > previousMillisChannel[i])) {
    for (int t = 0; t < notesChannel[i]; t++) {
    if (keyPressed[noteChannelPressed[i][t]] == true)
    noteOn(i, keyToMidiMap[noteChannelPressed[i][t]], potentValue[i]);
    noteOn(i, keyToMidiMap[noteChannelPressed[i][t]], fsrValue[i]);
    }
    notesChannel[i] = 0;

    @@ -184,4 +184,4 @@ void command(int cmd, int value1, int value2) {
    Serial.write(cmd);
    Serial.write(value1);
    Serial.write(value2);
    }
    }
  2. @codetinkerhack codetinkerhack created this gist Jan 21, 2013.
    187 changes: 187 additions & 0 deletions midiKeyboard_VelocityAftertouch.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,187 @@
    // Pin Definitions
    // Rows are connected to Digital
    const int rowPin[] = { 6, 7, 8, 5 };

    // FSRs connected to Analog
    const int potentPin[] = { 2, 3, 4, 5 };

    // The 74HC595 uses a serial communication
    // link which has three pins
    const int clock = 12;
    const int latch = 13;
    const int data = 11;

    int keyToMidiMap[32];

    boolean keyPressed[32];
    int noteChannelPressed[4][64];
    int notesChannel[4] = { 0, 0, 0, 0 };


    // use prepared bit vectors instead of shifting bit left everytime
    int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };
    int potentValue[4];

    unsigned long currentMillis;
    unsigned long previousMillisChannel[4] = { 0, 0, 0, 0 };

    // miliseconds delay before aftertouch sent
    unsigned long expressionDelay=1000;

    // 74HC595 shift to next column
    void scanColumn(int value) {
    digitalWrite(latch, LOW); //Pulls the chips latch low
    shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
    digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
    }

    void setup() {

    // Map scan matrix buttons/keys to actual Midi note number. Lowest num 41 corresponds to F MIDI note.
    keyToMidiMap[0] = 48;
    keyToMidiMap[1] = 41;
    keyToMidiMap[2] = 42;
    keyToMidiMap[3] = 43;
    keyToMidiMap[4] = 44;
    keyToMidiMap[5] = 45;
    keyToMidiMap[6] = 46;
    keyToMidiMap[7] = 47;

    keyToMidiMap[8] = 56;
    keyToMidiMap[1 + 8] = 49;
    keyToMidiMap[2 + 8] = 50;
    keyToMidiMap[3 + 8] = 51;
    keyToMidiMap[4 + 8] = 52;
    keyToMidiMap[5 + 8] = 53;
    keyToMidiMap[6 + 8] = 54;
    keyToMidiMap[7 + 8] = 55;

    keyToMidiMap[16] = 64;
    keyToMidiMap[1 + 16] = 57;
    keyToMidiMap[2 + 16] = 58;
    keyToMidiMap[3 + 16] = 59;
    keyToMidiMap[4 + 16] = 60;
    keyToMidiMap[5 + 16] = 61;
    keyToMidiMap[6 + 16] = 62;
    keyToMidiMap[7 + 16] = 63;

    keyToMidiMap[24] = 72;
    keyToMidiMap[1 + 24] = 65;
    keyToMidiMap[2 + 24] = 66;
    keyToMidiMap[3 + 24] = 67;
    keyToMidiMap[4 + 24] = 68;
    keyToMidiMap[5 + 24] = 69;
    keyToMidiMap[6 + 24] = 70;
    keyToMidiMap[7 + 24] = 71;

    // setup pins output/input mode
    pinMode(data, OUTPUT);
    pinMode(clock, OUTPUT);
    pinMode(latch, OUTPUT);

    pinMode(rowPin[0], INPUT);
    pinMode(rowPin[1], INPUT);
    pinMode(rowPin[2], INPUT);
    pinMode(rowPin[3], INPUT);

    Serial.begin(31250);

    delay(1000);

    }

    void loop() {

    currentMillis = millis();

    getNote();

    sendChannelExpression();

    sendChannelNotes();
    }

    void sendChannelExpression() {
    for (int c = 0; c < 4; c++) {
    if (currentMillis - previousMillisChannel[c] > expressionDelay) {
    midiChannelExpression(c, potentValue[c]);
    }
    }
    }


    void readChannelPressure(int channel) {
    int sensor = analogRead(potentPin[channel]);

    if (sensor > 0) {
    potentValue[channel] = map(sensor, 0, 900, 10, 127); //log(sensor2)*30+abs(sensor2);
    }
    }

    void sendChannelNotes() {
    for (int i = 0; i < 4; i++) {

    readChannelPressure(i);

    if (notesChannel[i] > 0 && (currentMillis > previousMillisChannel[i])) {
    for (int t = 0; t < notesChannel[i]; t++) {
    if (keyPressed[noteChannelPressed[i][t]] == true)
    noteOn(i, keyToMidiMap[noteChannelPressed[i][t]], potentValue[i]);
    }
    notesChannel[i] = 0;

    }
    }
    }

    void getNote() {

    for (int i = 0; i < 8; i++) {

    scanColumn(bits[i]);

    for (int t = 0; t < 4; t++) {

    int groupValue = digitalRead(rowPin[t]);

    // key pressed
    if (groupValue != 0 && !keyPressed[8 * t + i]) {

    keyPressed[8 * t + i] = true;

    // put the note into buffer

    previousMillisChannel[t] = currentMillis;

    noteChannelPressed[t][notesChannel[t]] = 8 * t + i;
    notesChannel[t]++;
    }

    // key released

    if (groupValue == 0 && keyPressed[8 * t + i]) {

    keyPressed[8 * t + i] = false;
    noteOn(t, keyToMidiMap[8 * t + i], 0);
    }
    }

    }
    }


    void noteOn(int channel, int pitch, int velocity) {
    command(0x90 + channel, pitch, velocity);
    }


    void midiChannelExpression(int channel, int value) {
    // Modulation wheel effect
    command(0xB0 + channel, 0x01, value);
    }

    void command(int cmd, int value1, int value2) {
    Serial.write(cmd);
    Serial.write(value1);
    Serial.write(value2);
    }