PaulRB:
Post your updated code.
Below is the final and updated code. The full code is exceeding 9000 characters so I reduced it a bit.
#include <avr/sleep.h>
#include <Keypad.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <DS3231.h>
#define powerPin0 0 // connected to RXI
#define powerPin1 1 // connected to TX0
#define interruptPin 2
#define MOSFET 5
#define chipSelect 10
#define MOSI 11
#define MISO 12
#define clockPin 13
#define redLED A0
#define greenLED A1
#define echoPin A2
#define trigPin A3
#define hallSensorPin A7
#define Password_Length 8
char enteredPassword [Password_Length];
unsigned long int masterPasswords [6] = {1111111, 2222222, 3333333, 4444444, 5555555, 6666666};
unsigned long int userPassword;
unsigned long int number = 0;
float duration, cm; // for ultrasonic sensor
byte data_count = 0;
byte counter = 0;
byte led_blink = 0;
bool passwordAccepted = false;
bool wokeUP = true;
bool card_Data = false;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 60000;
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '5', '9'},
{'3', '7', '*'},
{'4', '8', '#'},
{'2', '6', '0'}
};
byte rowPins[ROWS] = {3, 4, 2, 6};
byte colPins[COLS] = {7, 8, 9};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
File myFile;
DS3231 rtc(SDA, SCL);
void setup() {
rtc.begin();
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(hallSensorPin, INPUT);
pinMode(MOSFET, OUTPUT);
pinMode(powerPin0, OUTPUT);
pinMode(powerPin1, OUTPUT);
digitalWrite(powerPin0, HIGH);
digitalWrite(powerPin1, HIGH);
if (!SD.begin(10)) {
while (1);
digitalWrite(redLED, HIGH);
}
}
void loop() {
led_Blinking();
systemInactiveSleep(); // go to sleep function if inactive for 1 minute
char customKey = customKeypad.getKey();
if (customKey) {
previousMillis = currentMillis; // if key is pressed reset the time
enteredPassword[data_count] = customKey;
data_count++;
digitalWrite(greenLED, HIGH);
delay(100);
digitalWrite(greenLED, LOW);
delay(100);
}
if (data_count == Password_Length - 1) {
userPassword = atol(enteredPassword);
if (MasterPassword()) {
passwordAccepted = true;
}
else if (UserPassword()) {
card_Data = true;
passwordAccepted = true;
}
else {
digitalWrite(redLED, HIGH);
myFile = SD.open("wptm.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(" -- ");
myFile.print(rtc.getTimeStr());
myFile.print(" --> ");
myFile.println(userPassword);
myFile.close();
}
delay(1000);
digitalWrite(redLED, LOW);
counter++;
if (counter == 5) {
clearData();
counter = 0;
bool recheck = true;
digitalWrite(redLED, HIGH);
while (recheck) {
char customKey = customKeypad.getKey();
if (customKey) {
enteredPassword[data_count] = customKey;
data_count++;
digitalWrite(greenLED, HIGH);
delay(100);
digitalWrite(greenLED, LOW);
delay(100);
if (data_count == Password_Length - 1) {
unsigned long masterPass = atol(enteredPassword);
if (masterPass == masterPasswords [0]) {
digitalWrite(redLED, LOW);
counter = 0;
recheck = false;
}
else {
clearData();
}
}
}
}
}
}
if (passwordAccepted) {
passwordAccepted = false;
digitalWrite(greenLED, HIGH);
digitalWrite(MOSFET, HIGH);
delay(3000);
digitalWrite(greenLED, LOW);
digitalWrite(MOSFET, LOW);
if (card_Data) {
card_Data = false;
myFile = SD.open("pass.txt", FILE_WRITE);
if (myFile) {
myFile.println(number);
myFile.close();
}
}
bool hall = true;
while (hall) {
int hall_Val = analogRead(hallSensorPin);
if (hall_Val <= 70) {
hall = false;
}
else {
hall = true;
}
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1;
if (cm < 30) {
myFile = SD.open("time.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(" -- ");
myFile.print(rtc.getTimeStr());
myFile.print(" --> ");
myFile.println(cm);
myFile.close();
}
}
Going_To_Sleep();
reconnect_Peripherals();
}
led_blink = 0;
clearData();
}
}
void led_Blinking() {
while (led_blink < 3) {
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
led_blink++;
}
}
void systemInactiveSleep() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Going_To_Sleep();
reconnect_Peripherals();
led_blink = 0;
clearData();
}
}
bool MasterPassword() {
bool master_Password = false;
for (byte x = 0; x < 6; x++) {
if (userPassword == masterPasswords[x]) {
master_Password = true;
break;
}
}
return master_Password;
}
void clearData() {
currentMillis = millis();
previousMillis = millis();
while (data_count != 0) {
enteredPassword[data_count--] = 0;
}
return;
}
void Going_To_Sleep() {
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
delay(1000);
digitalWrite(greenLED, LOW);
pinMode(interruptPin, INPUT_PULLUP);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(powerPin0, LOW);
digitalWrite(powerPin1, LOW);
sleep_cpu();
}
void wakeUp() {
sleep_disable();
detachInterrupt(0);
digitalWrite(powerPin0, HIGH);
digitalWrite(powerPin1, HIGH);
}
void reconnect_Peripherals() {
if (!SD.begin(10)) {
while (1);
}
}