Home Alarm mills() doesn't work

Although i have read the tutorial on millis() i can't seem to add it on my code

this is the void loop

void loop()
{
  char key = kpd.getKey();
  if (key == '*' || key == '#') {
    position = 0;
    //setLocked == true;
  }
 
  if (key == secretCode[position]) {
    position++;
  }
  delay(100);
  if (position == 0 && test == 0) {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("ARMED");
    test = 1;
    }
  else if (position == 4){
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("DISSARMED");
    test = 0;
    //resetFunc();
    }
  if (magnetic0 == HIGH){
    door_open_time = millis ();
    lcd.print("dadas");
    if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 10000){
      digitalWrite(relay,HIGH);       
      lcd.setCursor(0,0);
      lcd.print("alarm...          ");
      lcd.setCursor(0,1);
      lcd.print("   ");
      trip_magnetic0();
    }
    }
    sensorcheck();
  }

i want when a person gets in the house to have 10 secs before the relay goes high and the sirens start to work but i can't get it to work do you have any ideas??

magnetic0 is the contact which goes to the front door

Think carefully about this section.

door_open_time = millis ();
    lcd.print("dadas");
    if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 10000){

Please post your code - all of it.

AWOL:
Think carefully about this section.

door_open_time = millis ();

lcd.print("dadas");
    if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 10000){




Please post your code - all of it.
#include <Wire.h>
#include <Keypad_I2C.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <SimpleTimer.h>


char* secretCode = "1234";
int position = 0;

//PIN CONTROL START
const int buzzer = 7; //BUZZER'S PIN
const int pir0 = A1; //MOTION DETECTOR 1
const int pir1 = 5; //MOTION DETECTOR 2
const int pir2 = 6; //MOTION DETECTOR 3
const int magnetic0 = 9; //MAGNETIC CONTACTS SET 1
const int magnetic1 = A0; //MAGNETIC CONTACTS SET 2
int relay = 8; //RELAY'S PIN FOR SIRIN AND STROBE
int setLocked;
//PIN CONTROL END

//LCD CODE START
#define lcdAddr 0x27
//LCD I2C ADDRESS
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // addr, EN, RW, RS, D4, D5, D6, D7, Backlight, POLARITY
//LCD CODE END

//KEYPAD CODE START
const byte ROWS = 3; 
const byte COLS = 4; 

char keys[ROWS][COLS] = {
  {'9','6','3','#'},
  {'8','5','2','0'},
  {'7','4','1','*'},
};

byte rowPins[ROWS] = {3,2,1};
byte colPins[COLS] = {4,5,6,0}; 

int i2caddress = 0x20; // KEYPAD I2C ADDRESS

Keypad_I2C kpd = Keypad_I2C( makeKeymap(keys), rowPins, colPins, ROWS, COLS, i2caddress );



//KEYPAD CODE END

#define	C0 440 
//BUZZER TONE CLASSIC


void setup(){
  lcd.begin(16,2); //INITIALIZE LCD
  lcd.setBacklight(1); // ENABLE LCD BACKLIGHT
  Serial.begin(9600); // ENABLE SERIAL FOR DEBUG PURPOSES
  kpd.begin(); //INITIALIZE KEYPAD

  //pinMode(buzzer, OUTPUT);
  //pinMode(pir0, INPUT);
  //pinMode(pir1, INPUT);
  //pinMode(pir2, INPUT);
  pinMode(magnetic0, INPUT);
  //pinMode(magnetic1, INPUT);
  pinMode(relay, OUTPUT);

  lcd.setCursor(0,0);
  lcd.print("--INITIALIZING--");
  lcd.setCursor(0,1);
  lcd.print("Please Wait...  ");
  delay(5000);//GIVE MOTION SENSOR TO CALIBRATE
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("    *ACTIVE*    ");
  delay(2000);
}

void indicator_pir0(){
  lcd.setCursor(0,0);
  lcd.print(" MOTION SENSOR 1");
  lcd.setCursor(0,1);
  lcd.print("    *ACTIVE*    ");
  delay(1000);
}

void indicator_pir1(){
  lcd.setCursor(0,0);
  lcd.print(" MOTION SENSOR 2");
  lcd.setCursor(0,1);
  lcd.print("    *ACTIVE*    ");
  delay(1000);
}

void indicator_pir2(){
  lcd.setCursor(0,0);
  lcd.print(" MOTION SENSOR 3");
  lcd.setCursor(0,1);
  lcd.print("    *ACTIVE*    ");
  delay(1000);
}

void indicator_magnetic0(){
  lcd.setCursor(0,0);
  lcd.print("  DOOR SENSOR 1 ");
  lcd.setCursor(0,1);
  lcd.print("    *ACTIVE*    ");
  delay(800);
  tone(7,C0,100); 
}  

void indicator_magnetic1(){
  lcd.setCursor(0,0);
  lcd.print("  DOOR SENSOR 2 ");
  lcd.setCursor(0,1);
  lcd.print("    *ACTIVE*    ");
  delay(1000);
}  

void trip_pir0(){
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print(" MOTION 1");
  digitalWrite(relay, HIGH);
}

void trip_pir1(){
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print(" MOTION 2");
  digitalWrite(relay, HIGH);
}

void trip_pir2(){
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print(" MOTION 3");
  indicator_pir2();
}

void trip_magnetic0(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" BREACH DOOR 1");
  digitalWrite(relay, HIGH);
  indicator_magnetic1();
}

void trip_magnetic1(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" BREACH DOOR 2");
  indicator_magnetic1();
}
long door_open_time = 0;
void sensorcheck(){
  int magnetic0 = digitalRead(A0);
//int magnetic1 = digitalRead(A0);
  int pir0 = digitalRead(A1);
  //int pir1 = digitalRead(5);
  //int pir2 = digitalRead(6);
  if ( magnetic0 == HIGH){
    trip_magnetic0();
  }
  else if ( magnetic1 == HIGH){
      trip_magnetic1();
    }

  else if ( pir0 == HIGH){
    trip_pir0();
  }
  else if ( pir1 == HIGH){
    trip_pir1();
  }
  else if ( pir2 == HIGH){
    trip_pir2();
  }
}

int test=0;
int test2=0;
int lastpir0=LOW;


void(* resetFunc) (void) = 0;


void loop()
{
  char key = kpd.getKey();
  if (key == '*' || key == '#') {
    position = 0;
    //setLocked == true;
  }
 
  if (key == secretCode[position]) {
    position++;
  }
 
  //if (position == 6) {
   // setLocked == false;
  //}
  delay(100);
  if (position == 0 && test == 0) {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("ARMING");
    test = 1;
    }
  else if (position == 4){
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("DISSARMED");
    test = 0;
    //resetFunc();
    }
  if (magnetic0 == HIGH){
    door_open_time = millis ();
    lcd.print("dadas");
    if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 1000){
      digitalWrite(relay,HIGH);       
      lcd.setCursor(0,0);
      lcd.print("alarm...          ");
      lcd.setCursor(0,1);
      lcd.print("   ");
      trip_magnetic1();
    }
    }
    sensorcheck();
  }
  /*if (position == 0 && pir0 == HIGH){
  door_open_time = millis ();
    if ((position == 0) && (millis () - door_open_time) >= 10000 ){
    digitalWrite(relay,HIGH);       
    lcd.setCursor(0,0);
    lcd.print("alarm...          ");
    lcd.setCursor(0,1);
    lcd.print("                                  ");
    }
  }
}
void setLocked(int locked)
{
  if (setlocked == true) {
    //digitalWrite(relay, HIGH);
    sensorcheck();
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("ARMED");
  }
  else if (setlocked == true) {
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("DISSARMED");
  }
}
*/

I can't see where door_open_time is defined. What datatype is it? It should be unsigned long.

And think carefully about @AWOL's advice.

...R

    door_open_time = millis ();
    lcd.print("dadas");
    if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 1000){

The if statement will still never be true.

You need to understand that replacing delay() with millis() ala the blink without delay example involves more than just changing a couple of lines of code.

Robin2:
I can't see where door_open_time is defined. What datatype is it? It should be unsigned long.

And think carefully about @AWOL's advice.

...R

PaulS:

    door_open_time = millis ();

lcd.print("dadas");
   if ((magnetic0 == HIGH) && (millis () - door_open_time) >= 1000){



The if statement will still never be true.

You need to understand that replacing delay() with millis() ala the blink without delay example involves more than just changing a couple of lines of code.

i did it like the blink without delay but again nothing

long previousMillis = 0;
long interval = 10000;  

void loop()
{
  unsigned long currentMillis = millis();
  char key = kpd.getKey();
  if (key == '*' || key == '#') {
    position = 0;
    //setLocked == true;
  }
 
  if (key == secretCode[position]) {
    position++;
  }
 
  delay(100);
  if (position == 0 && test == 0) {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("ARMING");
    test = 1;
    }
  else if (position == 4){
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("DISSARMED");
    test = 0;
    //resetFunc();
    }
    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){
      previousMillis = currentMillis; 
      //digitalWrite(relay,HIGH);       
      lcd.setCursor(0,0);
      lcd.print("alarm...          ");
      lcd.setCursor(0,1);
      lcd.print("   ");
      trip_magnetic1();
    }
    //sensorcheck();
  }

but again nothing

Again, your code is incomplete.

The solution is to

(Note that the answers can be complete only when the code is.)

PaulS:

but again nothing

Again, your code is incomplete.

The solution is to

(Note that the answers can be complete only when the code is.)

if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){

is this wrong??

is this wrong??

Since I can't see that magnetic0 is assigned a value, I'll have to assume that it is.

PaulS:

is this wrong??

Since I can't see that magnetic0 is assigned a value, I'll have to assume that it is.

magnetic0 is a magnetic contact which is connected to A0 and GND, when the magnet is there i get HIGH and when it is not i get LOW

when the magnet is there i get LOW and when it is not i get HIGH

i cleared a bit and maked it look more like the blink without delay example

    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){
      previousMillis = currentMillis; 
      
        if (magnetic0 == HIGH)
          lcd.setCursor(0,0);
          lcd.print("alarm... ");          
        digitalWrite(relay, HIGH);
    }
    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){
      previousMillis = currentMillis; 
      
        if (magnetic0 == HIGH)

Is the assignment of currentMillis to previousMillis going to affect the value of magnetic0? I can't see how.

PaulS:

    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){

previousMillis = currentMillis;
     
        if (magnetic0 == HIGH)



Is the assignment of currentMillis to previousMillis going to affect the value of magnetic0? I can't see how.

i had movement on my relay !!!!

also,

if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH))
{
  previousMillis = currentMillis; 
  if (magnetic0 == HIGH)  //<<<<<<<<<<<<<<<<<<you wouldn't be here if this wasn't true already, right?
    lcd.setCursor(0,0);
  lcd.print("alarm... ");          
  digitalWrite(relay, HIGH);
}

Geocheats2:

PaulS:

    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){

previousMillis = currentMillis;
     
       if (magnetic0 == HIGH)



Is the assignment of currentMillis to previousMillis going to affect the value of magnetic0? I can't see how.

i had movement on my relay !!!!

PaulS:

    if ((currentMillis - previousMillis > interval) && (magnetic0 == HIGH)){

previousMillis = currentMillis;
     
        if (magnetic0 == HIGH)



Is the assignment of currentMillis to previousMillis going to affect the value of magnetic0? I can't see how.

it is wrong in this way

    if (currentMillis - previousMillis > interval){
      previousMillis = currentMillis; 
        if (magnetic0 == HIGH)
          lcd.setCursor(0,0);
          lcd.print("alarm... ");          
          digitalWrite(relay, HIGH);
    }

i have to go like before

    if ((currentMillis - previousMillis > interval) && magnetic0 == HIGH){
      previousMillis = currentMillis; 
          lcd.setCursor(0,0);
          lcd.print("alarm... ");          
          digitalWrite(relay, HIGH);
    }

if i use this

    if (currentMillis - previousMillis > interval){
      previousMillis = currentMillis; 
        if (magnetic0 == HIGH)
          lcd.setCursor(0,0);
          lcd.print("alarm... ");          
          digitalWrite(relay, HIGH);
    }

the relay goes HIGH without me removing the magnet from the magnetic contact

Please add {} to your if

if (currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if (magnetic0 == HIGH)
{
lcd.setCursor(0,0);
lcd.print("alarm... ");
digitalWrite(relay, HIGH);
}
}

Thanks for the pointers everyone i think i got it to work

    if ((magnetic0 == HIGH) && (currentMillis - previousMillis > interval)){
      previousMillis = currentMillis; 
          lcd.setCursor(0,0);
          lcd.print("alarm... ");          
          digitalWrite(relay, HIGH);
    }

now i have a new problem if the interval has passed without the magnetic0 to go high it doesn't delay the rest of the programm

surbyte:
Please add {} to your if

if (currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if (magnetic0 == HIGH)
{
lcd.setCursor(0,0);
lcd.print("alarm... ");
digitalWrite(relay, HIGH);
}
}

this works i think better than the previous one

Geocheats2:

surbyte:
Please add {} to your if

if (currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if (magnetic0 == HIGH)
{
lcd.setCursor(0,0);
lcd.print("alarm... ");
digitalWrite(relay, HIGH);
}
}

this works i think better than the previous one

This works perfect THANKS

any ideas how to add a buzzer while waiting?