False Trigger Prevention

I'm trying to program a Lidar-based Arduino Leonardo and don't want to get false triggers. My goal is, that the trigger pins should be held up high for more than 3 seconds to be considered as a true trigger, otherwise, see them as a false alarm. I can only think of using the following code, but it seems to only log trigger data one within the if / else loop. Could anyone give me some advice on it, please?

// Traffic Light for 3 Lidar
//Traffic light code
int green=6;
int yellow=9;
int red=10;
int green2=1;
int yellow2=0;
int red2=5;
int Lidar_entry=23;

int Lidar_exit=22;
int Lidar_entry_trigger = 0;

int Lidar_exit_trigger = 0;
int Morning = 21;
int Morning_time = 0;
int switch1 = 8;
int switch2 = 4;
int switch1_time = 0;
int switch2_time = 0;
int state = 0;

void setup() {

  Serial.begin(57600);

  pinMode(green, OUTPUT);
  pinMode(yellow , OUTPUT);
  pinMode(red , OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(yellow2 , OUTPUT);
  pinMode(red2 , OUTPUT);
  pinMode(Lidar_entry , INPUT);
  pinMode(Lidar_exit , INPUT);
 
  pinMode(Morning, INPUT);
  pinMode(switch1, INPUT);
  pinMode(switch2, INPUT);
 }

 void loop() {
  Lidar_exit_trigger = digitalRead(Lidar_exit);
  Lidar_entry_trigger = digitalRead(Lidar_entry);
  
  Morning_time = digitalRead(Morning);
  switch1_time = digitalRead(switch1);
  switch2_time = digitalRead(switch2);
  
  Serial.print("Morning_time=");
  Serial.println(Morning_time);
  Serial.print("switch1_time=");
  Serial.println(switch1_time);
  Serial.print("switch2_time=");
  Serial.println(switch2_time);
 if (Lidar_exit_trigger){
  delay(3000);
  if (Lidar_exit_trigger){
    Serial.println("Car here");
  lidar_exit_trigger();
 }
 else{
  Serial.println("Pedestrain");
 }
 }
 else if(Lidar_entry_trigger){
  delay(3000);
  if (Lidar_entry_trigger){
    Serial.println("Car here");
  lidar_all_entry_trigger();
 }
 else{
  Serial.println("Pedestrain");
 }
 }
 else{
  lidar_no_trigger();
 }
 }

something like

unsigned long whenBtnPressed = 0;
unsigned long btnHoldTime = 3000;
bool triggered = false;
bool doTheThing = false;
voiding loopy()
{

If( pinread == HIGH and Triggered == false )
{
Triggered = true;
whenBtnPressed = millis();
} else {
Triggered = false;
}
if (Triggered )
{

if ( (millis() - whenBtnPressed) >= btnHoldTime )
{
DoTheThing = true;
}



if (DoTheThing )
{
PetThePuppetsHead();
DoTheThing = false;
}

}

The idea is to record when the button is pressed and to count how long the button is pressed and then if the pressed is 3 seconds trigger the thing to be done.

Lidar_entry_trigger ______|‾‾‾‾|____________________________________

Lidar_entry_exit_seconds  |<-------duration (sec)------->|

Lidar_exit_trigger _______________________________________|‾‾‾‾|____

If this is how the signals work and you need to know the duration an object is in between the triggers, then you'll need to measure the time duration from the rising edge to rising edge of the triggers. To detect a rising edge by polling, you need to store the previous reading.

Re: Lidar_entry_exit_seconds

Need to determine:

  • if a possible object is present (entry triggered)
  • if the object is a vehicle (entry and exit within minimum sec to max sec span)
  • if the object is possibly a pedestrian (long duration --> slow moving vehicle or pedestrian?)
  • if any trigger is invalid

Thanks Idahowalker. This is exactly what I pictured. I'll try this out.

Hi dlloyd. Thank you for your advice! There are triggers for both exit and entry, their ideas are the same. I would like to see if they've stayed triggered for 3s to proceed following codes. I'll try what Idahowalker proposed.