Example Ambient Light Sensor Averaging

Wink setup, hardware, basic programming. User to user support.
Posts: 32
Joined: Sun Feb 07, 2016 6:10 am

Example Ambient Light Sensor Averaging

Postby wclift » Sat Feb 27, 2016 5:21 am

I was ready to try some barrier detection with my new Wink. But my sensor readings bounced around a bit. So, I did some research on sensor averaging algorithms, found some complex array implementations that would average each reading with the 8 previous readings. That worked, but was more than I needed. I just decided to read the sensor 8 consecutive times and average the results and return them. I added some debug code to display value to the serial screen. I've started trying to use this for clean barrier detection that is less sensitive to light conditions.

This code builds from PlumGeek demo code and other internet examples. The mistakes are all mine :D!

Just thought I'd share,

Code: Select all

  Wayne's Ambient Sensor Average Routine

#include "WinkHardware.h"

// Raw and Average barrier reflection values
int leftReflection, centerReflection, rightReflection;   
int leftReflectAvg, centerReflectAvg, rightReflectAvg;

void setup() {
  hardwareBegin();       //initialize Wink's brain to work with his circuitry
  playStartChirp();       //Play startup chirp and blink eyes

void loop() {
} //end of loop()   

// Wayne barrierDetect variation
//   To get more consistent ambient light readings, this routine performs 8 readings
//   of each ambient light sensor and averages the values.  Global variables are
//   used for the average reading and the final raw reading.  I've found it helpful
//   to use the average reading for more stable sense values.
int barrierDetect(int DEBUG) {
  int leftOnLevel,  centerOnLevel,  rightOnLevel;
  int leftOffLevel, centerOffLevel, rightOffLevel;

  int leftReflectSum   = 0;
  int centerReflectSum = 0;
  int rightReflectSum  = 0;

  for(int i=0; i<8; i++) {
    digitalWrite(HeadLight, HIGH);                   // Turn on the IR headlight
    delayMicroseconds(200);                          // Wait 200uS for sensors to stabalize before reading
    leftOnLevel    = analogRead(AmbientSenseLeft);   // Read all 3 ambient sensors with headlight turned on
    centerOnLevel  = analogRead(AmbientSenseCenter);
    rightOnLevel   = analogRead(AmbientSenseRight);

    digitalWrite(HeadLight, LOW);                    // Turn off the IR headlight
    delayMicroseconds(200);                          // Wait 200uS for sensors to stabalize before reading
    leftOffLevel   = analogRead(AmbientSenseLeft);   // Read all 3 ambient sensors with headlight turn off
    centerOffLevel = analogRead(AmbientSenseCenter);
    rightOffLevel  = analogRead(AmbientSenseRight);

    leftReflection   = leftOnLevel   - leftOffLevel;    // Subtact the "off" value so we're left with just the
    centerReflection = centerOnLevel - centerOffLevel;  // Value from reflected light coming back into the sensor
    rightReflection  = rightOnLevel  - rightOffLevel;

    if (centerReflection < 0) { centerReflection = 0; } // Don't let reflection values go negative.
    if (leftReflection   < 0) { leftReflection   = 0; } // These are global variables and can be read later.
    if (rightReflection  < 0) { rightReflection  = 0; }

    leftReflectSum   += leftReflection;                 // Adding up 8 values of each sensor reading
    centerReflectSum += centerReflection;
    rightReflectSum  += rightReflection;
  leftReflectAvg   = leftReflectSum>>3;                 // Then the shift-right by 3 is really a binary
  centerReflectAvg = centerReflectSum>>3;               // divide-by-8 operation.
  rightReflectAvg  = rightReflectSum>>3;                // This calculates the average of the 8 sensor readings.

  if (DEBUG) {
    Serial.print("LR/CR/RR = (");
    Serial.print(")  \t(");
  return centerReflectAvg;                            // Return the value for centerReflection

Who is online

Users browsing this forum: No registered users and 2 guests