Hold all "millis()" values in an array?

Hi Folks,

I’m working on a project where I could have up to six devices connected to an arduino, it might be the case that not all of them will be attached at the same time, and all of them need to be able to receive PWM signals but without using delay().

There are a number of different PWM patterns already programmed into the code, what I’d like to do is be able to assign more than one device to a single pattern, and start/stop the patterns at different times to different devices.

I have a python script (and soon an Android app) that sends a message in the following format to the device:

<int(pin_number), string(pattern_name), int(highest_pwm_value 0 - 255), int(delay_between_pulses_in_seconds)>

Is there some way of coding something like the following, and if so, how would I go about it?

array timings[];

void fade(unsigned long thisMillis, int fadeInterval, int maxPWM, int motor_pin) {
  if (thisMillis - timings[motor_pin] >= fadeInterval) {
    // yup, it's time!
    if (fadeDirection == 1) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM) {
        // At max, limit and change direction
        fadeValue = maxPWM;
        fadeDirection = 0;
      }
    } else {
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit and change direction
        fadeValue = minPWM;
        fadeDirection = 1;
      }
    }
    // Only need to update when it changes
    analogWrite(motor_pin, fadeValue);

    // reset millis for the next iteration (fade timer only)
    timings[motor_pin] = thisMillis;
  }
}

The bit that I’m unsure about is how the array should be defined etc, as I’m primarily a python developer so it’s rare I have to explicitly set the type of an item in an array!

An array of structs is best for this situation

struct motor_data {

   int id;
   unsigned long start_time;   //store time stamp here
   unsigned long duration;      //time ends
   ...

};



motor_data motors[6] //declaration

motors[0].start_time = millis(); //accessing

ok, and I could then store pin numbers and all sorts in there as well, right?

So motors[motor_pin].start_time would hold the start millis, motors[motor_pin].current_pattern could hold the pattern I last assigned etc?

If that's correct, then the size of the logic in my code just got substantially smaller! :smiley:

yes that's the beauty of struct. You could wrap all relevant data into one "box"

arduino_new:
An array of structs is best for this situation

Also very handy for something unrelated I was trying, karma++.

LesserMole:
Also very handy for something unrelated I was trying, karma++.

Thank you!

motors[motor_pin].current_pattern could hold the pattern I last assigned etc?

I’m really struggling to see a relationship between a motor and a pattern. What the heck is a “PWM pattern” anyway?

PaulS:
I’m really struggling to see a relationship between a motor and a pattern. What the heck is a “PWM pattern” anyway?

Bad phrasing on my part, and literal interpretation on yours I suspect! :stuck_out_tongue:

I have a number of motors connected to an arduino that are controlled via PWM signals sent to a custom PCB using a N2222 transistor. I also have a number of functions within my code such as the “fade” example I posted above which can be selected by the user via the python script and an iteration over the Serial Input on the arduino to send a preset “pattern” of PWM pulses to the control board.

So the “patterns” dictate the length of the pulse and how often those pulses happen - in the “fade” example, the motor accelerates from 0 to and back again over milliseconds. PWM Patterns was just the simplest way I could think of describing it, and given the response, it seems to have been pretty successful! :smiley: