// #include <U8g2lib.h>
// // Pin definitions
// int ledcf = 2,ledws = 3,ledkt = 4;
// int switchcf = 5,switchws = 6,switchkt = 7;
// int cfflag = 0, wsflag = 0,ktflag = 0,guangflag = 0;
// float luxconst = 0;
// const float GAMMA = 0.7;
// const float RL10 = 50;
// // OLED display setup
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /*SDA=*/ 4, /*SCL=*/ 5); // Set your actual SDA and SCL pins here
// void setup() {
// pinMode(ledcf, OUTPUT);
// pinMode(ledws, OUTPUT);
// pinMode(ledkt, OUTPUT);
// pinMode(switchcf, INPUT_PULLUP);
// pinMode(switchws, INPUT_PULLUP);
// pinMode(switchkt, INPUT_PULLUP);
// u8g2.begin();
// u8g2.enableUTF8Print();
// }
// void loop() {
// ledweitchcontr(); // Control LEDs based on switches
// ledguangcontr(); // Control LEDs based on light sensor
// display(); // Display temperature on OLED
// }
// void display() {
// const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// int analogValue = analogRead(A0);
// float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// u8g2.setFont(u8g2_font_unifont_t_chinese2);
// u8g2.setFontDirection(0);
// u8g2.clearBuffer();
// u8g2.setCursor(0, 15);
// u8g2.print("温度:");
// u8g2.setCursor(50, 15);
// u8g2.print(celsius);
// u8g2.sendBuffer();
// delay(1000);
// }
// void ledon(int pinnum) {
// digitalWrite(pinnum, HIGH);
// }
// void ledoff(int pinnum) {
// digitalWrite(pinnum, LOW);
// }
// void ledweitchcontr() {
// if (digitalRead(switchcf) == HIGH) {
// ledoff(ledcf);
// cfflag = 0;
// } else if (cfflag == 0) {
// ledon(ledcf);
// cfflag = 1;
// }
// if (digitalRead(switchws) == HIGH) {
// ledoff(ledws);
// wsflag = 0;
// } else if (wsflag == 0) {
// ledon(ledws);
// wsflag = 1;
// }
// if (digitalRead(switchkt) == HIGH) {
// ledoff(ledkt);
// ktflag = 0;
// } else if (ktflag == 0) {
// ledon(ledkt);
// ktflag = 1;
// }
// }
// void ledguangcontr() {
// int analogValue = analogRead(A0);
// float voltage = analogValue / 1024.0 * 5.0;
// float resistance = 2000 * voltage / (1 - voltage / 5.0);
// float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// if (luxconst <= 300 && lux > 300) {
// ledoff(ledcf);
// ledoff(ledws);
// ledoff(ledkt);
// luxconst = lux;
// }
// if (luxconst > 300 && lux <= 300) {
// luxconst = lux;
// }
// }
//*******************************************************************************
//*******************************************************************************
//温湿度模块 + 烟雾报警(大于20度)时报警
// #include "DHT.h"
// #define DHTPIN 2
// #define DHTTYPE DHT22
// DHT dht(DHTPIN, DHTTYPE);
// int led1 = 3; // LED引脚
// int buzz = 13; // 蜂鸣器引脚
// void setup() {
// Serial.begin(9600);
// Serial.println(F("DHTxx test!"));
// dht.begin();
// pinMode(led1, OUTPUT);
// pinMode(buzz, OUTPUT);
// }
// void loop() {
// delay(2000);
// float h = dht.readHumidity();
// float t = dht.readTemperature();
// if (isnan(h) || isnan(t)) {
// Serial.println(F("Failed to read from DHT sensor!"));
// return;
// }
// Serial.print(F("Humidity: "));
// Serial.print(h);
// Serial.print(F("% Temperature: "));
// Serial.print(t);
// Serial.println(F("°C"));
// // 检查温度是否大于20度
// if (t > 20) {
// tone(buzz, 1000); // 发出声音
// digitalWrite(led1, HIGH); // 点亮LED灯
// } else {
// noTone(buzz); // 停止发声
// digitalWrite(led1, LOW); // 熄灭LED灯
// }
// }
//*********************************************************************************
//*********************************************************************************
//门控制-密码锁(舵机控制)
#include <Keypad.h>
#include <Servo.h>
Servo myServo;
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] = {10, 8, 7, 6}; // 连接到键盘行的引脚
byte colPins[COLS] = {5, 4, 3, 2}; // 连接到键盘列的引脚
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "2536"; // 预设密码
String inputPassword;
void setup() {
myServo.attach(11);
Serial.begin(9600);
Serial.println("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'A') { // #表示提交
if (inputPassword == password) {
Serial.println("\nAccess Granted");
myServo.write(180);
} else {
Serial.println("\nAccess Denied");
myServo.write(0);
}
inputPassword = ""; // 清空输入
Serial.println("Enter Password:");
} else if (key == '*') { // *表示清除
inputPassword = "";
Serial.println("\nInput cleared");
Serial.println("Enter Password:");
}
else if (key == 'B') { // B表示退格(此处假设B键作为退格键)
if (inputPassword.length() > 0) {
inputPassword.remove(inputPassword.length() - 1); // 删除最后一个字符
Serial.print("\r"); // 回到行首
Serial.print(" "); // 清除当前行(假设最大输入长度为16)
Serial.print("\r"); // 再次回到行首
for (int i = 0; i < inputPassword.length(); i++) {
Serial.print(inputPassword[i]);
}
}
}
else { // 输入密码
inputPassword += key;
Serial.print(key);
}
}
}