Arduino to trigger alarm only after 10 sec beam break

Hey guys.

I’ve used an Arduino pro mini 3.3v together with a set of IR beam break sensors https://www.amazon.co.uk/dp/B0742B8P8W/ … 4d40b9dc15

I want to have the Arduino turn a pin HIGH whenever the beam has been broken for 10 secs or more. I’m going to use it as an indicator for trucks waiting in a queue. Therefore it must NOT trigger the alarm if something just passes through the beam.

I’m not quiet sure if I should use a for loop or just a set of delays to manage this

So far I’ve managed to turn the alarm pin HIGH whenever the beam breaks.

Can you guys give some inputs as to how you would make the alarm only trigger after a 10 sec beam break?

My code looks like this:

#define IR_PIN 6

#define LED_PIN 13

#define ALARM_PIN 10

int val = 0;

void setup() {

Serial.begin(19200); //for debug

pinMode(ALARM_PIN, OUTPUT); //Alarm pin

pinMode(LED_PIN, OUTPUT); //setup the pin to light when set

pinMode(IR_PIN, INPUT); //from IR receiver

}

void loop() {

val = digitalRead(IR_PIN);

digitalWrite(ALARM_PIN, val);

}

// Assuming active LOW logic

// 10 seconds == 100 0.1 second intervals
int count = 100;
while (digitalRead(IR_PIN) == LOW && count > 0)
{
    delay(100);
    count--;
}

if (count == 0)
{
    // Was LOW for 10 seconds
    digitalWrite(ALARM_PIN, HIGH);
}

Have fun :slight_smile:

BTW: did those sensors work out OK for you?

Awesome.

Actually it’s wired so that when the beam breaks the input pin on the arduino goes high.

I’ll just switch that around.

What happens in your code when the count reaches 0? The ALARM_PIN will go high but will the counter reset to 100 automatically when the beam reestablishes?

I’ve only tested the sensors inside over a distance of a few metres. I’m hoping for success outside over 20m. If not I’ll have a whole other problem

I’ve tinkered a bit with it now.

Your code successfully turns the alarm_pin HIGH after 10 sec beam break. However, after that, the alarm_pin stays HIGH no matter what happens.

Also, I guess that if the beam breaks say five times for two secs each, the counter will reach 0 and turn the alarm_pin HIGH again.

I need something to “reset” the counter whenever the beam reestablishes