Skip to content

Instantly share code, notes, and snippets.

@ruizjme
Created November 7, 2015 11:53
Show Gist options
  • Save ruizjme/8128e55429e026602cb7 to your computer and use it in GitHub Desktop.
Save ruizjme/8128e55429e026602cb7 to your computer and use it in GitHub Desktop.

Revisions

  1. ruizjme created this gist Nov 7, 2015.
    180 changes: 180 additions & 0 deletions MIDI-Mod-Pot-Triggers.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,180 @@

    /*
    MOD
    POT
    BUTTONS (toggle)
    Smoothing
    CONNECTIONS:
    |-------------5V
    [POT]----------|-------------A0
    |-------------GND
    |-----------5V
    D2-------[BUTTON1]
    |--(10kR)---GND
    |-----------5V
    D3-------[BUTTON2]
    |--(10kR)---GND
    D10------[LED]------(10kR)---GND
    __________________
    | |-[Vcc]---5V
    | PROXIMITY SENSOR |-[Trig]--D13
    | |-[Echo]--D12
    |__________________|-[Gnd]---GND
    */

    #include <MIDI.h>

    MIDI_CREATE_DEFAULT_INSTANCE();

    #define maxDist 50 // defines range that the prox sensor will look at (in cm)

    #define channelOut 1 // MIDI channel (1-16)

    #define noteMod 1 // MIDI cc note number
    #define notePot 16
    #define noteButton1 17
    #define noteButton2 18

    #define led 10 // pins
    #define button1 2
    #define button2 3
    #define trigPin 13
    #define echoPin 12
    int potPin = A0;

    int button1State; // store state of buttons
    int button2State;
    int button1Prev = LOW;
    int button2Prev = LOW;

    const int numReadings = 10; // define the number of samples to keep track of
    const int thld = 2; // threshold for repeated values sent
    long time = 0; // for button state tracking
    long debounce = 50;

    int readingsMod[numReadings]; // the readings from the mod wheel
    int readingsPot[numReadings]; // readings from pot

    int readIndex = 0; // the index of the current reading
    int totalMod = 0; // the running total
    int totalPot = 0;
    int averageMod = 0; // the average
    int averagePot = 0;
    int lastMod = 0; // to store previous reading
    int lastPot = 0;

    void setup()
    {
    // initialize serial communication with computer:
    MIDI.begin();
    Serial.begin(115200);
    // initialize all the readings to 0:
    for (int thisReading = 0; thisReading < numReadings; thisReading++){
    readingsMod[thisReading] = 0;
    readingsPot[thisReading] = 0;
    }

    pinMode(button1, INPUT);
    pinMode(button2, INPUT);
    pinMode(led, OUTPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    }

    void loop() {
    // read button states
    button1State = digitalRead(button1);
    button2State = digitalRead(button2);

    // read distance with proximity sensor
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;

    // Button states
    if (button1State == HIGH && button1Prev == LOW && millis() - time > debounce) {
    MIDI.sendControlChange(noteButton1, 127, channelOut);
    time = millis();
    }

    if (button2State == HIGH && button2Prev == LOW && millis() - time > debounce) {
    MIDI.sendControlChange(noteButton2, 127, channelOut);
    time = millis();
    }

    // average Pot readings
    totalPot = totalPot - readingsPot[readIndex];
    readingsPot[readIndex] = analogRead(potPin);
    totalPot = totalPot + readingsPot[readIndex];

    averagePot = totalPot / numReadings;

    // map reading values to midi range
    int mapPot = map(averagePot, 0, 1023, 0, 127);

    if(mapPot>124) mapPot = 127;
    if(mapPot<4) mapPot = 0;

    // Pot CC
    if (mapPot > lastPot + thld || mapPot < lastPot - thld){
    lastPot = mapPot;
    // Serial.print("POT: ");
    // Serial.println(mapPot);
    MIDI.sendControlChange(notePot, mapPot, channelOut);
    delay(1);
    } else { delay(1); }

    // Mod CC (TRY CHANGING DISTANCE FOR AVERAGEMOD??)
    if (distance <= maxDist ){ // within range TRY ADDING && (mapMod > lastMod + thld || mapMod < lastMod - thld)

    totalMod = totalMod - readingsMod[readIndex];
    readingsMod[readIndex] = distance;
    totalMod = totalMod + readingsMod[readIndex];
    averageMod = totalMod / numReadings;
    int mapMod = map(averageMod, 0, maxDist, 0, 127);
    if(mapMod>125) mapMod = 127;
    if(mapMod<3) mapMod = 0;

    //lastMod = mapMod;

    // Serial.print("MOD: dist ");
    // Serial.print(distance);
    // Serial.print(" | avg ");
    // Serial.print(averageMod);
    // Serial.print(" | midi ");
    // Serial.println(mapMod);
    MIDI.sendControlChange(noteMod, mapMod, channelOut);
    delay(1);
    } else {
    delay(1);
    }

    // LED for hand within range
    if(distance <= maxDist) digitalWrite(led,HIGH);
    else digitalWrite(led,LOW);

    // button states re-assign
    button1Prev = button1State;
    button2Prev = button2State;

    // move through array
    readIndex = readIndex + 1;
    if (readIndex >= numReadings) readIndex = 0;
    }