Best method for timing/controlling a pattern of outputs

This is a somewhat general Arduino programming question with a specific project context. I'm quite comfortable with OO languages like Processing/Java, but I'm not quite as well-rounded with C/C++ programming and don't quite know how to tackle this (probably) simple question.

Let's say we have a button and a motor: the user presses a button and the motor goes on/off in several short pulses, then stops. This could be done very easily with delay() or, with a bit more code, using millis(). That's pretty easy but when one adds several buttons and several pulsing motors, the code gets messy very quickly. I'd especially like to be able to create different pulsing patterns (long, short, long, long, etc) which could be stored in an array - which makes using multiple instances of millis() even messier.

I'm curious if there's a cleaner and more re-usable way to handle this kind of thing? Ideally, a button would be pressed, the sequence of pulses would be triggered, but the pulse timing and ending code would be handled in some kind of function.

I'd especially like to be able to create different pulsing patterns (long, short, long, long, etc) which could be stored in an array - which makes using multiple instances of millis() even messier.

Why? Using arrays of off and on times would me the code cleaner, in my mind. Having a function that deals with a pattern, and hiding all the code that deals with the pattern in that function, seems pretty easy.

@PaulS: an array would be cleaner than other means of hard-coded values, yes. I guess I'm more interested in handling the timing issue - would millis() be my best option, or would threading or some other method be cleaner.

A current working version, which is fairly tidy for one motor:

int outputPin = 9;
int waveform[] = {
  500, 500, 1000, 1000, 250, 250 };
int waveformLen = 6;
int index = 0;
long prevMillis = 0;

void setup() {
  pinMode(outputPin, OUTPUT);
}

void loop() {
  if (millis() > prevMillis + waveform[index]) {
    if (index % 2 == 0) {
      digitalWrite(outputPin, HIGH);
    }
    else {
      digitalWrite(outputPin, LOW);
    }

    prevMillis = millis();
    index += 1;
    if (index > waveformLen-1) {
      index = 0;
    }
  }  
}

This is ok for just one instance, but if there are more motors the loop gets cluttered very fast.

I'm not sure if this concept matches your requirement.

I have a simple piece of code triggered by a timer interrupt. At each interrupt the code toggles a pin and then picks the next value from an array (looping around to the start when necessary) and puts that value in the timer as the next time interval. There are 3 lines of code in the interrupt routine.

Separately my "loop()" can change a value in the array if a different time duration is needed.

This is used to generate a PPM signal to drive an R/C transmitter.

...R

@Robin2: sounds interesting - do you have the code somewhere I could take a look?

You should be able to download it from Reply #7 on this page http://arduino.cc/forum/index.php/topic,163199.msg1220724.html#msg1220724

…R

I think you did a pretty good job on the single motor version. If you make a two dimensional array for the wave forms, and single dimension arrays for all the other global variables, you could do it without it getting too messy. One for loop nesting and a bunch of indices, and you'll be good to go.