a line of code i dont understand and wonder if i really need

for(int i=0; i<4; i++) {
Serial.print(buff*, BYTE); What does this line of code in my motordriver code mean. do i need it in there?*
here is the rest of the code that it goes with as well
buff[0]=0x80;

  • buff[1]=0x00;*
  • buff[2]=motor;*
  • buff[3]=speed;*
  • for(int i=0; i<4; i++) {*
    _ Serial.print(buff*, BYTE);*_

It looks to me like it is sending those 4 bytes "out" the serial port. Is that connected to the motor? Perhaps that is what tells it what to do.

for(int i=0; i<4; i++) {
    Serial.print(buff, BYTE);

Hopefully there is more, since you’re missing a closing squiggly-bracket.
As shown (with only an added closing bracket), it would output the first byte of the buffer (0x80) four times, which is probably not what it ought to do. Hopefully the missing code does something to advance to the other characters.

Not what I wrote - see here, reply #10:

Of course, if you were feeling adventurous, you could dispense with the for loop altogether :wink:

westfw:

for(int i=0; i<4; i++) {

Serial.print(buff, BYTE);



Hopefully there is more, since you're missing a closing squiggly-bracket.

Oh yeah well spotted. Although that does explain why the code was in italics. :wink:

Note to original poster: If you use the [ code ] tags then that particular problem goes away.

also, it is better to keep the same discussion within the original topic, rather than starting a new one...

[quote author=Nick Gammon]
If you use the [ code ] tags…[/quote]

Well, Nick got it, but for people who haven’t figured it out yet, the original code is the following:

for(int i=0; i<4; i++) {
    Serial.print(buff[i], BYTE);  What does this line of code in my motordriver code mean. do i need it in there?

To the Original Poster:

The following code assigns values to four members of the buff array:

  buff[0]=0x80;
  buff[1]=0x00;
  buff[2]=motor;
  buff[3]=speed;

The loop sends the binary values one at a time through the serial port.

  for(int i=0; i<4; i++) {
    Serial.print(buff[i], BYTE);

First time through the loop, the value of i is 0, so it sends buff[0]
Second time through the loop, the value of i is 1, so it sends buff[1]
Third time through the loop, the value of i is 2, so it sends buff[2]
Fourth time through the loop, the value of i is 3, so it sends buff[3]

After the fourth time, the value of i is 4, so it doesn’t go through again.

Now, you asked whether you need the Serial.print() line in the loop. Nick and KE7GJP are just guessing (me too), but here’s the thing:

Well, what is at the other end of the serial connection? Is it some kind of motor module (or some such thing) that needs a four-byte command? If so, then that loop (with the Serial.print() line) does the deed.
Regards,

Dave

Footnote:
Posts (and parts of posts) can be marked up to alter the appearance from the default. There are markup tags to make parts of the post appear in boldface. There are markup tags to make parts of the post to appear in italics.

Here’s an easy way to apply code tags:

In the edit window for posting, highlight the code. (Not the entire post, just the code.)
Press the “#” icon at the top of the edit window.

it puts at the beginning of the highlighted block and it puts at the end. That way, the post will show the code in a monospace font with formatting preserved. And things with [i] won’t be taken to imply the beginning of an “italics tag.”

Of course, you could type the tags manually instead of clicking an icon.

In any case…

Before you hit the “Post” button, press “Preview” to see if you got it right.

Most forums (fora for you Latin wonks) have a help page or a FAQ list that spells out the options and I would normally give a link to it. Maybe there is one here, but I don’t see it.

Well, there is a “Help” button but all it does is lead to a page that says ,“DUDE, WE ARE POPULATING OUR HELP SERVER.” (I’m not sure why they are shouting, and, furthermore, isn’t it just possible that not everyone identifies himself/herself as a DUDE?)

Oh, well…