Hi all,
I hope this finds everyone well.
This is my first project and I have zero programming experience so please go easy on me!!
I pinched some code for a 3x3 matrix which I built and it has worked well. im now trying to build a 5x5 matrix so I have changed the code to suit the extra leds....
Old code....
int row[] = {5, 6, 7};
int col[] = {2, 3, 4};
int row1[] = {-1, -1, -1}; //This set of array's represents the grid of LED's. The program checks through these lists to se
int row2[] = {-1, -1, -1};
int row3[] = {-1, -1, -1};
//int light[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int counter = 0;
int ledpins[] = {0,0};
int *ptr;
void setup(){
for(int i = 2 ; i < 8 ; i++){ //Initializes all the pins
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
Serial.begin(9600);
}
void loop(){
animate();
readInput();
}
void animate(){ //The 'animate' function takes the arrays for each row of LED's and uses them to light one row of LED's at a time
for (int r = 0 ; r < 3 ;r ++){
switch(r){ //The switch tells the function which row to read
case(0):
ptr = &row1[0];
break;
case(1):
ptr = &row2[0];
break;
case(2):
ptr = &row3[0];
break;
}
for (int i = 0 ; i < 3 ; i ++){
if (*(ptr + i) == 1){
digitalWrite(i + 2, HIGH);
}
else{
digitalWrite(i + 2, LOW);
}
}
if (row[r] != 5){ //Rows are lit by setting the ground pin to 'LOW' and the positiv pins to 'HIGH'
digitalWrite(row[r]-1, HIGH);
}
else{
digitalWrite(row[2], HIGH);
}
digitalWrite(row[r], LOW);
delay(5); // You can increase the delay if you want to see how the LED's are cycled. 100 is a good value for demonstrating
}
}
int updateFrame(int led){ //This function updates arrays 'row1', 'row2', and 'row3' according to the input
int counter = 0; // input is in the form if an integer, from 0-8, corrensponding to the 9 LED's
for (int y = 0 ; y < 3 ; y++){ //The for loops convert the integer input to the LED's coordinates, from (0,0) to (8,8)
for (int x = 0 ; x < 3 ; x ++){
if(counter == led){
int ledpins[] = {x, y};
switch(y){
case 0:
row1[x] = row1[x] * -1;
return 1;
case 1:
row2[x] = row2[x] * -1;
return 1;
case 2:
row3[x] = row3[x] * -1;
return 1;
}
counter ++;
}
counter ++;
}
}
}
void readInput(){ //This function interprets the serial input and, depending on the input, passes it through the function 'updateFrame'
if (Serial.available() > 0) {
int input = Serial.read();
if(input == 33){ //Inputting '!' (ascii 33) resets all the LED's to "off"
for(int x = 0 ; x < 3 ; x++){
row1[x] = -1;
row2[x] = -1;
row3[x] = -1;
}
}
else{
updateFrame(int((input)) - 48);
}
//Serial.println(input);
}
}
New code.....
int row[] = {7, 8, 9, 10, 11};
int col[] = {2, 3, 4, 5, 6};
int row1[] = {-1, -1, -1, -1, -1}; //This set of array's represents the grid of LED's. The program checks through these lists to se
int row2[] = {-1, -1, -1, -1, -1};
int row3[] = {-1, -1, -1, -1, -1};
int row4[] = {-1, -1, -1, -1, -1};
int row5[] = {-1, -1, -1, -1, -1};
//int light[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int counter = 0;
int ledpins[] = {0,0};
int *ptr;
void setup(){
for(int i = 2 ; i < 11 ; i++){ //Initializes all the pins
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
Serial.begin(9600);
}
void loop(){
animate();
readInput();
}
void animate(){ //The 'animate' function takes the arrays for each row of LED's and uses them to light one row of LED's at a time
for (int r = 0 ; r < 3 ;r ++){
switch(r){ //The switch tells the function which row to read
case(0):
ptr = &row1[0];
break;
case(1):
ptr = &row2[0];
break;
case(2):
ptr = &row3[0];
break;
case(3):
ptr = &row4[0];
break;
case(4):
ptr = &row5[0];
break;
}
for (int i = 0 ; i < 3 ; i ++){
if (*(ptr + i) == 1){
digitalWrite(i + 2, HIGH);
}
else{
digitalWrite(i + 2, LOW);
}
}
if (row[r] != 5){ //Rows are lit by setting the ground pin to 'LOW' and the positiv pins to 'HIGH'
digitalWrite(row[r]-1, HIGH);
}
else{
digitalWrite(row[2], HIGH);
}
digitalWrite(row[r], LOW);
delay(5); // You can increase the delay if you want to see how the LED's are cycled. 100 is a good value for demonstrating
}
}
int updateFrame(int led){ //This function updates arrays 'row1', 'row2', and 'row3' according to the input
int counter = 0; // input is in the form if an integer, from 0-8, corrensponding to the 9 LED's
for (int y = 0 ; y < 3 ; y++){ //The for loops convert the integer input to the LED's coordinates, from (0,0) to (8,8)
for (int x = 0 ; x < 3 ; x ++){
if(counter == led){
int ledpins[] = {x, y};
switch(y){
case 0:
row1[x] = row1[x] * -1;
return 1;
case 1:
row2[x] = row2[x] * -1;
return 1;
case 2:
row3[x] = row3[x] * -1;
return 1;
case 3:
row4[x] = row4[x] * -1;
return 1;
case 4:
row5[x] = row5[x] * -1;
return 1;
}
counter ++;
}
counter ++;
}
}
}
void readInput(){ //This function interprets the serial input and, depending on the input, passes it through the function 'updateFrame'
if (Serial.available() > 0) {
int input = Serial.read();
if(input == 33){ //Inputting '!' (ascii 33) resets all the LED's to "off"
for(int x = 0 ; x < 3 ; x++){
row1[x] = -1;
row2[x] = -1;
row3[x] = -1;
row4[x] = -1;
row5[x] = -1;
}
}
else{
updateFrame(int((input)) - 48);
}
//Serial.println(input);
}
}
My question is two-fold really...
- Is the new code ok or is there anything that I should look out for with this type of code?
- If you imagine a grid with A - E across the top and 1 - 5 going down, leds D3, D4, E2, E3, & E4 or permanently on. Is this right?
Thanks for any help you can offer
Alex