第 1–50 行 · 读接线和三类参数
Uno R3 接线如下。LCD 电源接匹配的供电,所有模块共地。单颗 LED 串联限流电阻。
| 器件 | 脚位 |
|---|---|
| 红外反射传感器 Vout | D2,检测到手默认 LOW |
| 红、黄、绿灯 | D3、D4、D5 |
| 无源蜂鸣器 | D6 |
| 键盘 R1–R4 | D7、D8、D9、D10 |
| 键盘 C1–C4 | D11、D12、D13、A0,A0 此处当数字脚 |
| I²C LCD | SDA→A4,SCL→A5,地址 0x27 |
Wire 和 LiquidCrystal_I2C 管屏幕通信,string.h 提供字符串比较,stdio.h 提供文字格式化。KEY_LAYOUT 是行列到键值的对照表,索引从 0 开始,例如第 2 行第 3 列得到 '6'。键盘要求稳定 35 ms,红外要求稳定 40 ms。反应等待最多 10 秒,成绩显示 6 秒。记忆默认 4 位、每组 5 秒,共 3 组。
展开原代码第 1–50 行
1
2 #include <Wire.h>
3 #include <LiquidCrystal_I2C.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 // LAB 6: Visual reaction + Audio reaction + 3-number memory game
8 // Arduino Uno R3, IR reflective sensor, G/Y/R traffic LEDs,
9 // passive piezo buzzer, 4x4 matrix keypad and I2C LCD1602.
10 // No Keypad library is needed: the matrix is scanned in this sketch.
11
12 // ------------------- Wiring -------------------
13 const byte IR_PIN = 2; // IR module Vout
14 const byte RED_PIN = 3; // Traffic light R
15 const byte YELLOW_PIN = 4; // Traffic light Y
16 const byte GREEN_PIN = 5; // Traffic light G
17 const byte BUZZER_PIN = 6; // Low-current passive buzzer +
18
19 const byte ROW_PINS[4] = {7, 8, 9, 10}; // R1, R2, R3, R4
20 const byte COL_PINS[4] = {11, 12, 13, A0}; // C1, C2, C3, C4
21
22 const char KEY_LAYOUT[4][4] = {
23 {'1', '2', '3', 'A'},
24 {'4', '5', '6', 'B'},
25 {'7', '8', '9', 'C'},
26 {'*', '0', '#', 'D'}
27 };
28
29 // A4 = SDA, A5 = SCL (Arduino Uno R3)
30 LiquidCrystal_I2C lcd(0x27, 16, 2);
31
32 // Most 3-pin obstacle-sensing IR modules drive Vout LOW when detecting a hand.
33 // Change to HIGH if your module is active-high.
34 const byte IR_ACTIVE_LEVEL = LOW;
35
36 const unsigned long IR_DEBOUNCE_MS = 40;
37 const unsigned long KEY_DEBOUNCE_MS = 35;
38
39 // Reaction settings
40 const unsigned long RESPONSE_TIMEOUT_MS = 10000UL;
41 const unsigned long RESULT_DISPLAY_MS = 6000UL;
42
43 // Memory settings
44 // A/B = number length
45 // C/0 = display time
46 // D = Backspace
47 byte numberLength = 4;
48 byte showSeconds = 5;
49 const byte GROUP_COUNT = 3;
50