Ah, ok.
In this case, have a set of dance patterns and check before each step has arrived. In case of a message, set the dance pattern pointer to the start of the new pattern. After this, take the next step in the pattern. Your loop could look something like this:
void loop() {
static unsigned long laststeptime = 0L;
// Read serial if soemthing came in
while (Serial.available () > 0) {
incomingbuffer[nextfree++] = Serial.read();
// Check here if you have a full message yet
if (message is complete) {
patternpointer = ... set new dance pattern ...
nextfree = 0; // Reset input buffer;
}
// Check if you can take the next dance step
unsigned long curtime = millis();
if (curtime - laststeptime > patternpointer->delay) {
// We have waited long enough, take next step
myservo.write (patternpointer->position);
if (... have we reached the last step in the pattern? ...) {
// Reset patternpointer to the start to loop current pattern
patternpointer = startofpattern;
}
else {
// Increment pattern pointer to next step
patternpointer++;
}
// Reset timer for this step
laststeptime = curtime;
}
}
The pseudo-code assumes you have a fixed collection of pattern which are structures with a delay and position. How you organise that depends on what you really need, this is more to illustrate how you do this in one loop.
Korman