I want to read both Velocity and Aftertouch from one FSR.
Ideally it would read around the first 80 percent of the potentiometer and map it
to Velocity 0-127 and after that (80 to 100) it would stay 127 Velocity and also output
Aftertouch where 80 to 100 Percent of the FSR gets mapped to 0 to 127 of Aftertouch.
Right now i got my code working, which reads both Values from one FSR, but i am not sure if i do it the right way, altough it works.
But it reads both Aftertouch and Velocity all the time.
How can i accomplish that?
here is my code (i used the Control Surface Library and merged some examples and already got great help from Pieter who wrote it)
it incorporates 3 mpr121 for the "keyboard" and one fsr beneath it to read velocity and aftertouch ideally.
#include <Control_Surface.h>
#include <Adafruit_MPR121.h>
USBMIDI_Interface midi;
FilteredAnalog<7> velocityPot = A0;
FilteredAnalog<7> aftertouchPot = A0;
constexpr analog_t minimumValue = 3276 ;
constexpr analog_t maximumValue = 16383 - 3276;
analog_t mappingFunction(analog_t raw) {
// make sure that the analog value is between the minimum and maximum
raw = constrain(raw, minimumValue, maximumValue);
// map the value from [minimumValue, maximumValue] to [0, 16383]
return map(raw, minimumValue, maximumValue, 10240, 16383);
// Note: 16383 = 2¹⁴ - 1 (the maximum value that can be represented by
// a 14-bit unsigned number
}
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap1 = Adafruit_MPR121();
Adafruit_MPR121 cap2 = Adafruit_MPR121();
Adafruit_MPR121 cap3 = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
uint16_t lasttouched2 = 0;
uint16_t currtouched2 = 0;
uint16_t lasttouched3 = 0;
uint16_t currtouched3 = 0;
// Instantiate a CCPotentiometer object
//CCPotentiometer potentiometer = {
// A3, // Analog pin connected to potentiometer
// {MIDI_CC::Channel_Volume, CHANNEL_1}, // Channel volume of channel 1
//};
//Instantiate a Pitchbend poti
//PBPotentiometer potentiometer = { A2,};
void setup() {
if (!cap1.begin(0x5A));
if (!cap2.begin(0x5B));
if (!cap3.begin(0x5C));
if
velocityPot.map(mappingFunction);
Control_Surface.begin();
}
void loop() {
currtouched = cap1.touched();
currtouched2 = cap2.touched();
currtouched3 = cap3.touched();
velocityPot.update();
if (aftertouchPot.update()) {
uint8_t newValue = aftertouchPot.getValue();
if (newValue > 0)
{
midi.sendCP (CHANNEL_1, newValue);
}
}
for (uint8_t i = 0; i < 12; i++) {
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
midi.sendNoteOn(i + 48, velocityPot.getValue());
}
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
midi.sendNoteOff(i + 48, 0);
}
if ((currtouched2 & _BV(i)) && !(lasttouched2 & _BV(i)) ) {
midi.sendNoteOn(i + 60, velocityPot.getValue());
}
if (!(currtouched2 & _BV(i)) && (lasttouched2 & _BV(i)) ) {
midi.sendNoteOff(i + 60, 0);
}
if ((currtouched3 & _BV(i)) && !(lasttouched3 & _BV(i)) ) {
midi.sendNoteOn(i + 72, velocityPot.getValue());
}
if (!(currtouched3 & _BV(i)) && (lasttouched3 & _BV(i)) ) {
midi.sendNoteOff(i + 72, 0);
}
}
// reset our state
lasttouched = currtouched;
lasttouched2 = currtouched2;
lasttouched3 = currtouched3;
Control_Surface.loop();
}