#include <LiquidCrystal.h>
#include <Keypad.h>
// LCD引脚配置
LiquidCrystal lcd(19, 20, 11, 12, 13, 14); // 根据实际接线调整
// 4x4矩阵键盘配置
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] = {42, 41, 40, 39}; // 连接键盘行引脚
byte colPins[COLS] = {38, 37, 36, 35}; // 修正了之前的错误引脚号(363改为36)
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// 光敏电阻引脚
const int photoResistorPin = 1;
// LED指示灯引脚
const int redLedPin = 4; // 红灯引脚
const int greenLedPin = 5; // 绿灯引脚
// 系统变量
bool submitted[8] = {false}; // 学号1-8的提交状态
int baselineLight = 0; // 环境光基准值
bool detecting = true; // 是否在检测作业状态
unsigned long lastDisplayTime = 0;
const int displayInterval = 1000; // 状态显示间隔(毫秒)
int currentID = -1; // 当前输入的学生ID
void setup() {
Serial.begin(115200);
// 初始化LCD
lcd.begin(16, 2);
lcd.print("Automatic HW");
lcd.setCursor(0, 1);
lcd.print("Collector v1.0");
// 初始化光敏电阻
pinMode(photoResistorPin, INPUT);
// 初始化LED指示灯
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// 初始状态:红灯亮,绿灯灭
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
delay(1000);
baselineLight = analogRead(photoResistorPin); // 获取环境光基准
delay(2000);
lcd.clear();
}
void loop() {
// 更新指示灯状态
updateLEDs();
if (detecting) {
detectHomework();
displayStatusCycle();
} else {
getStudentID();
}
}
// 更新LED指示灯状态
void updateLEDs() {
if (detecting) {
// 检测模式:红灯亮,绿灯灭
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
} else {
// 输入模式:红灯灭,绿灯亮
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
}
}
// 检测作业下落
void detectHomework() {
static int lastLight = baselineLight;
static bool wasDark = false;
int currentLight = analogRead(photoResistorPin);
// 检测到光线变暗
if (currentLight > baselineLight + 1000 && !wasDark) {
wasDark = true;
Serial.println("Dark detected");
}
// 检测到光线恢复
if (wasDark && currentLight > baselineLight - 50) {
wasDark = false;
Serial.println("Light restored - homework detected");
homeworkDetected();
}
lastLight = currentLight;
delay(50);
}
// 检测到作业后的处理
void homeworkDetected() {
detecting = false;
lcd.clear();
lcd.print("Homework Detected");
lcd.setCursor(0, 1);
lcd.print("Enter ID(1-8):");
// 更新LED状态
updateLEDs();
}
void getStudentID() {
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
if (key >= '1' && key <= '8') {
currentID = key - '1'; // 保存当前输入
lcd.clear();
lcd.print("ID:");
lcd.print(key);
lcd.print(" Press D to confirm");
lcd.setCursor(0, 1);
lcd.print("or * to cancel");
}
else if (key == 'D' && currentID != -1) { // 添加currentID有效性检查
submitted[currentID] = true;
lcd.clear();
lcd.print("ID:");
lcd.print(currentID + 1);
lcd.print(" SUBMITTED");
delay(2000);
detecting = true;
currentID = -1;
lcd.clear();
// 更新LED状态
updateLEDs();
}
else if (key == '*') {
detecting = true;
currentID = -1;
lcd.clear();
// 更新LED状态
updateLEDs();
}
}
}
// 循环显示提交状态
void displayStatusCycle() {
static int displayID = 0;
if (millis() - lastDisplayTime > displayInterval) {
lastDisplayTime = millis();
lcd.clear();
lcd.print("ID:");
lcd.print(displayID + 1);
lcd.print(" Status:");
lcd.setCursor(0, 1);
lcd.print(submitted[displayID] ? "Y Submitted" : "N Not Submitted");
displayID = (displayID + 1) % 8;
}
}