Midi notes output

Hello, I’ve found this sketch and I would like to output midi notes with an extra pot as well as the midi volume. I’m confused where to begin. Could someone please give me some pointers? thanks :slight_smile:

//#define DEBUG 1

// Constants
const int LED_PIN = 13; // LED connected to digital pin 13
const int POT_PIN = 0; // Pot connected to analog pin 0
const int POT_THRESHOLD = 7; // Threshold amount to guard against false values
const int MIDI_CHANNEL = 0; // MIDI Channel 1

#ifdef DEBUG
const int DEBUG_RATE = 9600; // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250; // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif

void setup()
{
pinMode(LED_PIN, OUTPUT); // Sets the digital pin as output
digitalWrite(LED_PIN, HIGH); // Turn the LED on
Serial.begin(SERIAL_PORT_RATE); // Starts communication with the serial port
}

void loop()
{
static int s_nLastPotValue = 0;
static int s_nLastMappedValue = 0;

int nCurrentPotValue = analogRead(POT_PIN);
if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)
return;
s_nLastPotValue = nCurrentPotValue;

int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 127); // Map the value to 0-127
if(nMappedValue == s_nLastMappedValue)
return;
s_nLastMappedValue = nMappedValue;

MidiVolume(MIDI_CHANNEL, nMappedValue);
}

void MidiVolume(byte channel, byte volume)
{
#ifdef DEBUG
Serial.println(volume, DEC);
#else
Serial.print(0xB0 | (channel & 0x0F), BYTE); // Control change command
Serial.print(0x07, BYTE); // Volume command
Serial.print(volume & 0x7F, BYTE); // Volume 0-127
#endif
}

please give me some pointers

Post code between the brackets that pop up when you click on the # icon in the reply box.

When do you want to send a note? When the note pot changes or the volume pot changes.

Let's assume it is the latter then wire up the note pot to another analogue pin say analogue 1 and replace the line:-

MidiVolume(MIDI_CHANNEL, nMappedValue);

with the following:-

byte notePotValue = map(analogRead(1),0,1023,1,127); MidiNoteOut(nMappedValue, notePotValue);

Then add at the end:-

void MidiNoteOut(byte volume, byte note)
{
#ifdef DEBUG
   Serial.print(volume, HEX);
  Serial.print("  ");
   Serial.println(note, HEX);
#else
   Serial.print(MIDI_CHANNEL | 0x90, BYTE);              // Channel 
   Serial.print(note, BYTE);
   Serial.print(volume, BYTE);
#endif
}

Thanks Grumpy Mike!!!!!!! :)