5 x magnetic switches with alarm

Hi guys, first of all I want to mention I have never coded before so please understand my lack of understanding.

I am just trying to make a code that can read 5 x magnetic switches at the same time and trigger an alarm if the switch is opened. I want it to only trigger the alarm every 60 seconds.

I have the code working just fine, so my question here is; will there be an overflow issue here? I have been reading about this overflow issue that will come after aprox 50 days and I just can not figure it out.

unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis5 = 0;

const long interval = 60000;

const int door1 = D1;
const int door2 = D2;
const int door3 = D3;
const int door4 = D5;
const int door5 = D6;

void setup()
{

  pinMode(door1, INPUT_PULLUP);
  pinMode(door2, INPUT_PULLUP);
  pinMode(door3, INPUT_PULLUP);
  pinMode(door4, INPUT_PULLUP);
  pinMode(door5, INPUT_PULLUP);
}

void loop()
{
  unsigned long currentMillis = millis();

  int status1 = digitalRead(door1);
  int status2 = digitalRead(door2);
  int status3 = digitalRead(door3);
  int status4 = digitalRead(door4);
  int status5 = digitalRead(door5);

  if (status1 != LOW && currentMillis - previousMillis1 >= interval)
  {
    alarm("door_1");
    previousMillis1 = currentMillis;
  }

  if (status2 != LOW && currentMillis - previousMillis2 >= interval)
  {
    alarm("door_2");
    previousMillis2 = currentMillis;
  }

  if (status3 != LOW && currentMillis - previousMillis3 >= interval)
  {
    alarm("door_3");
    previousMillis3 = currentMillis;
  }

  if (status4 != LOW && currentMillis - previousMillis4 >= interval)
  {
    alarm("door_4");
    previousMillis4 = currentMillis;
  }

  if (status5 != LOW && currentMillis - previousMillis5 >= interval)
  {
    alarm("door_5");
    previousMillis5 = currentMillis;
  }

}


void alarm(const char *)
{
// ALARM CODE HERE 
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

If you google

over coming the arduino millis rollover

You will find many explanations on how to avoid the overflow situation.

Tom... :slight_smile:
PS Karma for using code tags.. :slight_smile: :slight_smile: :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

If you google

over coming the arduino millis rollover

You will find many explanations on how to avoid the overflow situation.

Tom... :slight_smile:
PS Karma for using code tags.. :slight_smile: :slight_smile: :slight_smile:

Hi Tom and thank you for quick reply.
I have been reading this: Arduino Tutorial: Avoiding the Overflow Issue When Using millis() and micros() – Norwegian Creations and from my understanding my code should work even after it passes the 50 days? However, I dont really understand how. I guess I dont need to understand as long as it works but as I have never coded anything before it would be nice to get it confirmed by some of your experts here.

I can see that you might wish to show which sensor triggers the alarm, but I suspect the same alarm sound can be used for every door.

Your timing code should start when an alarm is triggered and then turn the alarm off when the time is up - something like this

if (alarmTriggered == true) {
    if (alarmOn == false) {
        alarmStartTime = currentMillis;
        alarmOn = true;
    }
    if (currentMillis -  alarmStartTime >= alarmOnTime) {
        alarmOn = false;
        alarmTriggered = false;
    }
    if (alarmOn == true) {
        // code to sound alarm
    }
}

If you use an array to hold the list of sensor pins and the status of the pins the code will be much simpler - something like this

for (byte n = 0; n < 5; n ++) {
    sensorStatus[n] = digitalRead(doorSensorPin[n]);
    if (sensorStatus[n] == LOW) {
        Serial.print("Alarm on door "); Serial.println(n);
        alarmTriggered == true;
    }
}[/code

...R
[url=http://forum.arduino.cc/index.php?topic=261445.0]Planning and Implementing a Program[/url]

Robin2:
I can see that you might wish to show which sensor triggers the alarm, but I suspect the same alarm sound can be used for every door.

Your timing code should start when an alarm is triggered and then turn the alarm off when the time is up - something like this

if (alarmTriggered == true) {

if (alarmOn == false) {
        alarmStartTime = currentMillis;
        alarmOn = true;
    }
    if (currentMillis -  alarmStartTime >= alarmOnTime) {
        alarmOn = false;
        alarmTriggered = false;
    }
    if (alarmOn == true) {
        // code to sound alarm
    }
}




If you use an array to hold the list of sensor pins and the status of the pins the code will be much simpler - something like this


for (byte n = 0; n < 5; n ++) {
    sensorStatus[n] = digitalRead(doorSensorPin[n]);
    if (sensorStatus[n] == LOW) {
        Serial.print("Alarm on door "); Serial.println(n);
        alarmTriggered == true;
    }
}[/code

...R
Planning and Implementing a Program

Hi and thanks for reply!

Its not an alarm sound. The alarm code is a code that I want to run once every minute, its triggers a notification somewhere. But I dont want 500 notifications, thats why I want it once every minute.

I am happy with my code (I know it does not look very professional but it works) but I just want to know if this will still work after overflow happens. And if not, what should I do?

You're using the standard subtraction model to work with millis. You should have no problems with rollover.

elmeromero:
Its not an alarm sound. The alarm code is a code that I want to run once every minute, its triggers a notification somewhere.

I obviously misunderstood your use of the words "trigger an alarm if the switch is opened" when what you actually want to do is "send a message every 60 seconds when an alarm is triggered"

What should happen if the sensor is triggered and a short time later it is no longer triggered?

What are the circumstances in which the message should stop being sent?

...R

Robin2:
I obviously misunderstood your use of the words "trigger an alarm if the switch is opened" when what you actually want to do is "send a message every 60 seconds when an alarm is triggered"

What should happen if the sensor is triggered and a short time later it is no longer triggered?

What are the circumstances in which the message should stop being sent?

...R

I don't have my code here so I can't show you right now but what happens is that when the alarm trigger I get a message on my phone, and it continues to do so every 60 seconds until the door is closed. If I did not have the 60 second timelimit in place then my phone would be flooded with many many messages until the door closes again. So the "alarm code" is a code that notifies me.

wildbill:
You're using the standard subtraction model to work with millis. You should have no problems with rollover.

Thank you! I have been reading about this but I don't understand it. It is explained many places but when I read it I can't understand how this works. I mean, why does it work? Is it possible to explain to someone like me and make me understand? I would really like to understand why my code is working like it should, when in my head it should not.

elmeromero:
I mean, why does it work?

Is this any help?

elmeromero:
I don't have my code here so I can't show you right now but what happens is that when the alarm trigger I get a message on my phone, and it continues to do so every 60 seconds until the door is closed.

I understand the periodic messages. I just was not clear what would cause them to stop.

What causes the doors to be opened thus triggering the alarm?

...R

dougp: yes actually little bit. I need to have a look but its starting to make more sense now. Thank you!

Robin2: its a public place where the door must be unlocked for safety reasons, but guests are not suposed to open it. So I want to know if anybody opens.

"The exit door is alarmed!"

Paul__B:
"The exit door is alarmed!"

Well you will have to be very quiet then... :slight_smile: :slight_smile: :slight_smile: :slight_smile:

elmeromero:
its a public place where the door must be unlocked for safety reasons, but guests are not suposed to open it. So I want to know if anybody opens.

Getting a message every minute suggests to me that you don't intend to do anything about an open door ?

A lot of freeloaders could get through a door in a few minutes.

...R