所以我正在学习如何使用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`
答案 0 :(得分:1)
所以我认为至少有一个错误就在这里
void setup() { //runs once
// initialize pins as outputs
for(int pinnum; pinnum >= lastpin; pinnum--)
{
pinMode(pinnum, OUTPUT);
}
在此之上设置pinnum = 13
然后在你的for循环中你说for(int pinnum...
这会将pinnum重新初始化为0,因此您的for循环不会显示出来。
你可以在http://www.compileonline.com/compile_cpp_online.php
测试这个词只需复制并粘贴以下内容并点击编译即可查看差异
//Working for loop
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
int a = 10;
for(a; a>0; a--){
cout << a << endl;
}
return 0;
}
然后尝试
//For loop like yours
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
int a = 10;
for(int a; a>0; a--){
cout << a << endl;
}
return 0;
}