int led[4][4]={{2,3,4,5},{6,7,8,9}};
void setup() {
for(int i = 2; i<=10;i++)
{
pinMode(i,OUTPUT);
}
// put your setup code here, to run once:
}
void loop() {
for(int rows = 2; rows <= 5; rows++)
{
for(int colums = 6; colums <= 9; colums++)
{
digitalWrite(led[rows][colums],HIGH);
delay(500);
digitalWrite(led[rows][colums],LOW);
}
}
// put your main code here, to run repeatedly:
}
this is my code what am i doing wrong?
Welcome to the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
int led[4][4]={{2,3,4,5},{6,7,8,9}};0
This array has 4 rows numbered 0 to 3 and 4 columns numbered 0 to 3
Your for loops use values from 2 to 5 and 6 to 9 as the array index values so are accessing memory outside of the array
gcjr
4
look this over
output
0 1 2
10 11 12
20 21 22
30 31 32
40 41 42
const int Nrow = 5;
const int Ncol = 3;
int matrix [5][3] = {
{ 0, 1, 2 },
{ 10, 11, 12 },
{ 20, 21, 22 },
{ 30, 31, 32 },
{ 40, 41, 42 },
};
void setup ()
{
Serial.begin (9600);
for (int row = 0; row < Nrow; row++) {
for (int col = 0; col < Ncol; col++) {
Serial.print (" ");
Serial.print (matrix [row][col]);
}
Serial.println ();
}
}
void loop () {
}
What exactly are you trying to do? is it a 4x4 array of LEDs or a 2x4 array?
This {{2,3,4,5},{6,7,8,9}}; is 2x4
Arrays also start at 0 index so both of your for loops are incorrect.
You also have a single delay instead of another after you turn off the LEDs.
Try this instead.
for(int row = 0; row < 2; row++) {
for(int column = 0; column < 4; column++) {
digitalWrite(led[row][column],HIGH);
delay(500);
digitalWrite(led[row][column],LOW);
delay(500);
}
}
J-M-L
6
either you use the pins in the array, or you just use the for loops indexes as PIN numbers
for the setup it would be like this
void setup() {
for(int i = 2; i<=10;i++) pinMode(i,OUTPUT); // pin D2 to D10 as output
}