So i'm learning how to control an 8x8 LED matrix with an arduino, however for some reason my code isn't working. 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. I want to make a snake kind of design with my LEDs, so it moves diagonally. 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.
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...
This reinitializes pinnum to 0 so your for loop is not exicuting.
You can test this therory here 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;
}