Basic question about reading incoming data in Serial Monitor

Hi there,
My apologies, as I'm sure this has been covered before in some form or another. Couldn't find any examples online that helped though.

I've got a code that allows me to send midi data from a musical keyboard into my arduino via a MIDI input jack. It then prints the data into the serial monitor. My question is in regards to organizing the data that's coming in.

It looks like I'm getting three numbers coming in at a time from my keyboard controller (status 0-255, pitch 0-127 and data 0-127)
I'd like to turn these numbers each into a separate integer that I can use in other parts of my code.

I know this is a fairly basic question, but I couldn't find any examples in the arduino library that helped with this.

Any info is greatly appreciated!

#include <AltSoftSerial.h>
AltSoftSerial midiSerial;

void setup()
{
  Serial.begin(115200);
  midiSerial.begin(31250);
}

void loop()
{
  while ( midiSerial.available() > 0 )
  {
    int incomingByte = midiSerial.read();
    midiSerial.write( incomingByte );
    Serial.println( incomingByte, DEC );
  }
}

Here's an example of my incoming data in the Serial Monitor. You can see there are 3 sets of numbers being constantly fed into the Arduino:

The first number is moving around as I move a midi fader
100
178
14
101
178
14
103
178
14
104
178
14
106
178
14
107
178
14
108
178
14
109
178
14
110
178
14
113
178
14
114
178
14
115
178
14
116
178
14
118
178
14
119
178
14
120
178
14
122
178
14
123
178
14
126
178
14
127
147
71
5
131
71
0

One of those is going up and down from 0 to 127, and I'd like to turn that into an integer. Should I use Serial.parseInt();??

Thanks for any help

i am sure others will have better knowledge, as i am looking into MIDI commands as well, to go along with my MIDI Drum kit.
i am guessing "command, Pitch, Velocity, ..."
maybe a time parameter as well...

~Travis

1 Like

this project "may" be of some help too. it is Nick Gammon's MIDI Decoder.
http://gammon.com.au/forum/?id=12746

~Travis

Each of those is an integer. Are you saying you want to merge all 3 values from a MIDI command into a single integer? What algorithm do you want to use? I mean if you have 100, 88, 63 what answer do you expect?

Steve

Right, meant to say that I want to turn the three numbers into three separate integers. Specifically, I'll only be needing one though

How do you know which is which? i.e. if there is any problem with your comms, how do you decide which number goes to which variable?

But yes, use parseint for the first try.

1 Like

I looked up using ascii tables, but I wasn't successful.

I'd like to get the data value and assign it to an integer called ""MIDIData3"

Hoping for some help. I'm scrolling through the Arduino Examples now hoping to find something. No luck so far

I added this to the end of the loop:

int MIDIData3 = Serial.parseInt();

But now it seems to only receive data once every second or so. Is that normal?

My mistake, you're reading bytes off the wire, I had though it was text. In that case, I'm not sure what you need - your code already reads each into incomingByte. What do you want to do with it?

Right now I'm getting three separate messages from my midi device all into that one "incomingByte" integer. What I'd like to do is turn these three pieces of data each into their own separate integer.

The purpose would be to control the movement of a motor with the value from one of the integers.

ce projet peut être bien utile pour les boites à musique comme l'electribe korg.

1 Like

Which one?

The one that is coming from my midi controller's "Data" control (it's the number that is moving from 0-127 in my second post). Really any one would work though.

Thanks,

  • Zach

Ahh, I figured it out on my own!

I had to add a few extra lines (4 I believe)

#include <AltSoftSerial.h>
AltSoftSerial midiSerial;

void setup() {
  Serial.begin(115200);
  midiSerial.begin(31250); }

void loop() {
  while ( midiSerial.available() > 2 ) {
    int commandByte = midiSerial.read();
    int noteByte = midiSerial.read();
    int velocityByte = midiSerial.read();
    midiSerial.write(commandByte);
    midiSerial.write(noteByte);
    midiSerial.write(velocityByte);
    Serial.println(velocityByte);  } }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.