# Integer to Byte Conversion

I am writing some code to read a value from the serial port then write a MIDI command back to the serial port to pass it into a MIDI cable.

I'm having trouble passing a numeric value (i.e. 60) to the Serial port and then re-assembling the bytes and printing it. I can convert the value to an integer but cannot seem to find the right way to do a Serial.print of the value as a byte. See code below, which doesn't work.

// Arduino code

unsigned int val =0;
boolean isDirty;
void setup()
{
Serial.begin(31250);

}

void loop()
{

while(Serial.available() >0)
{
val =10; // 810 = 80; 87 * 10 = 870; ...
val += (Serial.read() - '0'); // '8' - '0' = 8
isDirty = true;
}
if(isDirty)
{

noteOn(0x90,val,0x45);
delay(200);
noteOn(0x90,val,0x00);
val = 0;
delay(200);
isDirty = false;
}
}

void noteOn(byte cmd, int data1, byte data2) {

Serial.print(cmd, BYTE);
Serial.print(data1,BYTE); //doesn't work
// byte b = 60; //this works
// Serial.print(b,BYTE); //doesn't work
Serial.print(data2, BYTE);

}

You are not going to be able to do a Serial.write on anything bigger than a byte. If you have an int, you can write the least significant byte (LSB) and then the most significant byte (MSB), or write the MSB then the LSB.

The receiver will have to put the bytes back together as an int. The way the receiver expects to receive the bytes will define the order in which you need to send them.

You can use the lowByte and highByte functions to extract the LSB and MSB of the int.

You are not going to be able to do a Serial.write on anything bigger than a byte

?

You can write a string or a buffer of a given length with "Serial.write".

OK, so you can write an array of BYTEs, one byte at a time. That's not the same as writing a multi-byte value like an int.

See the code below as a point of contrast to clarify here. I'm sure my newness to low level programming is the cause of the confusion here but why does the code below work?

Specifically, the code has an iterator, setting a byte value = 30 and incrementing it on a loop. I'm simply trying to swap out the byte value generated in this for loop with a value coming from the serial port. Can someone explain the best way to do that?

`````` void loop() {
// play notes from F#-0 (30) to F#-5 (90):
for (note = 30; note < 90; note ++) {
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note, 0x45);
delay(100);
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note, 0x00);
delay(100);
}
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(byte cmd, byte data1, byte data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
``````

If you open the serial monitor, instead of having the midi cable plugged in, you should see data that Serial.print is sending to the serial port. (Use the rightmost button to open the serial monitor; make sure the baud rate matches that of the Serial.begin statement).

If you open the serial monitor, instead of having the midi cable plugged in, you should see data that Serial.print is sending to the serial port. (Use the rightmost button to open the serial monitor; make sure the baud rate matches that of the Serial.begin statement).

Thanks. Yes, I have that sorted out. I switch the baud back to 9600 to view the output (MIDI uses 31250). I can see that I'm re-assembling the correct integer, but when I send it to the MIDI cable as a byte (using various methods) I don't get the same result as the second code sample I attached.

Thanks for you help and prompt replies here... this is driving me a little batty.

What variable type is note?

It is a byte.

If you are writing the data to the serial port correctly, as evidenced by the correct data appearing in the serial monitor, then the problem must be that you are not writing the correct data to the serial port. That is, the midi device is expecting something other, or more, than 3 bytes.