Problems in Sketch ( please help )

I’m a newbie and can’t figure out how to fix curtain issues in this code, especially the (int i = 0; i < 8; i++) error which doesn’t let me use anything that has a loop. This would really help me if you replied, I’m open to any ideas.

FQ6TCSRH0A28ARP.ino (6.91 KB)

I recommend always setting File > Preferences > Compiler warnings: > All, File > Preferences > Show verbose output during: > compilation(uncheck). Then review the full contents of the console(black window at the bottom of the Arduino IDE window), you need to scroll up to see it all. It will often contain helpful hints. In this case we have:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_699651\sketch_dec11awert.ino: In function 'setup':

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_699651\sketch_dec11awert.ino:299:21: warning: iteration 5 invokes undefined behavior [-Waggressive-loop-optimizations]

    pinMode(rowPins[i], OUTPUT); // make all the LED pins outputs

                     ^

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_699651\sketch_dec11awert.ino:296:3: note: containing loop

   for (int i = 0; i < 8; i++)

   ^

C:\Program Files (x86)\arduino-nightly\hardware\arduino\avr\cores\arduino\main.cpp: In function 'main':

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_699651\sketch_dec11awert.ino:299:21: warning: iteration 5 invokes undefined behavior [-Waggressive-loop-optimizations]

    pinMode(rowPins[i], OUTPUT); // make all the LED pins outputs

                     ^

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_699651\sketch_dec11awert.ino:296:3: note: containing loop

   for (int i = 0; i < 8; i++)

   ^

So find the relevant lines in your code:

const int columnPins[] = {  9, 10, 11, 12, 13};
const int rowPins[] = { 8, 7, 6, 5, 4};
int incomingByte;

void setup() {

  for (int i = 0; i < 8; i++)
  {
    Serial.begin(9600);
    pinMode(rowPins[i], OUTPUT); // make all the LED pins outputs
    pinMode(columnPins[i], OUTPUT);
    digitalWrite(columnPins[i], HIGH); // disconnect column pins from Ground
  }
}

The arrays have 5 elements but your for loop is written for 8 elements, resulting in undefined behavior. The simple solution would be to change:

 for (int i = 0; i < 8; i++)

to:

 for (int i = 0; i < 5; i++)

If you want to automatically determine the number of loops required you can do something like this:

 for (int i = 0; i < sizeof(rowPins) / sizeof(rowPins[0]); i++)

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Thank you! This is just what I needed.

Why do you do a Serial.begin() 8 times? Once is enough :slight_smile:

If your code is small enough (maximum post size is around 9kB), please post it in the post using code tags

type
** **[code]** **

paste your code after that
type
** **[/code]** **
after the pasted code

I’ve done it for you; Ive also fixed up the indentations so it’s easier to read. Use tools → autoformat (or T) for that before posting code.

/*
  matrixMpxAnimation sketch
  animates two heart images to show a beating heart
*/
// the heart images are stored as bitmaps - each bit corresponds to an LED
// a 0 indicates the LED is off, 1 is on
byte P4[] =
{
  B01010,
  B10101,
  B10001,
  B01010,
  B00100
};

byte dw[] =
{
  B00000,
  B00000,
  B11111,
  B00000,
  B00000
};

byte op[] =
{
  B00100,
  B00100,
  B00100,
  B00100,
  B00100
};

byte fs[] =
{
  B00001,
  B00010,
  B00100,
  B01000,
  B10000
};

byte bs[] =
{
  B10000,
  B01000,
  B00100,
  B00010,
  B00001
};


byte A[] =
{
  B00100,
  B01010,
  B11111,
  B10001,
  B10001
};
byte B[] =
{
  B11110,
  B10001,
  B11110,
  B10001,
  B11110
};
byte C[] =
{
  B11111,
  B10000,
  B10000,
  B10000,
  B11111
};
byte D[] =
{
  B11110,
  B10001,
  B10001,
  B10001,
  B11110
};
byte E[] =
{
  B11111,
  B10000,
  B11110,
  B10000,
  B11111
};
byte F[] =
{
  B11111,
  B10000,
  B11100,
  B10000,
  B10000
};
byte G[] =
{
  B11111,
  B10000,
  B10011,
  B10001,
  B01110
};
byte H[] =
{
  B10001,
  B10001,
  B11111,
  B10001,
  B10001
};
byte I[] =
{
  B01110,
  B00100,
  B00100,
  B00100,
  B01110
};
byte J[] =
{
  B11111,
  B00001,
  B00001,
  B10001,
  B01110
};
byte K[] =
{
  B10001,
  B10010,
  B10100,
  B11010,
  B10001
};
byte L[] =
{
  B10000,
  B10000,
  B10000,
  B10000,
  B11110
};
byte M[] =
{
  B01010,
  B10101,
  B10101,
  B10001,
  B10001
};
byte N[] =
{
  B10001,
  B11001,
  B10101,
  B10011,
  B10001
};
byte O[] =
{
  B01110,
  B10001,
  B10001,
  B10001,
  B01110
};
byte P[] =
{
  B11110,
  B10001,
  B11110,
  B10000,
  B10000
};
byte Q[] =
{
  B01110,
  B10001,
  B10101,
  B01110,
  B00100
};
byte R[] =
{
  B11110,
  B10001,
  B11110,
  B10001,
  B10001
};
byte S[] =
{
  B01111,
  B10000,
  B01110,
  B00001,
  B11110
};
byte T[] =
{
  B11111,
  B00100,
  B00100,
  B00100,
  B00100
};
byte U[] =
{
  B10001,
  B10001,
  B10001,
  B10001,
  B01110
};

byte V[] =
{
  B10001,
  B10001,
  B10001,
  B01010,
  B00100
};
byte W[] =
{
  B10001,
  B10001,
  B10101,
  B10101,
  B01010
};
byte X[] =
{
  B10001,
  B01010,
  B00100,
  B01010,
  B10001
};
byte Y[] =
{
  B10001,
  B01010,
  B00100,
  B00100,
  B00100
};
byte Z[] =
{
  B11111,
  B00010,
  B00100,
  B01000,
  B11111
};
byte n1[] =
{
  B00100,
  B01100,
  B10100,
  B00100,
  B11111
};
byte n2[] =
{
  B01100,
  B10010,
  B00100,
  B01000,
  B11111
};
byte n3[] =
{
  B01110,
  B10001,
  B00110,
  B10001,
  B01110
};
byte n4[] =
{
  B00110,
  B01010,
  B11111,
  B00010,
  B00010
};
byte n5[] =
{
  B11111,
  B10000,
  B11110,
  B00001,
  B11110
};
byte n6[] =
{
  B01110,
  B10000,
  B11110,
  B10001,
  B01110
};
byte n7[] =
{
  B11111,
  B00010,
  B00100,
  B01000,
  B10000
};
byte n8[] =
{
  B01110,
  B10001,
  B01110,
  B10001,
  B01110
};
byte n9[] =
{
  B00111,
  B01001,
  B00111,
  B00001,
  B00001
};
byte n0[] =
{
  B01110,
  B11001,
  B10101,
  B10011,
  B01110
};

byte P5[] =
{
  B01010,
  B00000,
  B10001,
  B10001,
  B01110
};

byte P1[] =
{
  B10101,
  B10101,
  B10101,
  B00000,
  B10101
};

byte P2[] =
{
  B00000,
  B00100,
  B00000,
  B00100,
  B00000
};

byte P3[] =
{
  B00000,
  B00100,
  B00000,
  B00100,
  B00000
};


const int columnPins[] = {  9, 10, 11, 12, 13};
const int rowPins[] = { 8, 7, 6, 5, 4};
int incomingByte;

void setup()
{

  for (int i = 0; i < 8; i++)
  {
    Serial.begin(9600);
    pinMode(rowPins[i], OUTPUT); // make all the LED pins outputs
    pinMode(columnPins[i], OUTPUT);
    digitalWrite(columnPins[i], HIGH); // disconnect column pins from Ground
  }
}

