Teensy as a touch midi controller.

Status
Not open for further replies.

pl3as3st0pm3

New member
Hello! This is my first arduino project. I decided to repeat the project of the touch midi controller on the Teensy 3.2 board, here is an example: https://www.youtube.com/watch?v=oSeH9Q4-yL8. I’ll be brief: it works, but not like in a video, where each press is processed as one, regardless of its duration, in my case, long touches sound like multiple short ones, as if I were pounding the sensor. The author of the project is not responding. Tell me what needs to be fixed in the code?

Code:
unsigned int thresh = 3000; 
unsigned int data; 
int tickTime = 100; 

byte readPins[] = {
  0, 1, 3, 4, 23, 22, 19, 18
};

byte pitches[] = {
  60, 62, 64, 65, 67, 69, 71, 72
};

byte playFlag[] = {
  0, 0, 0, 0, 0, 0, 0, 0
};

byte tick[] = {
  0, 0, 0, 0, 0, 0, 0, 0
};


void setup() {
  usbMIDI.setHandleControlChange(OnControlChange); 
  pinMode(13, OUTPUT); 
  digitalWrite(13, HIGH); 
}

void loop() {
  checkTouch(); 
  checkTick();
  usbMIDI.read(); 
}


void checkTouch() {
  for(int i = 0; i < 8; i++) {
    data = touchRead(readPins[i]); 
    if(data > thresh) {
      usbMIDI.sendControlChange(1, constrain(map(data, thresh, 65535, 0, 127), 0, 127), 1);
      if(playFlag[i] == 0) {
        usbMIDI.sendNoteOn(pitches[i], 127, 1); 
        playFlag[i] = 1;
        tick[i] = tickTime; 
      }
    }
  }
}

void checkTick() {
  for(int i = 0; i < 8; i++) {
    if(tick[i] == 0 && playFlag[i] == 1) {
      playFlag[i] = 0;
      usbMIDI.sendNoteOff(pitches[i], 0, 1); 
    }
    if(tick[i] > 0) {
      tick[i] = tick[i] - 1;
    }
  }
}

void OnControlChange(byte channel, byte controller, byte value) {
    digitalWrite(13, HIGH); 
    thresh = value * 100; 
    delay(10); 
    digitalWrite(13, LOW); 
}

Thank you in advance!
 
You've got two functions checkTouch() and checkTick()

checkTouch() looks at the values from the touch pins and turns the note on if the value is high enough, and checkTick turns the note off after 100 checks (from the value of checkTick)
ergo, a note held continuously will be played every 100 loops, at whatever length that is.

For only 8 notes, I'd suggest the best (easiet) way to do this is to store an array of which notes are on or off. In your checkTouch() function, if a note is off you should turn it on if it goes over a threhold, or if it is on check if it's under a threshold.
 
Status
Not open for further replies.
Back
Top