I am wondering if it is possible to remove all delays from my code, but working exactly as it is, so that I can do other things during those 'delays'?
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long startMillis; //set up millis for reset
unsigned long currentMillis; //pull up millis for reset
const unsigned long period = 604800000; //the value is a number of milliseconds, ie 7 days
unsigned long previousMillis = 0; // will store last time LCD was updated
const long interval = 1000; // interval at which to turn off (milliseconds)
#define SS_PIN 10
#define RST_PIN 9
#define RELAY 3 //relay pin
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup(){
Serial.begin(9600); // Initiate a serial communication
Serial.println(F("Initializing SPI, mfrc522, Relay, and Buzzer"));
SPI.begin();
mfrc522.PCD_Init();
pinMode(RELAY, OUTPUT);
pinMode(BUZZER, OUTPUT);
Serial.println(F("No sound from Buzzer until instructed otherwise"));
noTone(BUZZER);
Serial.println(F("Initialize LCD"));
lcd.init();
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD backlight on"));
lcd.backlight();
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 100 milliseconds"));
delay(100);
Serial.println(F("Buzzer deactivated"));
noTone(BUZZER);
Serial.println(F("LCD print 'ARDUINO SECURITY DOOR' for 2 seconds"));
lcd.print(F("ARDUINO SECURITY"));
lcd.setCursor(0, 1);
lcd.print(F(" DOOR "));
delay(2000);
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD print 'ACTIVATING SYSTEM' for 2 seconds"));
lcd.print(F(" ACTIVATING "));
lcd.setCursor(0, 1);
lcd.print(F(" SYSTEM "));
delay(2000);
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("Relay test - activated for 2 milliseconds"));
digitalWrite(RELAY, HIGH);
delay(2);
digitalWrite(RELAY, LOW);
Serial.println(F("LCD print 'PLEASE BE PATIENT' for 2 seconds"));
lcd.print(" PLEASE BE ");
lcd.setCursor(0, 1);
lcd.print(" PATIENT ");
delay(2000);
Serial.println(F("Relay default is LOW"));
digitalWrite(RELAY, LOW);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 1 second"));
delay(1000);
Serial.println(F("Buzzer deactivated"));
noTone(BUZZER);
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD print 'SCAN CARD TO UNLOCK' forever, or until card is scanned"));
lcd.print(" SCAN CARD ");
lcd.setCursor(0, 1);
lcd.print(" TO UNLOCK ");
Serial.println(F("LCD backlight is to be turned off after 30 seconds if no card is scanned"));
previousMillis = millis();
Serial.println(F("Set up resetFunc"));
Serial.println(F("If 7 days have elapsed, call reset"));
}
void(*resetFunc) (void)=0;
void loop(){
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
Serial.println(F("get the current 'time'"));
Serial.println(F("test whether the period has elapsed"));
Serial.println(F("call reset"));
resetFunc(); //call reset
}
currentMillis = millis();
if (millis() - previousMillis > 30 * 1000UL) { lcd.noBacklight(); lcd.clear(); }
if ( ! mfrc522.PICC_IsNewCardPresent()) return; // Look for new cards
if ( ! mfrc522.PICC_ReadCardSerial()) return; // Select one of the cards
Serial.print(F("UID tag :"));
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
content.toUpperCase();
if ((content.substring(1) == "93 6D BE 0D")
||(content.substring(1) == "51 67 DC B0")
||(content.substring(1) == "A3 23 74 11")//change here the UID of the card/cards that you want to give access A3 23 74 11
||(content.substring(1) == "55 42 2B 0D"))
{
previousMillis = millis();
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD backlight is on"));
lcd.backlight();
Serial.println(F("LCD print 'ACCESS GRANTED' for 7 seconds"));
lcd.print(" ACCESS ");
lcd.setCursor(0, 1);
lcd.print(" GRANTED! ");
Serial.println(F("Relay activated for 7 seconds"));
digitalWrite(RELAY, HIGH);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 400 milliseconds"));
delay(400);
Serial.println(F("Buzzer deactivated"));
noTone(BUZZER);
delay(6600);
Serial.println(F("Relay deactivated"));
digitalWrite(RELAY, LOW);
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD print 'SCAN CARD TO UNLOCK' forever, or until card is scanned"));
lcd.print(" SCAN CARD ");
lcd.setCursor(0, 1);
lcd.print(" TO UNLOCK ");
Serial.println(F("LCD backlight is to be turned off after 30 seconds if no card is scanned"));
}
else {
previousMillis = millis();
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD backlight is on"));
lcd.backlight();
Serial.println(F("LCD print 'ACCESS DENIED' for 1.75 seconds"));
lcd.print(" ACCESS ");
lcd.setCursor(0, 1);
lcd.print(" DENIED ");
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
delay(150);
noTone(BUZZER);
delay(25);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 150 milliseconds"));
delay(150);
Serial.println(F("Buzzer deactivated for 25 milliseconds"));
noTone(BUZZER);
delay(25);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 150 milliseconds"));
delay(150);
Serial.println(F("Buzzer deactivated for 25 milliseconds"));
noTone(BUZZER);
delay(25);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 150 milliseconds"));
delay(150);
Serial.println(F("Buzzer deactivated for 25 milliseconds"));
noTone(BUZZER);
delay(25);
Serial.println(F("Buzzer activated at 1KHz"));
tone(BUZZER, 1000);
Serial.println(F("Buzzer sounding for 150 milliseconds"));
delay(150);
Serial.println(F("Buzzer deactivated for 1 second"));
noTone(BUZZER);
delay(1000);
Serial.println(F("Clear LCD"));
lcd.clear();
Serial.println(F("LCD print 'SCAN CARD TO UNLOCK' forever, or until card is scanned"));
lcd.print(" SCAN CARD ");
lcd.setCursor(0, 1);
lcd.print(" TO UNLOCK ");
Serial.println(F("LCD backlight is to be turned off after 30 seconds if no card is scanned"));
}
}
Thanks for your help!