Converting MIDI notes to Characters?(simple array)

Hello all,
In my current project I'm using an LCD Screen, and buttons which change the scale/notes of a MIDI instrument.

I currently have the LCD displaying those, but the notes are displayed as their corresponding MIDI number (e.g. Middle C is showing as 60, C# is showing as 61).

I want to create an array that will assign the MIDI note numbers to their corresponding letter, so I can display the note letter on the LCD, instead of the number. Any ideas on how to write this array? I believe it may be quite simple but I wouldn't know the logic to do it.
Any help is appreciated.

Are the Midi note numbers in a contiguous range and if so what is that range ?

Any ideas on how to write this array?

It's actually a 2D array. You need an array of strings, not an array of chars.

char *notes[] = {"C", "C#", /* the rest of the strings go here */ };

Then, whenever you get a note number, subtract 60, and use the result as an index into the array.

This assumes that the lowest note number you see is 60. If there are lower numbers, replace 60 with the lowest number, and make sure that the notes array defines the strings in order starting at that lowest value.

char notes[] = {"C", "C#", / the rest of the strings go here */ };

So would I declare this as an int before setup? To be honest I haven't played with arrays much before.

would I declare this as an int before setup?

No, you declare it as Paul suggested. Do that before setup() so that the array is global but it is an array of pointers to strings not an array of ints.

You did not answer my question in reply #1

UKHeliBob:
No, you declare it as Paul suggested. Do that before setup() so that the array is global but it is an array of pointers to strings not an array of ints.

You did not answer my question in reply #1

My range is from C0 to G10, so 0-127, the full range. The up/down buttons change the root note of the "scale" up or down a semitone (++/-- 1). I just need to assign the MIDI note numbers to the corresponding note char, so it will display on the LCD

so 0-127

So you need an array as suggested by Paul containing 128 entries, one per note. When you want to print the note name then you do

lcd.print(notes[noteNumber]);

UKHeliBob:
So you need an array as suggested by Paul containing 128 entries, one per note. When you want to print the note name then you do

lcd.print(notes[noteNumber]);

So does this mean my array will look like;

char noteNames[128] = {"C1", "C#1","D1",D#1","E1" }; etc, to include every note?

Also as i didn't mention before, I have 13 keys(notes) per scale

Not quite like that (check Paul's post carefully) but in principle, yes.

I am far from musical, but I assume that the note names repeat in the sequence. If so then you don't actually need 128 entries. How often do the names repeat ?

UKHeliBob:
Not quite like that (check Paul's post carefully) but in principle, yes.

I am far from musical, but I assume that the note names repeat in the sequence. If so then you don't actually need 128 entries. How often do the names repeat ?

I guess they don't repeat at all in theory. Across the 128 notes, You have about 10 octaves, so example, there will be 10 C's, but I wanted to display C5/C4/C3 depending on which C note it is, instead of just C.
So I guess I need to enter from C0-G10.

For now, I will probably stick to just A-G# (12 entries) to get it working though.

Have a look at this

char * noteNames[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

void setup()
{
  Serial.begin(115200);
  for (int noteNumber = 0; noteNumber < 128; noteNumber++)
  {
    byte octave = noteNumber / 12;
    byte noteInOctave = noteNumber % 12;
    Serial.print(octave + 1);
    Serial.println(noteNames[noteInOctave]);
  }
  Serial.println();
}

void loop()
{
}

I have probably abused the musical notation system but it should give you some ideas

Honestly 1 Array of 12 const Strings, with the octave nr added behind it just before display should suffice.

Are the Midi note numbers in a contiguous range and if so what is that range ?

looking at the chart midi note '0' = c-1 so

displayNoteNames=noteString[midinote%12]+String((midiNote/12)-1,DEC);

should result in the desired result.

Thanks for your help guys,

To be honest I'm still not fully understanding everything enough to implement it, but I guess its my job to study and figure it out :slight_smile:

For the record, is there a more appropriate/relevant place for MIDI/Music Arduino posts in particular?