#define ROWS 6
#define COLS 4
//Matrix of output pins
int ledMatrix[4][6] = {
{22, 23, 24, 25, 26, 27},
{28, 29, 30, 31, 32, 33},
{34, 35, 36, 37, 38, 39},
{40, 41, 42, 43, 44, 45}
};
//pin matrix we operate
int ledMat[ROWS][COLS];
//state of pin matrix
bool ledStat[ROWS][COLS];
//position of cursor
int cursorRow = 0; //当前选择LED的光标位置
int cursorCol = 0;
//column that user has choosen
int column = 0;
//pins of pushButtons
const int inputPin = 2; // input按钮引脚
const int zeroPin = 3; // zero按钮引脚
const int jumpPin = 4; // jump按钮引脚
void setup() {
Serial.begin(9600);
int column; //colum number
// put your setup code here, to run once:
for (int i = 0; i < 4; i++) { // 4 列
for (int j = 0; j < 6; j++) { // 6 行
ledMat[j][i] = ledMatrix[i][j];
}
}
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
pinMode(ledMat[row][col], OUTPUT);
ledStat[row][col] = 0;
}
}
pinMode(inputPin, INPUT_PULLUP);
pinMode(zeroPin, INPUT_PULLUP);
pinMode(jumpPin, INPUT_PULLUP);
}
void loop() {
Serial.print("This system is used to fill in positions in a table!\n");
Serial.print("Please select a colum(1~4):\n");
while(Serial.available() == 0) {}
column = Serial.parseInt();
while (Serial.available()) { Serial.read();}//丢掉换行符
Serial.println(column);
cursorRow = 0; // 光标初始在第一行
cursorCol = column - 1; // 用户输入的列数转换为索引(0-3)
if (column == 0) { //如果输入的是字符char,也将结束循环,因为默认输入的是数字
Serial.println("No table to be filled in, code ending!");
while (true) {} // 结束程序,进入无限循环
}
if (column < 1 || column > 4)
{
Serial.println("Invalid input! Please enter a number between 1 and 4.");
Serial.println("_____________________________________________________");
delay(1000);
return; // 如果输入无效,直接返回,等待下一次输入
}
while(1)
{
updateLEDs(ledStat, cursorRow, cursorCol);
inputPB();
zeroPB();
Serial.println("Waiting for button press...");
delay(100);
}
}
void updateLEDs(bool ledStat[ROWS][COLS], int cursorRow, int cursorCol) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (row == cursorRow && col == cursorCol) {
// 光标LED闪烁
blink(ledMat[row][col], 100);
} else {
// 根据ledStat状态点亮或熄灭LED
digitalWrite(ledMat[row][col], ledStat[row][col] ? HIGH : LOW);
}
}
}
}
void blink(int led, long seconds)
{
digitalWrite(led, HIGH);
delay(seconds);
digitalWrite(led, LOW);
delay(seconds);
}
void inputPB() {
if (digitalRead(inputPin) == LOW) { // 检测按键是否按下(低电平有效)
delay(50); // 简单去抖动
if (digitalRead(inputPin) == LOW) { // 再次确认按键按下
// 1. 点亮当前光标位置的LED
ledStat[cursorRow][cursorCol] = 1;
// 2. 更新光标位置
if (cursorRow < ROWS - 1) {
// 如果不是最后一行,光标移动到下一行
cursorRow++;
} else {
// 如果是最后一行,光标移动到下一列的第一行
cursorRow = 0;
cursorCol = (cursorCol + 1) % COLS; // 如果已经是最后一列,回到第一列
}
Serial.println("Input button pressed: LED turned ON and cursor moved.");
while (digitalRead(inputPin) == LOW) {} // 等待按键释放
}
}
}
void zeroPB() {
if (digitalRead(zeroPin) == LOW) { // 检测按键是否按下(低电平有效)
delay(50); // 简单去抖动
if (digitalRead(zeroPin) == LOW) { // 再次确认按键按下
// 1. 熄灭当前光标位置的LED
ledStat[cursorRow][cursorCol] = 0;
// 2. 更新光标位置
if (cursorRow < ROWS - 1) {
// 如果不是最后一行,光标移动到下一行
cursorRow++;
} else {
// 如果是最后一行,光标移动到下一列的第一行
cursorRow = 0;
cursorCol = (cursorCol + 1) % COLS; // 如果已经是最后一列,回到第一列
}
Serial.println("Zero button pressed: LED turned OFF and cursor moved.");
while (digitalRead(zeroPin) == LOW) {} // 等待按键释放
}
}
}