Interfacing MIDI, reading, and Switching, noobie

Hello there,

I am new to the arduino programming community, but i have 3 projects in mind that would like some help with writing these programs.

the first program reads a midi input, and displays the 3 bytes on an LCD

the second program reads midi program changes and CC messages and changes relays, the third program functions similarly.

some parts of the programs i can take from tom scarff's source codes for drum controls, and the 13 switch CC controller.

My first task is to write the first program, so the flow is that the chip reads the 3 bytes of midi, and sends the to display on the LCD (sort of midi message tester). So, first questions, serial.begin() commands for midi and LCD are different, so should i at first initialize it for MIDI, read the messages and then initialize LCD and Display? (will post my syntax in a bit.

Thank you!

serial.begin() commands for midi and LCD are different,

If you are using a serial LCD then you can't use MIDI as well. unless you:- 1) Use an arduino mega as that has 4 serial ports 2) Use the software serial library to connect your serial LCD 3) Use a parallel LCD so you don't need more than one port. See the MIDI section of:-

so using the 4bit LCD library i can use a midi serial input and write it out to the LCD right?

My rough idea for the flow is have a momenatry switch on one of the output pins to ground, so that at first the chip scans the first input byte and displays it on the LCD, then waits for the button to be pressed before showing the next byte; but to do this, should i first make the chip scan all the 3 bytes in a row? or can i do it one by one? (meaning scan once, then wait until button is pressed and scan again?)

Thank you!

Serial data is kept in a buffer. Bytes are added at the end, and removed from the front. So, it doesn't matter whether you read one byte and do something with it, then read another and process it, then read the third byte and process it, or if you read three bytes, and process them.

If your sending MIDI data to the Arduino though, the buffer is going to fill up pretty quick if you are only processing a byte when a button is pushed, unless you're pushing the button pretty quickly.

ok i got my lcd parts (need to assemble after school today), but here is my preliminary syntax for reading the 3 midi bytes.

My output to the lcd is as integers, but i know there is an error in the lcd.printIn command because it wont compile. at the moment i just want the lcd to print out the byte in an integer of binary form, or what not, can anyone help with this part? later i want to actually assaing names to the different midi messages but that will be done through switch cases.

*note half of the initialized bytes are not used as they were copied form Tom Scarff’s code for a midi analyzer…

//example use of LCD4Bit library

#include <LCD4Bit.h> 
//create object to control an LCD.  
//number of lines in display=1
LCD4Bit lcd = LCD4Bit(1); 

//some messages to display on the LCD
char msgs[8][15] = {"Waiting", "Note_on", "Note_off", "Poly_Key_Pres", "Control_Change", "Program_Change", "Channel_Pres", "Pitch_bend"};

byte DEBUG=1;
byte LedPin =13;
byte midiByte;
byte channel=0;
byte velocity;
byte val;
byte velocityFlag=1;
byte noteOnFlag=0;
byte noteFlag=0;
byte statusTest;
byte channelTest;
byte status;
byte selectStatus;
byte Flag=0;
byte x;
byte midibyte1;
byte midibyte2;
byte midibyte3;

void setup() { 
  pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
  pinMode(2,INPUT); // switch status/channel
  digitalWrite(2, HIGH); // turn on pullup resistor
  lcd.init(); // initiallize LCD

   for (x=1; x<=4; x++){
    digitalWrite( LedPin, HIGH );
    digitalWrite( LedPin, LOW );

  Serial.begin(31250);   // set MIDI baud rate


void loop() {  

  while(Serial.available <= 0) {

      lcd.printIn(midibyte1);      //prints first byte (command MSG)
  //print something on the display's second line. 

  lcd.cursorTo(2, 0);              //line=2, x=0.
                          //lcd.printIn("Score: 6/7");

      lcd.printIn(midibyte2);      //Prints second Byte
      lcd.printIn(midibyte3);      //prints third byte

  lcd.printIn("Done reading");
  digitalWrite(13, LOW);
  //print some dots individually
  for (int i=0; i<3; i++){

  //scroll entire display 20 chars to left, delaying 50ms each inc
  lcd.leftScroll(20, 50);


void readMidi(){

  while (Flag==0){
    if (Serial.available() > 0) {  
      midiByte =;



while(Serial.available <= 0) {

needs to be

while(Serial.available() <= 0) {

Change all the lcd.printIn calls to lcd.print, and the program will then compile. But, it may not function correctly.

digitalRead is used to read data from a digital pin. You have 3 calls to digitalRead after the call to Serial.available. I thought you were trying to read serial data. You’d do that with a call to, which returns a -1 if no data is available.

Oh forgot to say, I plan on using a momentary push button switch at digital pin 2 that will hold each midi message on the LCD board until pressed, thus the code for digital read at pin 2

ok so i'm trying to get my LCD to work, i'm using a LMB168NFC for which i found the spec sheet here:

wired like so:

and set up using these instructions:

after i uploaded the program, only one half of the lcd charaters a lit up (blocks) and i get on message, i did edit the cpp file for the lcd.h library, any help?

I had a problem like this with my LCD, a few of the pins were not the same as in that tutorial, it hink the enable and R/S or R/W were different. have a look at the pinouts for your LCD and make sure they are correct.

also the sample code for the 4 bit LCD did not (the banana one with the second line uncommented) work for me either

When i trid this one (dont worry about the LM35, just leave the analog open and you will get random temperatures)

When i just copied and pasted this code and ran it it worked perfectly. so now i base my 4 bit display on that method of changing lines.