Go Down

Topic: help needed on bit of code, please, anyone???? (Read 1 time) previous topic - next topic

marcjwebb

here is the line of code :   Serial.print(cmd, BYTE);

i just wanted to know that if i take out the word Byte, like this - Serial.print(cmd);  will it work the same????


first line from the section of code that is:
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
}

WizenedEE

change
Code: [Select]

Serial.print(whatever, BYTE);

to
Code: [Select]

Serial.write(whatever);


Serial.print(whatever) will convert your number to letters representing the number and then print it.

marcjwebb

so doing this instead will work fine?? and same for the others with "Byte" too???

Serial.print(cmd);

PaulS

Quote
so doing this instead will work fine?

No. Serial.print() and Serial.write() send data in completely different ways.

Code: [Select]
byte val = 243;
Serial.print(val);

will send '2', '4', and '3' to the serial port.

Code: [Select]
Serial.write(val);
will send 243 to the serial port.

marcjwebb

ok thats cool, so how would you go about this section with "Byte" in a number of lines:

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);

PaulS

Quote
so how would you go about this section with "Byte" in a number of lines:

Replace them with Serial.write() just like you've been told at least three times now.

kd7eir

#6
May 05, 2012, 04:03 am Last Edit: May 05, 2012, 04:21 am by kd7eir Reason: 1
It should look like this:
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);

etc...

marcjwebb

Ok see this is where Pauls doesn't make sense.  In one reply you put that serial.print and serial.write are two different things so if seems odd to the replace print with write. Or is this because I am new and they actually do the same thing. Also I changed a line of code whereby I deleted the word byte from anywhere and the code compiles. So any guidance as to why ?


PaulS

Quote
In one reply you put that serial.print and serial.write are two different things

They are, and I explained why.

Quote
so if seems odd to the replace print with write.

What is odd was that print() used to be used to send binary data by using the optional second argument of BYTE. That was not obvious from the name of the function, nor was the BYTE name consistent with the other uses of the second argument, which was, and still is, to define the base (binary, decimal, octal, hex, etc.) to be used when converting the value to a string to be sent.

As a result of the inconsistency, and since there was a perfectly usable method to send binary data already, the BYTE keyword was removed. Now, you must use the correct function to send binary data.

Nothing confusing about it now.

Quote
Also I changed a line of code whereby I deleted the word byte from anywhere and the code compiles. So any guidance as to why ?

Reread all the answers you have been given, in light of what is above. If you still don't understand that just removing  the BYTE keyword is wrong, I don't think we will be able to convince you.

If you simply run your code, you will see that it doesn't work. Maybe that will convince you.

marcjwebb

ok so here i have changed as you have mentioned to do, however if serial.write and serial.print do different things i dont understand why you have said that i need to change it to this:

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.write(cmd);
  Serial.write(data1);

as aposed to this:

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);


marcjwebb

here is the code as its original state:

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
}

and i have changed it to this:

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.write(cmd);
  Serial.write(data1);
}

i have changed it as it says "Byte does not name a...." with the version 1.0

so now the one above should work fine and get round the Byte thing????????

liudr

So why this new thread?! Aren't you getting help on the other thread? Have you read the sticky thread the forum admin pointed to you yet? Besides, your question is very vague. I read it several times and couldn't understand what the problem is.

marcjwebb

basically no i have had little help but have had a few people just being awkward. my original code was -
// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);

but was having problems with the sections that have Byte at the end of the line, so put it on here (by the way have not used arduino before or posted anything on here before) and was said that the sections with byte in needed to be changed to serial.write instead of serial.print and discard the Byte

can you help shed some some light here?
this is what i have changed it to -

// Send a MIDI Command with 3 parts
void midiCmd(char cmd, char data1, char data2) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  lastCmd[3] = data2;
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}
// Send a MIDI Command with 2 parts
void midiCmdShort(char cmd, char data1) {
  lastCmd[1] = cmd;
  lastCmd[2] = data1;
  Serial.write(cmd);
  Serial.write(data1);
}

AWOL

I've merged these two threads so that people who have taken the time and effort to reply don't feel they've wasted their time.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I've merged these two threads so that people who have taken the time and effort to reply don't feel they've wasted their time.

I don't think that worked. I've taken the time and effort to explain the Serial.print() and Serial.write() nuances, and explained how to convert the 0022 code to 1.0 code, and why those changes are necessary. I still feel like I wasted my time.

Go Up