This is how I did it in an old Arduino project (OscDigiSeq - Arduino CV/Gate Sequencer):
// convert note number to DAC mV’s
// DAC outputs from 0 to Vcc pin voltage (+5V)
// We have 1V/Oct, so 5 octaves
int midiToCV(byte pitch) {
float voltsPerNote = 0.0833; // 1/12 V
float mV; // from 0 to 0xFFF (4096);mV = 1000 * (pitch * voltsPerNote);
return (constrain(map(mV, 0, 5000, 0, 4095), 0, 4095));
}
I don’t remember why I had to put that constrain() function on the map() result, I probably borrowed that from somewhere!
Reference to the map() function including implementation: https://www.arduino.cc/reference/en/language/functions/math/map/.