I’m building an MIDI to CV converter using an Arduino nano along with a MIDI input circuit, however, when uploading the code and connecting the MIDI to a laptop via USB, the Arduino doesn’t recognize any input signals. Also, I’m confused on how to code for MIDI notes if anyone can offer guidance. I’ve followed building the circuit in the following link: MIDI Input Circuit
Below is also the code I’m using:
/*
* Connections:
* (1) Midi Connector
* (2) DAC MCP4728:
* SDA pin to A4/SDA (Arduino Nano)
* SCL pin to A5/SCL (Arduino Nano)
* GND to GND
* VCC to +5V
* (3) Clock Rate Potentiometer
*
* Outputs:
* DAC OUT
* Channel A-C to Drum Kit and Channel D to VCO
* Clock output with LED
* Gate 1 and 2 with LEDs
*/
#include <MIDI.h>
#include <Wire.h> // Include the Wire to talk I2C
#include <Adafruit_MCP4728.h>
// This is the I2C address of the MCP4728
#define MCP4728_ADDR 0x64
MIDI_CREATE_DEFAULT_INSTANCE();
Adafruit_MCP4728 dac;
#define GATE1 4 // pin D4 for Gate 1
#define GATE2 3 // pin D3 for Gate 2
#define G1LED 7 // pin D7 for signaling LED of Gate 1
#define G2LED 6 // pin D6 for signaling LED of Gate 2
#define CLOCK_OUT 5 // pin D5 (PWM) for Clock Out
#define CLED 8 // pin D8 for signaling LED of Clock out
float refVoltage = 5.04; // Note cannot be higher than reference voltage
int previousNotePlayed1;
int previousNotePlayed2;
int previousNotePlayed3;
int previousNotePlayed4;
int clockRate = A0; // Potentiometer connected
int clockSpeed; // current clock speed, in milliseconds per beat
void setup() {
// put your setup code here, to run once:
pinMode(CLED, OUTPUT);
pinMode(G1LED, OUTPUT);
pinMode(G2LED, OUTPUT);
pinMode(CLOCK_OUT, OUTPUT);
pinMode(GATE1, OUTPUT);
pinMode(GATE2, OUTPUT);
MIDI.setHandleNoteOn(doThisNoteOn); // calls the function on note on
MIDI.setHandleNoteOff(doThisNoteOff); // calls the function on note off
MIDI.begin(MIDI_CHANNEL_OMNI); // Listen to all midi channels
Serial.begin(250000);
Wire.begin();
dac.begin(0x64);
} // end of setup
void loop() {
// put your main code here, to run repeatedly:
MIDI.read(); // Read incoming messages
// Read the value of the tempo potentiometer and map it to a range of reasonable clock speeds
int clockValue = analogRead(clockRate);
clockSpeed = map(clockValue, 0, 1023, 1000, 250); // tempo range from 60 BPM to 240 BPM
// send a clock pulse every clockSpeed milliseconds
static unsigned long lastClockTime = 0;
if (millis() - lastClockTime >= clockSpeed){
digitalWrite(CLOCK_OUT, HIGH);
digitalWrite(CLED, HIGH);
delayMicroseconds(clockValue); // initially had delayMicroseconds(10);
digitalWrite(CLOCK_OUT, LOW);
digitalWrite(CLED, LOW);
lastClockTime = millis();
}
} // end of loop
void doThisNoteOn(byte channel, byte note, byte velocity){
if (channel == 1){ // Listen to channel 1 to set CV1
previousNotePlayed1 = note;
float voltPerOctave = (note - 24) * 0.0833;
int dacInput = (voltPerOctave / refVoltage) * 4096;
dac.setChannelValue(MCP4728_CHANNEL_A, dacInput);
digitalWrite(GATE1, HIGH);
digitalWrite(G1LED, HIGH);
}
if (channel == 2){ // Listen to channel 2 to set CV2
previousNotePlayed2 = note;
float voltPerOctave = (note - 24) * 0.0833;
int dacInput = (voltPerOctave / refVoltage) * 4096;
dac.setChannelValue(MCP4728_CHANNEL_B, dacInput);
digitalWrite(GATE1, HIGH);
digitalWrite(G1LED, HIGH);
}
if (channel == 3){ // Listen to channel 3 to set CV3
previousNotePlayed3 = note;
float voltPerOctave = (note - 24) * 0.0833;
int dacInput = (voltPerOctave / refVoltage) * 4096;
dac.setChannelValue(MCP4728_CHANNEL_C, dacInput);
digitalWrite(GATE1, HIGH);
digitalWrite(G1LED, HIGH);
}
if (channel == 4){ // Listen to channel 4 to set CV4... this CV will be used for the VCO
previousNotePlayed4 = note;float voltPerOctave = (note - 24) * 0.0833;
int dacInput = (voltPerOctave / refVoltage) * 4096;
dac.setChannelValue(MCP4728_CHANNEL_D, dacInput);
digitalWrite(GATE2, HIGH);
digitalWrite(G2LED, HIGH);
}
} // end of doThisNoteOn
void doThisNoteOff(byte channel, byte note, byte velocity){ //Handles note off correctly for previously played noted
if (note == previousNotePlayed1){
MIDI.sendNoteOff(1, note, 0);
// velocity = 0;
digitalWrite(GATE1, LOW);
digitalWrite(G1LED, LOW);
}
if (note == previousNotePlayed2){
MIDI.sendNoteOff(2, note, 0);
// velocity = 0;
digitalWrite(GATE1, LOW);
digitalWrite(G1LED, LOW);
}
if (note == previousNotePlayed3){
MIDI.sendNoteOff(3, note, 0);
// velocity = 0;
digitalWrite(GATE1, LOW);
digitalWrite(G1LED, LOW);
}
if (note == previousNotePlayed4){
MIDI.sendNoteOff(4, note, 0);
// velocity = 0;
digitalWrite(GATE2, LOW);
digitalWrite(G2LED, LOW);
}
} // end of doThisNoteOff
Also a picture of the module: