welcome at SebWalak.com

Non-blocking lighting effects in Arduino

Arduino tinkering

There’s been some occasions when I needed to perform few operations on an Arduino/Genuino board, but these had to happen along each other. This platform does not support parallel execution, and there isn’t any built-in higher level tools to support asynchronous execution, but you can emulate asynchronous execution by changing the composition of your code.

In this particular case my requirements for the product were:

  1. The lighting is to be following some predefined, sometimes elaborate patterns of intensities depending on elapsed time
  2. The product would react to user interactions via a number of buttons and a potentiometer

Some of the problems requiring non-blocking processing can be tackled with interrupts but the number of pins providing this feature is limited on the traditional boards (Uno, Nano, Mega, Micro and others). For a potentiometer some additional circuitry would be needed to identify user’s input. Interrupts wouldn’t (without making it a really awkward solution) help to control the output simultaneously.

I however opted-in for a solution where the task is broken down into many short steps (as a function of time) and that allows to perform other, but also short, activities in the meantime.

Here’s the code:

const int LED = 5;

long startedAt;

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

const int lightingPatternOnOffSmooth[][2] = {
  {0, 0},
  {2000, 255},
  {4000, 0},
};

const int lightingPatternOnOffRapid[][2] = {
  {0, 0},
  {1999, 0},
  {2000, 255},
  {3999, 255},
  {4000, 0},
};

int intensityAsAFunctionOfTime(const int f[][2], int sizeOfArray, const long time) {
  int noOfRefPts = sizeOfArray / sizeof(f[0]);
  int period = f[noOfRefPts - 1][0];

  long t = time % period;
  int indexBefore = -1;
  int indexAfter = -1; 
  long totalTime = 0;
  int nextPt[2];

  for (int refPt = noOfRefPts - 1; refPt >= 0 ; refPt--) {
    if (t >= f[refPt][0]) {
      if (refPt == noOfRefPts - 1) {
        nextPt[0] = f[0][0] + period;
        nextPt[1] = f[0][1];
      } else {
        nextPt[0] = f[refPt+1][0];
        nextPt[1] = f[refPt+1][1];
      }
      long timeDiff = nextPt[0] - f[refPt][0];
      float timeFraction = (t - f[refPt][0]) / float(timeDiff);
      int intensityDiff = nextPt[1] - f[refPt][1];
      int intensityFraction = intensityDiff * timeFraction;
      return f[refPt][1] + intensityFraction;
    }
  }
  return 0;
}

void loop() {

  int pwm = intensityAsAFunctionOfTime(
    lightingPatternOnOffRapid, 
    sizeof(lightingPatternOnOffRapid), 
    millis());

  analogWrite(LED, pwm);

  // capture user's input or handle other stuff

}

The above listing contains two lighting patterns:

const int lightingPatternOnOffSmooth[][2] = {
  {0, 0},
  {2000, 255},
  {4000, 0},
};

const int lightingPatternOnOffRapid[][2] = {
  {0, 0},
  {1999, 0},
  {2000, 255},
  {3999, 255},
  {4000, 0},
};

These two-dimensional arrays define two simple lighting patterns. Each provides characteristic function values at their times. The first index of nested array denotes a point in time, the second is the 8 bit value that will be written to an output pin (make sure to use PWM pin). The pattern will loop (jump back to first value when last one was processed). The function intensityAsAFunctionOfTime() will smooth out the values as it hits the times in-between defined points. The above two patterns demonstrate how to achieve smooth or rapid transition.

If you wonder why I don’t perform sizeof within the intensityAsAFunctionOfTime() function itself it is because the array variable decays into a pointer as I am passing it into the function, which looses the information about its size.

And here you have more fancy patterns:

const int lightingPatternHeartbeat[][2] = {
  {0, 0},
  {50, 80},
  {250, 0},
  {300, 0},
  {350, 100},
  {500, 0},
  {1500, 0}
};

const int lightingPatternGlow[][2] = {
  {0, 5},
  {1000, 50},
  {1500, 5},
  {1750, 10},
  {2000, 5},
  {2500, 50},
  {3000, 5},
};
Thumbnail