I'm trying to set up a visual alarm that goes off if a person comes within a certain distance of the sensor. However, I don't want the alarm constantly going off while the person is within range (anywhere from 30 seconds to tens of minutes).
How would I make it so it goes through the Alarm block, then waits until the person leaves before resetting and going off when another person comes by later? Here's my code so far:
#include <Wire.h>
#include "SparkFun_VL53L1X_Arduino_Library.h"
VL53L1X distanceSensor;
const int OfficeLight = 3 ; //Office Strobe to Pin 3
const int LaundryLight = 4 ; //Laundry Strobe to Pin 4
void setup(void)
{
Wire.begin();
Serial.begin(9600);
Serial.println("VL53L1X Qwiic Test");
if (distanceSensor.begin() == false)
Serial.println("Sensor offline!");
}
void loop(void)
{
//Poll for completion of measurement
while (distanceSensor.newDataReady() == false)
delay(5);
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
float distanceInches = distance * 0.0393701;
float distanceFeet = distanceInches / 12.0;
//Alarm
if (distanceFeet <= 12) {
for (int a=0; a < 2; a++){
Serial.println("Person at Desk");
digitalWrite(OfficeLight, HIGH);
digitalWrite(LaundryLight, HIGH);
delay(1000);
digitalWrite(OfficeLight, LOW);
digitalWrite(LaundryLight, LOW);
delay(3000);
}
}
}