Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Touchsensors + usbMIDI

  1. #1
    Junior Member
    Join Date
    May 2017
    Posts
    7

    Touchsensors + usbMIDI

    Hi,
    I'm working on a project with the Teensy 3.2. I want to build a Midi controller with touchsensors using the internal touchpins and usbMidi. But I have two questions/problems.

    I have started writing a code (see below) but have the problem that the note keeps on playing repeatedly when I keep my finger on the sensor. How do I make it play only once and in the rhythm I tap the sensor. My second question is, how would I implement variable velocity. So it reacts to the way I play (how hard/fast I touch the sensor). I thought of using the threshold for that, but I guess thats not the right way.


    const int channel = 1;
    void setup() {
    Serial.begin(9600);
    }

    void loop() {

    if (touchRead(1) > 1000) {
    usbMIDI.sendNoteOn (40, 99, channel);
    }
    if (touchRead(1) < 1000) {
    usbMIDI.sendNoteOff (40, 0, channel);
    }

    Serial.println(touchRead(1));
    while (usbMIDI.read()){}
    delay(5);
    }

    Appreciate any help or advice
    Attached Files Attached Files
    Last edited by Indisch; 12-17-2017 at 02:10 PM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,228
    You need to add a variable to remember if you've already sent note-on. When it's not, do the first part where you check if it should turn on. When it is on, do the second part where to check if it should turn off. The idea is you do not want to be checking if the note should turn on when it's already on.

    Kinda like this:

    Code:
    const int channel = 1;
    bool noteIsOn = false;
    
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
    
      if (noteIsOn == false) {
        if (touchRead(1) > 1100) {
          usbMIDI.sendNoteOn (40, 99, channel);
          noteIsOn = true;
        }
      } else {
        if (touchRead(1) < 900) {
          usbMIDI.sendNoteOff (40, 0, channel);
          noteIsOn = false;
        }
      }
      Serial.println(touchRead(1));
      while (usbMIDI.read()) {}
      delay(5);
    }
    It also helps to use 2 different thresholds, where the on threshold is slightly higher. That way the note won't rapidly turn on & off if the signal is right at the threshold and has a small amount of noise. In this code, I changed 1000 to 1100 and 900. You might wish to adjust as you like.

  3. #3
    Junior Member
    Join Date
    May 2017
    Posts
    7
    Thanks for your quick repsonse. Thats a simple adjustment which makes a lot of sense. I guess the part with the variable velocity is a "bit" more complicated?

  4. #4
    Junior Member
    Join Date
    May 2017
    Posts
    7
    Hi again,
    wanted to ask if someone could tell which MIDI message to use to send "Start/Pause", "Stop" and "Record", when a certrain Button is pressed. So I can record using a DAW without having to hit record on the computer.

    Thanks for any help...

  5. #5
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Vancouver Canada
    Posts
    621
    They are a type of real-time sysex message: https://en.wikipedia.org/wiki/MIDI_Machine_Control

    Or you may be able to use MIDI CC messages if your DAW allows MIDI learn for transport control.

  6. #6
    Junior Member
    Join Date
    May 2017
    Posts
    7
    Thanks for the response. I'm still a bit confused how to implement it as a code. As an example, if I want to send "Play" with a button on Pin 1 which is set up with a Pull-Up resistor:

    if (digitalRead(1) == LOW) //Button pressed
    {
    usbMIDI.sendSysEx(length, array); //length = 1538 for "Play", array = ???
    }

    Would length be 1538 (in hexadecimal 06 02: 06 for sub-ID 1 and 02 for sub-ID 2)? What would the array be?

    As a DAW I use Logic 7, don't know if I can do it using the CC.

  7. #7
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Vancouver Canada
    Posts
    621
    F0 7F 7F 06 01 F7 is how I read that.
    Which makes the length 6.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •