简体   繁体   English

Arduino 8x8 LED矩阵不会关闭

[英]Arduino 8x8 LED matrix won't turn off

So i'm learning how to control an 8x8 LED matrix with an arduino, however for some reason my code isn't working. 因此,我正在学习如何使用arduino控制8x8 LED矩阵,但是由于某些原因,我的代码无法正常工作。 I've got 8 rows(each attached to it's own pin, from 12 - 5), and 4 columns(each with it's own pin, pins 0-3) working at the moment. 我现在有8行(每个连接到它自己的引脚,从12-5)和4列(每个都有自己的引脚,引脚0-3)正在工作。 I want to make a snake kind of design with my LEDs, so it moves diagonally. 我想用我的LED做出蛇形的设计,所以它会沿对角线移动。 the code was working, and then i decided to add two rows of code(which i've deleted now) and it's still not working. 该代码正在工作,然后我决定添加两行代码(现在我已删除了),但仍无法正常工作。 What happens is all of the LEDs light up perpetually, instead of one at a time. 发生的是所有LED永久点亮,而不是一次点亮。

EDIT: I know that use of delay is generally not good, as well as the fact i should have used a switch case, but I figured this was simple enough to not have to worry about it. 编辑:我知道使用延迟通常不好,以及我本应该使用开关盒的事实,但是我认为这很简单,不必担心它。

Here's the code: 这是代码:

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`

So I think at least one error is right here 所以我认为这里至少有一个错误

void setup() {  //runs once
//  initialize pins as outputs
  for(int pinnum; pinnum >= lastpin; pinnum--) 
  {
    pinMode(pinnum, OUTPUT);
  }

above this you set pinnum = 13 But then in your for loop you say for(int pinnum... 在此之上,您设置pinnum = 13但在您的for循环中您说for(int pinnum...

This reinitializes pinnum to 0 so your for loop is not exicuting. 这会将pinnum重新初始化为0,因此您的for循环不会出错。

You can test this therory here http://www.compileonline.com/compile_cpp_online.php 您可以在这里测试此理论http://www.compileonline.com/compile_cpp_online.php

Just copy and paste in the following and hit compile to see the difference 只需复制并粘贴以下内容,然后点击编译即可看到不同之处

//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;
}

Then try 然后尝试

//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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM