Receiving partial messages when using VirtualWire and ServoTimer2 I'm writing a project where an Arduino board that is connected to a servo engine receives commands from another Arduino. The possible commands are:


Move 10 degrees
Reset position
Sweep until you're told to stop


My problem is with the sweep-mode, when I get the sweep command, I get into a while loop, that first checks for a sweep-terminator and then sweeps.

} else if (message == sweep) {
  Serial.println("sweep");
  while (msg1 != sweep_term) {
    // set the direction for the servo to move
    if (val == 750){
      val = 2250;
    } else {
      val = 750;
    }

    // move the servo
    myservo.write(val);
    // wait for the servo to move
    delay(15);

    // wait for a message
    if (vw_get_message(buf1, &amp;buflen1)) {
      buf1[buflen1] = '\0';
      msg1 = (char*)buf1;
      memset(buf1,'0',VW_MAX_MESSAGE_LEN);
      Serial.print(buflen1);
      Serial.print(" [sweep] Got: ");
      Serial.println(msg1);
    }
  }
}


For some reason, when I'm in this loop I don't get the sweep terminator message, or I get a part of it. I suspect that the servo is responsible for this, since when I run the same program without myservo.write()s I do receive those terminators.
If msg1 is of type char* (as is implied by the cast in msg1 = (char*)buf1;), then it seems to me that that statement will copy the address of buf1 to msg1.  Then, when you fill buf1 with ascii zeroes (ie, characters '0') msg1 ends up being a string of ascii zeroes.

On the other hand, if msg1 is of type String (to go along with your comment about buf1 being of type String) the cast should be unnecessary.

Note, if you don't give the source for the vw_get_message() routine and the declarations for all of the constants and variables used in the code extract, it's unlikely anyone can definitively answer your question.

Note, the while test, msg1 != sweep_term, may not be well-defined on entry to the while loop.  Or, if msg1 is left over from a previous use, you won't enter the loop at all.  Consider writing the loop in the form do { ... } while (msg1 != sweep_term); instead.

Note, if (as said in a comment) buf1 is of type String, then the statement memset(buf1,'0',VW_MAX_MESSAGE_LEN); is a line of code I'd recommend against, because it treats buf1 as a simple array rather than as an object.  (In this case it should make no difference, but it requires some research to be sure of that. [1,2,3,4,5])