Pages: [1] 2   Go Down
Author Topic: help needed on bit of code, please, anyone????  (Read 1215 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

change
Code:
Serial.print(whatever, BYTE);
to
Code:
Serial.write(whatever);

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Serial.print(cmd);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 600
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so doing this instead will work fine?
No. Serial.print() and Serial.write() send data in completely different ways.

Code:
byte val = 243;
Serial.print(val);
will send '2', '4', and '3' to the serial port.

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 600
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Tucson, AZ
Offline Offline
Sr. Member
****
Karma: 8
Posts: 311
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should look like this:
 Serial.write(cmd);
 Serial.write(data1);
 Serial.write(data2);

etc...
« Last Edit: May 04, 2012, 09:21:27 pm by kd7eir » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 600
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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????????
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7164
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 284
Posts: 25584
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 600
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: