Loop stops when using print.lcd

Hello!

I am relatively new to arduino and am trying to make a simple door notification system. I have a magnetic switch hooked up to my arduino as well as an LCD screen. When the door becomes open the sensor detects so and sends my phone an alert and when it is closed it continues to scan the sensor. This all works the problem comes in when I try to have it print to the LCD screen that the door is opened or closed, the loop completely stops running and stops checking the door sensor. If you could help me out that would be great! Below is the code I have so far.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Bridge.h>

#define BLYNK_PRINT Serial
#include <BlynkSimpleYun.h>

#include <Keypad.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*******";

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Bridge.begin();
Blynk.begin(auth);
// make the pushbutton's pin an input:
pinMode(pushButton, 2);

}

void loop() {

// the loop routine runs over and over again forever:

// read the input pin:
int buttonState = digitalRead(pushButton);
Blynk.run();
// print out the state of the button:
Serial.println(buttonState);
if (buttonState > 0) {
Blynk.notify("Door Opened!");

lcd.init();
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Door Opened!");

delay (5000);

}
else{
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Door Closed");
}

}

Your code will print the message EVERY TIME it goes through the loop (hundreds of times a second). It seems that to stop the display flickering you have put in a delay of 5 seconds. This makes the Arduino do NOTHING for 5 seconds, go through the loop once again and then do nothing, etc.

Remove the delay.

Look for the transition in your signal (0->1 or 1->0, ie, the change) to print you message, not the actual current state of the variable.

Please go and read the instructions, then go back and modify your post using the "More --> Modify" option to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as such so we can examine it conveniently and accurately. Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, the code you post could well be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.