In the meantime I worked on the remainder of the code. I got pitch bend and reset 0 point to work.
Here's the code in its entirety
#include <MIDI.h>
# define SWITCH 7 // switch for noteOn
# define PITCHADJUST 8 // switch to adjust pitch
# define PITCHINPUT 1 // 10 turn pot for pitch bend
int val1 = 0;
int val2 = 0;
int DATA3 = 0;
int noteCounter = 0;
int MIDI_ROOT_NOTE = 60;
int PITCHADJUST_RANGE = 0;
int PITCH_RANGE = 24;
byte LowerBits;
byte UpperBits;
int SensorPin = 0; // Connect the pressure sensor to A0
int sensorValue;
int MidiValue = 0; // volume Value
int Old_midiValue;
int vol07 = 0;
int vol39 = 0;
int cc7 = 7;
int cc39 = 39;
float MIDIPITCHSCALE = 0.03785;
MIDI_CREATE_DEFAULT_INSTANCE();
void SendMIDI (char cmd, char data1, char DATA2) {
Serial.write(byte (cmd));
Serial.write(byte (data1));
Serial.write(byte (DATA2));
}
void control () {
val1 = digitalRead (SWITCH);
if (val1 == HIGH) {
noteCounter = noteCounter + 1;
if (noteCounter == 1) {
SendMIDI(0x90, MIDI_ROOT_NOTE, 127);
}
}
else {
SendMIDI(0x80, MIDI_ROOT_NOTE, 127);
noteCounter = 0;
}
}
void volume () {
sensorValue = analogRead (SensorPin);
MidiValue = sensorValue * (16383 / 1023);
if (MidiValue > 16383){
MidiValue = 16383;
}
if (MidiValue < 0){
MidiValue = 0;
}
vol07 = (byte)(MidiValue >> 7);
vol39 = (byte)(MidiValue & 0x7F);
SendMIDI(0xB0, cc7, vol07);
SendMIDI(0xB0, cc39, vol39);
}
void ProcessAnalogValue (byte) {
float _x = Z(1);
val2 = digitalRead(PITCHADJUST);
if (val2 == HIGH) {
DATA3 = analogRead(1);
PITCHADJUST_RANGE = 608 - DATA3;
}
int _Converted = (int) ((_x - 297 + PITCHADJUST_RANGE) / MIDIPITCHSCALE);
if (_Converted > 16383) {
_Converted = 16383;
}
if (_Converted < 0 ) {
_Converted = 0;
}
LowerBits = (byte)(_Converted & 0x7F);
_Converted >>= 7;
UpperBits = (byte)(_Converted & 0x7F);
SendMIDI(0xE0, LowerBits, UpperBits);
}
float Z (byte PIN)
{
int tmp;
int data1 = 0;
int DATA2 = 0;
int summary = 0; // summary of input data
int h;
// Get average data from Analogpin1
for (h = 0; h < 10; h ++) {
data1 = analogRead (PITCHINPUT);
summary = summary + (data1);
}
tmp = summary / 10;
return (float) tmp;
}
void setup() {
MIDI.begin(1);
Serial.begin(115200);
SendMIDI (0xB0, 0x06, PITCH_RANGE);
pinMode (PITCHADJUST, INPUT);
pinMode (SWITCH, INPUT);
}
void loop() {
volume();
control();
ProcessAnalogValue(PITCHINPUT);
delay (1);
}
Credit for the code goes to Mitshusi Abe, I only tinkered with it to get it working.