#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h> // Add the ArduinoJson library
#include <Keypad.h>
//#include <EEPROM.h>
//#include <SPI.h>
#include <SdFat.h> // SdFat kütüphanesi eklendi
#include <Servo.h>
int servoPin=11;
Servo servo1;
#define SPI_SPEED SD_SCK_MHZ(4)
#define CS_PIN 53
#define Alarm1 10
SdFat sd; // SdFat object
LiquidCrystal_I2C lcd(0x27, 20, 4); // Initialize the LCD
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
struct User {
String username;
String password;
String phone;
String fullName;
};
int Do = 262; // do
int Re = 294; // re
int Mi = 330; // mi
int Fa = 349; // fa
int Sol = 392; // sol
int Lya = 440; // la
int Si = 494; // si
int donce = 523; // do (thin)
int but = 0; // For read situation by buton
User users[50];
int currentUser = 0;
int YanlisSifre = 0;
String enteredPassword = "";
String targetPassword = "1234";
void setup() {
// Serial.begin(9600);
keypad.addEventListener(keypadEvent);
lcd.init();
lcd.backlight();
if (!sd.begin(CS_PIN, SPI_SPEED)) {
if (sd.card()->errorCode()) {
lcd.println("SD initialization failed.");
} else if (sd.vol()->fatType() == 0) {
lcd.println("Can't find a valid FAT16/FAT32 partition.");
} else {
lcd.println("Can't determine error type");
}
return;
}
servo1.attach(servoPin);
lcd.println("Files on card:");
lcd.println(" Size Name");
pinMode(Alarm1, OUTPUT); //We set INPUT or OUTPUT
// sd.ls(LS_R | LS_SIZE);
writeToSDCard();
// Read user data from SD card
Kayitokuma();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("no Tusuna basin");
lcd.setCursor(0, 1);
lcd.print("Daire numarasi girin");
lcd.setCursor(0, 2);
lcd.print("Sil 'C' Enter 'E'");
lcd.setCursor(0, 3);
lcd.print("Kapi Ac'*'");
servo1.write(0);
}
// Function to read and parse JSON user data from the SD card
void Kayitokuma() {
File myFile = sd.open("DaireSahip.txt", FILE_READ);
if (myFile) {
lcd.setCursor(0, 0);
lcd.print("Kayit okuma...");
// Allocate a buffer to store file contents
StaticJsonDocument<4096> doc; // Adjust size as needed
DeserializationError error = deserializeJson(doc, myFile);
if (!error) {
JsonArray usersArray = doc["users"].as<JsonArray>();
int i = 0;
// Print each user data to serial monitor and save in array
for (JsonObject user : usersArray) {
users[i].username = user["username"].as<String>();
users[i].password = user["password"].as<String>();
users[i].phone = user["phone"].as<String>();
users[i].fullName = user["fullName"].as<String>();
/*
// Print to Serial Monitor
Serial.print("User ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print("Username: ");
Serial.print(users[i].username);
Serial.print(", Password: ");
Serial.print(users[i].password);
Serial.print(", Phone: ");
Serial.print(users[i].phone);
Serial.print(", Full Name: ");
Serial.println(users[i].fullName);
*/
i++;
if (i >= 50) break; // Prevent array overflow
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kayit okuma basarili");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("JSON Hatasi");
// Serial.println("Error reading JSON file.");
}
myFile.close();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dosya yok");
// Serial.println("File not found.");
}
delay(2000);
lcd.clear();
}
void writeToSDCard() {
File myFile = sd.open("DaireSahip.txt", FILE_WRITE);
if (myFile) {
StaticJsonDocument<1024> doc;
JsonArray usersArray = doc.createNestedArray("users");
// Example user data
JsonObject user1 = usersArray.createNestedObject();
user1["username"] = "user1";
user1["password"] = "pass1";
user1["phone"] = "1234567890";
user1["fullName"] = "John Doe";
JsonObject user2 = usersArray.createNestedObject();
user2["username"] = "user2";
user2["password"] = "pass2";
user2["phone"] = "0987654321";
user2["fullName"] = "Jane Smith";
// Serialize the JSON document to a string
String jsonString;
serializeJson(doc, jsonString);
// Write to SD card
myFile.println(jsonString);
myFile.close();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yazma Basarili");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yazma Hatali");
}
delay(500);
lcd.clear();
}
void loop() {
keypad.getKey();
}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
if (eKey >= '0' && eKey <= '9') {
enteredPassword += eKey; // Basılan tuşu şifreye ekle
lcd.setCursor(0, 1);
lcd.print("Pass: "); // "Pass: " kısmını ekrana yazdır
// Ardından enteredPassword uzunluğu kadar * karakteri ekrana yazdır
for (int i = 0; i < enteredPassword.length(); i++) {
lcd.print('*'); // Şifre uzunluğu kadar yıldız yazdır
}
}
switch (eKey) {
case '*':
checkPassword();
resetPassword();
break; // '*' tuşuna basıldığında şifre kontrolünü yap
case '#':
break; // '#' tuşuna basıldığında şifreyi sıfırla
case 'B':
// currentUser = (currentUser + 1) % 50;
displayUsers();
break; // 'B' tuşuna basıldığında kullaniciları göster
case 'C':
lcd.setCursor(0, 1);
lcd.print("Pass: "); // "Pass: " kısmını ekrana yazdır
enteredPassword="";
eKey="";
break;
case 'D':
currentUser = atoi(enteredPassword.c_str()); // enteredPassword'ı int'e çevir
displayUsers();
break;
}
break;
}
}
/// Function to display user details one by one
void displayUsers() {
lcd.clear();
// Only display if there is valid data in the current user
if (users[currentUser].username.length() > 0) {
// Display Username (truncate if too long)
lcd.setCursor(0, 0);
lcd.print("User: " + users[currentUser].username.substring(0, 20));
// Display masked Password
lcd.setCursor(0, 1);
lcd.print("Pass: " + String(users[currentUser].password.length(),'*'));
// Display Phone (truncate if too long)
lcd.setCursor(0, 2);
lcd.print("Phone: " + users[currentUser].phone.substring(0, 20));
// Display Full Name (truncate if too long)
lcd.setCursor(0, 3);
lcd.print("Ad: " + users[currentUser].fullName.substring(0, 20));
} else {
// If user data is invalid (empty), display a message
lcd.setCursor(0, 0);
lcd.print("No user data.");
currentUser=0;
}
// Increment to the next user, wrap around if necessary
delay(300); // Display user info for 3 seconds before clearing the screen
}
/// Function to check if the entered password is correct
void checkPassword() {
// Kullanici şifresinin doğru olup olmadığını kontrol et
if (enteredPassword == users[currentUser].password) {
// Şifre doğruysa, girdiği şifreyi ve kullanici bilgilerini seri monitöre yazdır
/*
Serial.print("Entered Password: ");
Serial.println(enteredPassword);
Serial.print("Current User: ");
Serial.println(users[currentUser].username);
Serial.print("Stored Password: ");
Serial.println(users[currentUser].password);
*/
lcd.clear();
lcd.print("Onaylandi");
servo1.write(180);
if (enteredPassword == "1453" && users[21].password == "1453") {
// Serial.print("kapali: ");
noTone(Alarm1);
}
delay(2000);
servo1.write(0);
// Doğru şifre girişi sonrasında yapılacak işlemler burada olabilir
} else {
// Yanlış şifre ise "Wrong" yazdır
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yanlis Sifre: ");
/*
// Yanlış şifre ile ilgili bilgileri seri monitöre yazdır
Serial.print("Entered Password: ");
Serial.println(enteredPassword);
Serial.print("Current User: ");
Serial.println(users[currentUser].username);
Serial.print("Stored Password: ");
Serial.println(users[currentUser].password);
*/
YanlisSifre=YanlisSifre + 1;
currentUser=-0;
if(YanlisSifre==2){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1 hakkiniz kaldi");
lcd.setCursor(0, 1);
lcd.print("Yanlis girerseniz");
lcd.setCursor(0, 2);
lcd.print("POLIS Aranacaktir");
}
if(YanlisSifre==3){
tone(Alarm1,donce); //The alarm starting
delay(500);
noTone(Alarm1);
delay(10);
tone(Alarm1,Re);
delay(300);
}
}
void resetPassword(); // Şifre kontrolünden sonra şifreyi sıfırla
}
/// Function to reset the entered password
void resetPassword() {
}