#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int totalStudents = 10;
char studentNames[10][20] = {"Hazim", "Haiqal", "Husni", "Hadif", "Irfan", "Iskandar", "Maisarah", "Ariana", "Amirah", "Damia"};
bool attendanceStatus[10] = {false};
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] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 0, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your I2C address if different
const int buttonPin = 13; // Pin connected to the push button
const int buzzerPin = 12; // Pin connected to the buzzer
bool keypadEnabled = true; // Flag to check if keypad is enabled
int currentStudent = 0;
bool lastButtonState = HIGH; // Previous state of the button
unsigned long lastDebounceTime = 0; // Last time the button state was toggled
unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
lcd.begin(16, 2);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Ensure the backlight is turned on
lcd.print("Attendance System");
delay(2000);
lcd.clear();
}
void loop() {
// Read the state of the button
bool reading = digitalRead(buttonPin);
// Check if the button state has changed
if (reading != lastButtonState) {
lastDebounceTime = millis(); // Reset the debounce timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has stabilized and is LOW, toggle the keypad state
if (reading == LOW) {
keypadEnabled = !keypadEnabled;
lastDebounceTime = millis(); // Reset debounce timer to avoid multiple toggles
}
}
lastButtonState = reading;
if (keypadEnabled) {
char key = keypad.getKey();
if (key) {
switch (key) {
case '#':
markPresent();
break;
case 'A':
displayPresentCount();
break;
case 'B':
displayAbsentCount();
break;
case 'C':
displayPresentNames();
break;
case 'D':
displayAbsentNames();
break;
case '*':
displayNextStudent();
break;
}
}
} else {
lcd.clear();
lcd.print("Keypad Disabled");
}
}
void markPresent() {
attendanceStatus[currentStudent] = true; // mark as present
lcd.clear();
lcd.print(studentNames[currentStudent]);
lcd.setCursor(0, 1);
lcd.print("Present!");
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
delay(1000); // Keep the buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
lcd.clear();
currentStudent++;
if (currentStudent >= totalStudents) {
currentStudent = 0; // Reset to the first student
}
}
void displayPresentCount() {
int presentCount = 0;
for (int i = 0; i < totalStudents; i++) {
if (attendanceStatus[i]) {
presentCount++;
}
}
lcd.clear();
lcd.print("Present: ");
lcd.print(presentCount);
delay(2000);
lcd.clear();
}
void displayAbsentCount() {
int absentCount = totalStudents - currentStudent;
lcd.clear();
lcd.print("Absent: ");
lcd.print(absentCount);
delay(2000);
lcd.clear();
}
void displayPresentNames() {
lcd.clear();
lcd.print("Present Students:");
delay(1000);
lcd.clear();
for (int i = 0; i < totalStudents; i++) {
if (attendanceStatus[i]) {
lcd.clear();
lcd.print(studentNames[i]);
delay(1000);
lcd.clear();
}
}
lcd.clear();
}
void displayAbsentNames() {
lcd.clear();
lcd.print("Absent Students:");
delay(1000);
lcd.clear();
for (int i = 0; i < totalStudents; i++) {
if (!attendanceStatus[i]) {
lcd.clear();
lcd.print(studentNames[i]);
delay(1000);
lcd.clear();
}
}
lcd.clear();
}
void displayNextStudent() {
lcd.clear();
lcd.print(studentNames[currentStudent]);
delay(1000);
lcd.clear();
currentStudent++;
if (currentStudent >= totalStudents) {
currentStudent = 0; // Reset to the first student
}
}