void loop()
{
  // see if there's incoming serial data:
  if (Serial.available() > 0)
  {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();

    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'A')
    {
      show(A, 500);
    }
    if (incomingByte == 'B')
    {
      show(B, 500);
    }
    if (incomingByte == 'C')
    {
      show(C, 500);
    }
    if (incomingByte == 'D')
    {
      show(D, 500);
    }
    if (incomingByte == 'E')
    {
      show(E, 500);
    }
    if (incomingByte == 'F')
    {
      show(F, 500);
    }
    if (incomingByte == 'G')
    {
      show(G, 500);
    }
    if (incomingByte == 'H')
    {
      show(H, 500);
    }
    if (incomingByte == 'I')
    {
      show(I, 500);
    }
    if (incomingByte == 'J')
    {
      show(J, 500);
    }
    if (incomingByte == 'K')
    {
      show(K, 500);
    }
    if (incomingByte == 'L')
    {
      show(L, 500);
    }
    if (incomingByte == 'M')
    {
      show(M, 500);
    }
    if (incomingByte == 'N')
    {
      show(N, 500);
    }
    if (incomingByte == 'O')
    {
      show(O, 500);
    }
    if (incomingByte == 'P')
    {
      show(P, 500);
    }
    if (incomingByte == 'Q')
    {
      show(Q, 500);
    }
    if (incomingByte == 'R')
    {
      show(R, 500);
    }
    if (incomingByte == 'S')
    {
      show(S, 500);
    }
    if (incomingByte == 'T')
    {
      show(T, 500);
    }
    if (incomingByte == 'U')
    {
      show(U, 500);
    }
    if (incomingByte == 'V')
    {
      show(V, 500);
    }
    if (incomingByte == 'W')
    {
      show(W, 500);
    }
    if (incomingByte == 'X')
    {
      show(X, 500);
    }
    if (incomingByte == 'Y')
    {
      show(Y, 500);
    }
    if (incomingByte == 'Z')
    {
      show(Z, 500);
    }
    if (incomingByte == '0')
    {
      show(n0, 500);
    }
    if (incomingByte == '1')
    {
      show(n1, 500);
    }
    if (incomingByte == '2')
    {
      show(n2, 500);
    }
    if (incomingByte == '3')
    {
      show(n3, 500);
    }
    if (incomingByte == '4')
    {
      show(n4, 500);
    }
    if (incomingByte == '5')
    {
      show(n5, 500);
    }
    if (incomingByte == '6')
    {
      show(n6, 500);
    }
    if (incomingByte == '7')
    {
      show(n7, 500);
    }
    if (incomingByte == '8')
    {
      show(n8, 500);
    }
    if (incomingByte == '9')
    {
      show(n9, 500);
    }
    if (incomingByte == ' ')
    {
      delay(400);
    }
    if (incomingByte == '!')
    {
      show(P1, 500);
    }
    if (incomingByte == '&')
    {
      show(P5, 500);
    }
    if (incomingByte == '-')
    {
      show(dw, 500);
    }
    if (incomingByte == '|')
    {
      show(op, 500);
    }
    if (incomingByte == '/')
    {
      show(fs, 500);
    }
    if (incomingByte == '<')
    {
      show(bs, 500);
    }
    if (incomingByte == '%')
    {
      show(P4, 500);
    }

    if (incomingByte == ':')
    {
      show(P2, 500);
    }
    if (incomingByte == ';')
    {
      show(P3, 500);
    }

    if (incomingByte == 'a')
    {
      show(O, 500); show(W, 500);    delay(400);
      show(Y, 500); show(E, 500); show(S, 500);
    }




  }
}
// routine to show a frame of an image stored in the array pointed to by the image parameter.
// the frame is repeated for the given duration in milliseconds
void show( byte * image, unsigned long duration)
{
  unsigned long start = millis(); // begin timing the animation
  while (start + duration > millis()) // loop until the duration period has passed
  {
    for (int row = 0; row < 5; row++)
    {
      digitalWrite(rowPins[row], HIGH); // connect row to +5 volts
      for (int column = 0; column < 5; column++)
      {
        boolean pixel = bitRead(image[row], column);
        if (pixel == 1)
        {
          digitalWrite(columnPins[column], LOW); // connect column to Gnd
        }
        delayMicroseconds(300); // a small delay for each LED
        digitalWrite(columnPins[column], HIGH); // disconnect column from Gnd
      }
      digitalWrite(rowPins[row], LOW); // disconnect LEDs
    }
  }
}