LEDs flickering

Hi,

I have a follow up not directly related to the flicker but related to this project and setup. When I moved my installation to a new room (the exhibit space) with new dimensions, I changed the wall distance variable but now there is a new issue in the moment when I step out of the laser's line of sight. I think I've narrowed down the problem to the following: the system does not retain the stored_brightness because in the process of stepping out (PRESENCE to not PRESENCE), the edge of the laser sensor seems to read distances that are farther than they really are. So instead of reading 100,100,100,500,500,500,500 it reads something like 100,200,400,450,500. Those intermediate values change the brightness of the lights before storing a stored_brightness and throw things off. Instead of just the 100 dictating a brightness which is retained when the sensor reads 500.

All that said, is there a way to ignore those intermediate values (when an object leaves the laser beam) and instead have a cleaner change in distance? I tried to ignore the current loop if a PAST_PRESENCE==true and PRESENCE==false, but there is more than 1 intermediate value

Here is my recent code for reference. Thanks!

#include <MegunoLink.h>
#include <CommandHandler.h>
#include <TCPCommandHandler.h>
#include <ArduinoTimer.h>
#include <CircularBuffer.h>
#include <EEPROMStore.h>
#include <Filter.h>
ExponentialFilter<long> ADCFilter(0, 0);

#include <RBDdimmer.h>
#include <Wire.h>
#include <LIDARLite.h>

#define USE_SERIAL  Serial
#define outputPin  10 
#define zerocross  2 // for boards with CHANGEBLE input pins

dimmerLamp dimmer(outputPin); //initialase port for dimmer for UNO, MEGA, Leonardo...

LIDARLite myLidarLite;

const float min_dist = 100;
const float max_dist = 440;
const float wall_dist = 455; //set too high if you want the brightness to not be stored
const float min_brightness = 40; //40 functionally off
const float max_brightness = 89; //functionally brightest
const float m = -1*(max_brightness-min_brightness)/(max_dist-min_dist);

float stored_brightness = 42; //initialize with some value

bool PRESENCE = false;
unsigned long startTime;
const unsigned long period = 20000;
bool timing=false;
bool periodElapsed = false;
int time_count=0;

void setup() {
  USE_SERIAL.begin(9600);

  myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
  myLidarLite.configure(0); // Change this number to try out alternate configurations
  
  pinMode(outputPin, OUTPUT);
  pinMode(zerocross, INPUT);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialization: name.begin(MODE, STATE) 
  dimmer.toggleSettings(0, 100); //Name.toggleSettings(MIN, MAX);
  dimmer.setPower(min_brightness);
  dimmer.setState(ON); // state: dimmer1.setState(ON/OFF);
}

void setDimmerOnPresence(float dist) {
  
  float brightness = m*(dist-min_dist)+max_brightness; //linear dropoff

  if(!PRESENCE){
    dimmer.setPower(stored_brightness);
    USE_SERIAL.print("A");
  }
  else{
    USE_SERIAL.print("B");
    if(dist>max_dist){ //max_dist
      dimmer.setPower(66); //min_brightness
    }
    if(dist<min_dist){
      dimmer.setPower(max_brightness-2);
    }
    else{
      dimmer.setPower(brightness);
    }
  }
  stored_brightness=dimmer.getPower();
}

void loop() {  
  float dist = myLidarLite.distance();
  dist = dist-5.0;
  ADCFilter.Filter(dist);
  
  if(dist<wall_dist){
    PRESENCE = true;
  }
  else{
    PRESENCE = false;
  }
  
  if((dist==1.0 || dist>999.0) && (time_count<15)){
    time_count=time_count+1;
    return;
  }
  time_count=0;
  
  setDimmerOnPresence(dist);

  USE_SERIAL.print(dimmer.getPower());
  USE_SERIAL.print("    ");
  USE_SERIAL.println(dist);
}