#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
// Initialize LCD with updated pin connections
LiquidCrystal lcd(PB0, PB1, PB3, PB4, PB5, PB6);
// Initialize Servo Motors
Servo servo1, servo2, servo3;
// Define the password
String password = "1234";
String input = "";
// LED and Buzzer pins
#define LED_PIN PB7
#define BUZZER_PIN PB8
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {PA0, PA1, PA2, PA3}; // Connect to row pinouts of the keypad
byte colPins[COLS] = {PA4, PA5, PA6, PA7}; // Connect to column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Enter Password:");
// Initialize LED and Buzzer
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
// Attach servo motors to pins
servo1.attach(PA8);
servo2.attach(PA9);
servo3.attach(PA10);
// Move servos to initial positions
servo1.write(0);
servo2.write(0);
servo3.write(0);
delay(500);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
checkPassword();
} else if (key == '*') {
input = "";
lcd.clear();
lcd.print("Enter Password:");
} else {
input += key;
lcd.setCursor(0, 1);
lcd.print(input);
}
}
}
void checkPassword() {
lcd.clear();
if (input == password) {
lcd.print("Access Granted");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer
delay(500); // Buzzer sound duration
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
delay(1000);
lcd.clear();
lcd.print("Dispensing...");
// Move servos to dispense medicine
servo1.write(90); // Dispense from servo 1
delay(1000);
servo1.write(0);
delay(1000);
servo2.write(90); // Dispense from servo 2
delay(1000);
servo2.write(0);
delay(1000);
servo3.write(90); // Dispense from servo 3
delay(1000);
servo3.write(0);
// Buzzer indication of successful dispensing
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
lcd.clear();
lcd.print("Completed");
delay(2000);
// Reset
input = "";
lcd.clear();
lcd.print("Enter Password:");
digitalWrite(LED_PIN, LOW);
} else {
lcd.print("Wrong Password");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
input = "";
}
}