Count how often a Motion sensor has been Triggered

Hello Guys, first off I want to say I'm a Big noob in Arduino, i only recently started, but here is my problem:

I have two motion detectors that output a 1-Minute Signal when motion is detected, and I want to know how often these Detectors have been triggered, and store it as a variable. I hope you can help me. This is my current code that decides when an alarm should be triggered.

 if (Arming = 1) {
    Sensor1 = digitalRead(27);
    Sensor2 = digitalRead(26);

    if ((Sensor1 = LOW) || (Sensor2 = LOW))  {

      digitalWrite(33, LOW);

      alarmON();
    }

Note: I'm using the integrated Pull-up Resistor, so LOW means Motion

First you need to learn the difference between = and ==

Thank you, as I said I am a noob, but this doesn't answer my Question

Not much point answering your question if you don't understand the difference between = and == because whatever code you write will not work. That's why I said "First you need to..."

it was a mistake because I coded that at 3 am yesterday, I know the difference

Exactly speaking I don't see any question in your post.

Ok, I'll take your word for that.

Next you need to understand how to detect an edge or change on an input pin. Have a look at the "state change" example sketch.

Ok so I had a moment of clarity right now and wrote this code, that should probably work?

if (Arming == 1) {

    Sensor1 = digitalRead(27);
    Sensor2 = digitalRead(26);

    if ((Sensor1 == LOW) || (Sensor2 == LOW))  {

      digitalWrite(33, LOW);

      state = 0;

      alarmON();
    }

if (state == 0) {
      counter++;
      state = 1;         //this should ensure it only runs once per detection
}

Great, but what happens on the second detection?

It depends from that what you understand as "works". This code does not answer to question

doesn't matter, I phrased it wrong, I only want to know how often either of them triggered not separately

... and what happens the second time either of them are triggered?

It seens to me , your code will count all cases- either it triggered together or not

What is the "ON" state of the sensors - LOW or HIGH?

Yes, the if function checks if sensor one or two have triggered, both triggering simultaneously is extremely unlikely. And then will increment one number, that's exactly what I need, as the sensors are on two completely different doors

on is LOW as i mentioned in my Question

State returns back to 0 so the counter gets incremented and state goes back to 1, so it doesn't trigger again until either is triggered? or am I stupid ? xD

I just realised, because the Sensor will output a signal for one minute when triggered, i need to make another if function that puts state to 0 when sensors are high. Sorry about my stupidity xD

because it would just keep adding until the signal goes away

1 Like

Wait that wouldn't do anything facepalm I think the only way is a timer that blocks the function from running twice within the triggering period

No, another, possibly more sensible way, is to detect when both sensors have returned to HIGH, then set state = 0.

Better still, rather than making state an int, make it a Boolean who's name indicates its purpose and the meaning of true. For example:

bool sensorsTriggered = false;
...
if (Arming == 1) {

    Sensor1 = digitalRead(27);
    Sensor2 = digitalRead(26);

    if ((Sensor1 == LOW) || (Sensor2 == LOW))  {

      digitalWrite(33, LOW);

      if (!sensorsTriggered) {
        counter++;
        sensorsTriggered = true;
      }
      alarmON();
    }
    else {
      sensorsTriggered = false;
    }

You are an angel, I took the most complex route when I could just have thought simple.

Thank you very much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.