Skip to content

Add a smarter control logic to light intensity checking #2

@alemorm

Description

@alemorm

Right now, there is no way to distinguish between the artificial light that comes from the lamp itself from the natural light that illuminates the room. This can be solved by looking at the variance of the averaged light measurements and if they are changing between averaging intervals:

  • A simple solution would be to move the data collection to a new function that declares a static double array that records the last nth average intensity values to measure whether there is a monotonic difference between the average measurements. Here is some code to illustrate the functionality:
getAverages(int lightSamples[sampleNum], double *lightAverage, double *averageDifference) {
   // Initializes the number of averages collected
  const  int averagesNum = 3;
   // Initializes the static counter for the number of averages collected
  static int averagesCount = 0;
  // Iterates lastAverages index from 0 to averagesNum with a modulus operator
  const int averagesInd = averagesCount++ % averagesNum;
  // Initializes the static double array that persists between multiple loop calls
  static double lastAverages[averagesNum] = {0};

  // Collect sampleNum number of samples to average
  for (int i=0; i<sampleNum; i++) {
    // Read the current light levels
    lightSamples[i] = analogRead(sensorPin);
    lightAverage += lightSamples[i];
    delay(readDelay);
  }

  // Calculate the average value of the light samples
  lightAverage /= sampleNum;

  // Assign the light intensity averages
  lastAverages[averagesInd] = lightAverage;

  // When the averagesNum value is reached, check the difference
  if (averagesInd == (averagesNum - 1)) {
    averageDifference = 0;
    for (int n = 1; n<averagesNum; n++) {
      averageDifference += lastAverages[n] - lastAverages[n-1];
    }
  }
}

Then if the averageDifference value is positive, that means that the light has become brighter, which would indicate the sun light is getting more intense. A lamp light would presumably not change intensity as much.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions