Hi all,
I'm a bit stuck at programming.
And there is quite a lot to find about it on the forum, however, so far it didn't help.
I feel like I'm close, but it does not work yet as it should.
I have a sensor which detects vacancy in a room (ToF sensor).
However, I don't want the sensor to turn the light directly on, I rather have the sensor checks the room for 5 seconds and then turn the light on.
So, similar like a pushbutton, if you press it for 5 seconds it will turn a LED on.
But with the sensor it works a bit different.
Here is my code
/* THEBULB. THE SO-LOO-TION SENSOR */
#include <Wire.h>
#include <VL53L1X.h>
VL53L1X sensor;
unsigned long interval = 5000;
unsigned long sensorActive;
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // use 400 kHz I2C
pinMode(LED_BUILTIN,OUTPUT);
pinMode(13, INPUT_PULLUP);
pinMode(12, OUTPUT);
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Let op: de sensor wordt momenteel niet gedetecteerd. Controleer aansluitingen. ");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);
sensor.startContinuous(100);
}
void loop()
{
Serial.print(sensor.read());
if (sensor.readRangeContinuousMillimeters () <=1000 && millis() - sensorActive < interval) { //schakelafstand wordt hier ingevoerd. Onder deze waarde is de schakeling actief.
digitalWrite (LED_BUILTIN, HIGH);
digitalWrite (13, HIGH);
digitalWrite (12, HIGH);
}else { //hoger dan de schakelafstand.
sensorActive = millis();
digitalWrite (LED_BUILTIN, LOW);
digitalWrite (13, LOW);
digitalWrite(12, LOW);
}
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT");}
Serial.println();
}