นี้คือโค้ดที่เขียนตัวมอตอร์ร่วมกับอย่างอื่น คือเราใช้ตัวมอเตอร์ร่วมกับจอแอลซีดีแล้วตัวมอเตอร์หมุนไม่หยุดแถมตัวหน้าจอแอลซีดีก็ขึ้นจาง ๆ หรือบางครั้งก็เออเร่อ วอนผู้รู้ช่วยบอกหน่อยค่ะ
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Password.h>
#define buzzer 12
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
String newPasswordString;
char newPassword[6];
byte a = 5;
bool value = true;
Password password = Password("1234"); //Enter your password
const String currentPassword = "1234";
const int maxPassword = 5;
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'D', 'C', 'B', 'A'},
{'#', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'},
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
servo.attach(11);
servo.write(50);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("WELCOME TO");
lcd.setCursor(0, 1);
lcd.print("HIGHPERFECTSAFETY");
delay(3000);
lcd.clear();
}
void loop() {
lcd.setCursor(1, 0);
lcd.print("ENTER PASSWORD");
char key = keypad.getKey();
if (key != NO_KEY) {
delay(60);
if (key == 'C') {
resetPassword();
} else if (key == 'D') {
if (value == true) {
dooropen();
value = false;
} else if (value == false) {
doorlocked();
value = true;
}
} else {
processNumberKey(key);
}
}
}
void processNumberKey(char key) {
static String enteredPassword = "";
lcd.setCursor(a, 1);
lcd.print("*");
a++;
if (a == 11) {
a = 5;
}
currentPasswordLength++;
password.append(key);
if (currentPasswordLength == maxPasswordLength) {
dooropen();
doorlocked();
}
if (key) {
if (key != 'D') {
enteredPassword += key;
} else {
if (enteredPassword == currentPassword) {
Serial.println("Password correct!");
} else {
Serial.println("Password incorrect!");
currentPasswordLength++;
if (currentPasswordLength >= maxPasswordLength) {
Serial.println("Maximum attempts reached!");
activateBuzzer();
delay(5000);
currentPasswordLength = 0;
} else {
activateBuzzer();
}
}
}
}
}
void activateBuzzer() {
tone(buzzer, 9000);
delay(9000);
noTone(buzzer);
}
void dooropen() {
if (password.evaluate()) {
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
servo.write(50);
delay(1000);
lcd.setCursor(0, 0);
lcd.print("CORRECT PASSWORD");
lcd.setCursor(0, 1);
lcd.print("OPEN THE DOOR");
delay(1000);
lcd.clear();
a = 5;
} else {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
lcd.setCursor(0, 1);
lcd.print("PLEASE TRY AGAIN");
delay(2000);
lcd.clear();
a = 5;
}
resetPassword();
}
void resetPassword() {
password.reset();
currentPasswordLength = 0;
lcd.clear();
a = 5;
}
//void changePassword() {
// newPasswordString = "1234";
// newPasswordString.toCharArray(newPassword, newPasswordString.length() + 1); //convert string to char array
// password.set(newPassword);
// resetPassword();
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Password changed");
// delay(1000);
// lcd.clear();
//}
void doorlocked() {
if (password.evaluate()) {
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
servo.write(90);
delay(100);
lcd.setCursor(0, 0);
lcd.print("CORRECT PASSWORD");
lcd.setCursor(2, 1);
lcd.print("DOOR LOCKED");
delay(2000);
lcd.clear();
a = 5;
} else {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
lcd.setCursor(0, 1);
lcd.print("PLEASE TRY AGAIN");
delay(2000);
lcd.clear();
a = 5;
}
resetPassword();
}
SG90 servo motor หมุนไม่หยุดทำยังไงดี
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Password.h>
#define buzzer 12
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
String newPasswordString;
char newPassword[6];
byte a = 5;
bool value = true;
Password password = Password("1234"); //Enter your password
const String currentPassword = "1234";
const int maxPassword = 5;
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'D', 'C', 'B', 'A'},
{'#', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'},
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
servo.attach(11);
servo.write(50);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("WELCOME TO");
lcd.setCursor(0, 1);
lcd.print("HIGHPERFECTSAFETY");
delay(3000);
lcd.clear();
}
void loop() {
lcd.setCursor(1, 0);
lcd.print("ENTER PASSWORD");
char key = keypad.getKey();
if (key != NO_KEY) {
delay(60);
if (key == 'C') {
resetPassword();
} else if (key == 'D') {
if (value == true) {
dooropen();
value = false;
} else if (value == false) {
doorlocked();
value = true;
}
} else {
processNumberKey(key);
}
}
}
void processNumberKey(char key) {
static String enteredPassword = "";
lcd.setCursor(a, 1);
lcd.print("*");
a++;
if (a == 11) {
a = 5;
}
currentPasswordLength++;
password.append(key);
if (currentPasswordLength == maxPasswordLength) {
dooropen();
doorlocked();
}
if (key) {
if (key != 'D') {
enteredPassword += key;
} else {
if (enteredPassword == currentPassword) {
Serial.println("Password correct!");
} else {
Serial.println("Password incorrect!");
currentPasswordLength++;
if (currentPasswordLength >= maxPasswordLength) {
Serial.println("Maximum attempts reached!");
activateBuzzer();
delay(5000);
currentPasswordLength = 0;
} else {
activateBuzzer();
}
}
}
}
}
void activateBuzzer() {
tone(buzzer, 9000);
delay(9000);
noTone(buzzer);
}
void dooropen() {
if (password.evaluate()) {
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
servo.write(50);
delay(1000);
lcd.setCursor(0, 0);
lcd.print("CORRECT PASSWORD");
lcd.setCursor(0, 1);
lcd.print("OPEN THE DOOR");
delay(1000);
lcd.clear();
a = 5;
} else {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
lcd.setCursor(0, 1);
lcd.print("PLEASE TRY AGAIN");
delay(2000);
lcd.clear();
a = 5;
}
resetPassword();
}
void resetPassword() {
password.reset();
currentPasswordLength = 0;
lcd.clear();
a = 5;
}
//void changePassword() {
// newPasswordString = "1234";
// newPasswordString.toCharArray(newPassword, newPasswordString.length() + 1); //convert string to char array
// password.set(newPassword);
// resetPassword();
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Password changed");
// delay(1000);
// lcd.clear();
//}
void doorlocked() {
if (password.evaluate()) {
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
servo.write(90);
delay(100);
lcd.setCursor(0, 0);
lcd.print("CORRECT PASSWORD");
lcd.setCursor(2, 1);
lcd.print("DOOR LOCKED");
delay(2000);
lcd.clear();
a = 5;
} else {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
lcd.setCursor(0, 0);
lcd.print("WRONG PASSWORD!");
lcd.setCursor(0, 1);
lcd.print("PLEASE TRY AGAIN");
delay(2000);
lcd.clear();
a = 5;
}
resetPassword();
}