Hello, I am new here, looking for help. I have built a basic 5-Pin Midi output setup with the Leonardo with some buttons, potis, distance measurement, display. The hardware, except for the Midi part, is working fine, meaning I can see all the values on the display. Midi out is connected to my audio interface, which monitors to the computer. If I connect the Interface's in to my Midi-keyboards out, it monitors fine. I want to trigger synths later, the monitoring on the computer is just a test. Here is the ruff schematic, leaving some stuff out but to give you an overview:
My problem is: I can not output any Midi. I have tried using different serials. The serial is working fine for monitoring serial prints at the monitor, but tx pin with Serial2.write does not send any Midi. I have tried using the Midi Library as well as the controller.h approach but it just wont output. Since i am getting a bit frustrated, I kindly ask for help. I use Visual Studio Code, but have tried the Arduino IDE as well. The code contains some outcommented serial prints, they work fine if I do Serial.begin(115200), that is not the problem. But the outcommented Serial1.write(...) did not work. This is the code:
#include <Arduino.h>
#include <MIDI.h>
#include <U8g2lib.h>
#include <Wire.h>
#define SDA 2
#define SCL 3
#define echoTrig A5
#define echoReturn A4
#define dice1 5
#define dice2 7
#define dice1Led 4
#define dice2Led 6
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
int i,j;
int pots[4] = {A3,A2,A1,A0}; // POT pins
int buts[6] ={8,9,10,11,12,13}; // BUT pin
const int numpots = sizeof(pots)/sizeof(pots[0]); // amount of potis
const int numbuts = sizeof(buts)/sizeof(buts[0]); // amount of buttons
int potvals[numpots]; // POT value array
int butvals[numbuts]; // BUT value array
int distval; // Distance value
int yoff = 30; // screen Y-offset
int rowheight = 11; // screen row height
int xcolpots = 55; // x-pixels second column POT
int xcolbuts = 15; // x-pixels columns BUT
int midival; // MIDI value
void show(int potvals[], int butvals[], int distval){
u8g2.setFont(u8g2_font_t0_11b_tf);
u8g2.firstPage();
do {
for (i=0; i<numbuts; i++){
if(butvals[i] == 0){
u8g2.setCursor(2 + xcolbuts*i, yoff + numpots/2 * rowheight);
u8g2.print(i+1);
// Serial.print("But "); Serial.print (i+1);
// Serial.print(" / Value: "); Serial.print(butvals[i]);
// Serial.print(" | ");
// Serial.println();
}
}
j = 0;
for (i=0; i<numpots/2; i++){
u8g2.setCursor(2, yoff + i*rowheight);
u8g2.print(F("P"));
u8g2.setCursor(8, yoff + i*rowheight);
u8g2.print(j+1);
u8g2.setCursor(23, yoff + i*rowheight);
u8g2.print(potvals[j]); j++;
// Serial.print("Pot "); Serial.print (j+1);
// Serial.print(" / Value: "); Serial.print(potvals[j]);
// Serial.print(" | ");
u8g2.setCursor(2+xcolpots, yoff + i*rowheight);
u8g2.print(F("P"));
u8g2.setCursor(8+xcolpots, yoff + i*rowheight);
u8g2.print(j+1);
u8g2.setCursor(23+xcolpots, yoff + i*rowheight);
u8g2.print(potvals[j]); j++;
// Serial.print("Pot "); Serial.print (j+1);
// Serial.print(" / Value: "); Serial.print(potvals[j]);
// Serial.print(" | ");
// Serial.println();
}
if(distval < 50){
u8g2.setCursor(2, yoff - rowheight);
u8g2.print(F("DIS"));
u8g2.setCursor(23, yoff - rowheight);
u8g2.print(distval);
}
} while( u8g2.nextPage() );
}
void potbuts(int potvals[], int butvals[]){
for (i = 0; i<numpots; i++){
int tmp = analogRead(pots[i]);
midival = map(tmp,13,1020,0,127);
// Serial.print("Pot "); Serial.print (i+1);
// Serial.print(" / Value: "); Serial.print(midival);
// Serial.print(" | ");
potvals[i] = midival;
}
for (i = 0; i<numbuts; i++){
int butvalue = digitalRead(buts[i]);
butvals[i] = butvalue;
if(butvalue == 0){
int note = 60;
int velo = 100;
int chan = 1;
MIDI.sendNoteOn(note, velo, chan);
// Serial1.write(144); //noteOn channel 1
// Serial1.write(42); //pitch
// Serial1.write(127); //velocity
TXLED1;
//show(potvals, butvals, distval);
digitalWrite(dice2Led, HIGH);
delay(100);
TXLED0;
MIDI.sendNoteOff(note, 0, chan);
//note OFF
// Serial1.write(144);
// Serial1.write(42);
// Serial1.write(0);
digitalWrite(dice2Led, LOW);
}
}
}
// DISTANCE MEASUREMENT
void distance(int *distval){
long duration;
digitalWrite(echoTrig, LOW);
delayMicroseconds(2);
digitalWrite(echoTrig, HIGH);
delayMicroseconds(10);
digitalWrite(echoTrig, LOW);
duration = pulseIn(echoReturn, HIGH);
*distval = (duration/2) / 29.1;
}
//SETUP
void setup(){
u8g2.begin();
u8g2.setFlipMode(0);
MIDI.begin(MIDI_CHANNEL_OFF);
// Serial.begin(115200);
// Serial1.begin(31250);
// while (!Serial1) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
pinMode(SDA, OUTPUT);
pinMode(SCL, OUTPUT);
for (int i = 0; i<4; i++)
{
pinMode(pots[i], INPUT_PULLUP);
}
for (int i = 0; i<6; i++)
{
pinMode(buts[i], INPUT_PULLUP);
}
pinMode(echoTrig, OUTPUT);
pinMode(echoReturn, INPUT);
pinMode(dice1, INPUT);
pinMode(dice2, INPUT);
pinMode(dice1Led, OUTPUT);
pinMode(dice2Led, OUTPUT);
}
//LOOOP
void loop(){
potbuts(potvals, butvals);
distance(&distval);
show(potvals, butvals, distval);
}
It just outputs the values of potis, knobs and distance on the display. It should just send a MIDI.noteOn if a button is pressed for testing.
Any help is appreciated. Thank you very much.
