0

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!

2
  • 1
    unsigned long timings[SOME_VALUE]; – user31481 Feb 15 '18 at 10:24
  • that actually already looks about right. – dandavis Feb 15 '18 at 12:36
1

This is a classic example where you want to create a class. A class will contain all the control logic for dealing with one pin - all the functions, all the variables, etc.

Then you just create as many instances of the class as you need for your different pins. Each one is completely separate from the other, and each one can be controlled at will.

For instance, you might make something like this (untested):

class FaderPin {

    private:
        uint32_t timestamp;
        int motor_pin;
        int fadeInterval;
        int minPWM;
        int maxPWM;
        int fadeDirection;
        int fadeValue;

    public:
        FaderPin(int pin, int minp, int maxp, int interval) {
            motor_pin = pin;
            minPWM = minp;
            maxPWM = maxp;
            fadeInterval = interval;
            fadeDirection = 1;
            fadeValue = minp;
            timestamp = millis();
        }

        void fade() {
            uint32_t thisMillis = millis();
            if (thisMillis - timestamp >= 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)
               timestamp = thisMillis;
           }
       }

};

Then you can:

FadePin motorOne(5, 0, 255, 10);
FadePin motorTwo(6, 0, 255, 10);

And regularly call:

motorOne.fade();
motorTwo.fade();
1
  • Divide and conquer. – user31481 Feb 15 '18 at 10:38
1

The answer given at https://forum.arduino.cc/index.php?topic=529364.msg3608306#msg3608306 is as follows:

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

Thoughts on this approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.