Hey Everyone,
I am struggling to find a way to get semi-consistent data out of this. I don’t need to be super accurate but I will get swinging values each time I upload new code.
It basically goes something like I boot it up and if it sees me ill get an output of say 100 then it sits on and then I update with the new code and this time when it sees me it’s at -2 even though I try and stand about the same distance away each time.
(I do a 10 average to get the numbers)
for (byte i = 0; i <= 10; i++)
{
ir1 = movementSensor.getIR1();
movementSensor.refresh();
ir1Last = ir1 + ir1Last;
delay(10);
if (i == 10)
{
ir1dif = ir1 - (ir1Last / 10);
if (inRange(ir1dif, SenseMin, SenseMax) )
{
Serial.println(“in Range”);
lightsOn = true;
}
else
{
Serial.print("out of Range: ");
Serial.println(ir1dif);
lightsOn = false;
}
ir1Last = 0;
ir1dif = 0;
}
}
movementSensor.refresh();
}
I was hoping to create a threshold limit of knowing when someone is in front of it. I even tried at the boot to capture what the number is when you are standing at it 2ft away and then 3ft away so I would know if someone is in front of it roughly in that location.
Long story short, I know it’s affected by ambient factors but are there any implementations I could put into effect to help mitigate this? This will be embedded into a wall sculpture in a gallery and ideally, I wouldn’t have to come in and figure out the calibration for that moment.
Any insight would be great!
Thanks!
Change the code to first check that data is available: if (movementSensor.available()). For your purpose it is better to read all the IR values and act on a change of either of them as each takes a little different angle.
Okay, I am using the movement sensor.available(). that was just a snippet of a nested if statement. I will try coding something to look for a change!
I am wondering if it would ever flatline? The overall goal is Sculpture activates while someone is in front of it, the longer they stay the more exciting the lights get. So, if they are standing there and say after 30 secs is flat lines? Just something I am wondering if I need to look out for! I will use all 4 sensors when I write this new function. Thanks for the advice!
Here is the full function:
void HumanCheck()
{
if (allowIR == true)
{
if (lightsOn == false)
{
if (movementSensor.available())
{
for (byte i = 0; i <= 10; i++)
{
ir1 = movementSensor.getIR1();
movementSensor.refresh();
ir1Last = ir1 + ir1Last;
delay(10);
if (i == 10)
{
ir1dif = ir1 - (ir1Last / 10);
if (inRange(ir1dif, SenseMin, SenseMax) )
{
Serial.println(“in Range”);
lightsOn = true;
Serial.println(“Run Warmup”);
}
else
{
Serial.print("out of Range: ");
Serial.println(ir1dif);
lightsOn = false;
}
ir1Last = 0;
ir1dif = 0;
}
}
movementSensor.refresh();
}
}
}
}
I think you are trying to do too much in a single function. break it down into a number of different functions :
Function A. Read the sensor and store the values.
Function B. Logic to detect whether someone is (still) in front or not.
Function C. controls the light brightness, increases based on the seconds someone is in front or (slowly) reduces if no one.
etc.
Function A could look like :
void ReadSensor()
{
uint8_t i = 0;
// global defined
ir1Last = ir2Last = ir3Last = ir4Last = 0;
if (allowIR) {
while (i <= 10) {
if (movementSensor.available()) {
ir1Last += movementSensor.getIR1();
ir2Last += movementSensor.getIR2();
ir3Last += movementSensor.getIR3();
ir4Last += movementSensor.getIR4();
movementSensor.refresh();
i++;
}
}
}
}
Switched it to your exampled and this is the ticket! Thank you so much. It is also way more responsive with this method.