所以我正在学习如何使用 arduino 控制 8x8 LED 矩阵,但是由于某种原因,我的代码无法正常工作。我现在有 8 行(每行都连接到它自己的引脚,从 12 到 5)和 4 列(每列都有自己的引脚,引脚 0-3)。我想用我的 LED 做一个蛇形的设计,所以它对角线移动。代码正在工作,然后我决定添加两行代码(我现在已经删除了),但它仍然无法正常工作。所发生的情况是所有 LED 都会永久亮起,而不是一次亮起。
编辑:我知道延迟的使用通常不好,以及我应该使用开关盒的事实,但我认为这很简单,不必担心。
这是代码:
int pinnum = 13;
int lastpin = 0;
int col = 0;
int k;
void setup() { //runs once
// initialize pins as outputs
for(int pinnum; pinnum >= lastpin; pinnum--)
{
pinMode(pinnum, OUTPUT);
}
for(int i = 5; i <= 13; i++) //starts with all of them off
{
digitalWrite(i,LOW);
}
for(int i = 0; i <= 4; i++) //starts with all of them off
{
digitalWrite(i, HIGH);
}
}// END SETUP
void loop() {
pinon(12);
togglecol();
delay(1000);
pinon(11);
togglecol();
delay(1000);
pinon(10);
togglecol();
delay(1000);
pinon(9);
togglecol();
delay(1000);
pinon(8);
togglecol();
delay(1000);
pinon(7);
togglecol();
delay(1000);
pinon(6);
togglecol();
delay(1000);
pinon(5);
togglecol();
delay(1000);
}
void togglecol()
{
if(col % 4 == 1) //column = 1, pin 3
{
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}
else if(col % 4 == 2) //COLUMN = 2, PIN 2
{
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
else if(col % 4 == 3) //COLUMN = 3, PIN 1
{
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
else if(col % 4 == 0) // COLUMN 3, PIN 0
{
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
col++;
} //END TOGGLECOL
void pinon(int pin)
{
for(k = 5; k <= 13; k++) //turning all rows off
{
digitalWrite(k, LOW);
}
digitalWrite(pin, HIGH); //activating correct row again
}//END PINON`