0

I'm a C/C++ beginner and trying to build a simple script. I'm running this on an Arduino.

#include <Arduino.h>

int pin_locations[3][3] = {
  {8,  5, 4},
  {9,  6, 3},
  {10, 7, 2}
};

void setup() {
  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j) {
      pinMode(pin_locations[i][j], OUTPUT);
    }
  }
}

void convert_drawing_to_LEDS(int drawing[]) {
  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j) {
      if (drawing[i][j] == 1) {
        digitalWrite(pin_locations[i][j], HIGH);
      }
    }
  }
}

void loop() {
  // drawing
  int my_drawing[3][3] = {
    {1, 1, 1},
    {1, 0, 1},
    {1, 1, 1}
  };

  convert_drawing_to_LEDS(my_drawing);

}

But it gives me two errors:

src/main.cpp: In function 'void convert_drawing_to_LEDS(int*)': src/main.cpp:31:23: error: invalid types 'int[int]' for array subscript if (drawing[i][j] == 1) { ^ src/main.cpp: In function 'void loop()': src/main.cpp:46:37: error: cannot convert 'int ()[3]' to 'int' for argument '1' to 'void convert_drawing_to_LEDS(int*)'
convert_drawing_to_LEDS(my_drawing); ^ Compiling .pio/build/uno/FrameworkArduino/WInterrupts.c.o *** [.pio/build/uno/src/main.cpp.o] Error

Can anyone help me?

Thanks!

1
  • 4
    there is no language called C/C++. C and C++ are two different languages. Afaik arduino is C++ Commented Mar 8, 2022 at 13:09

1 Answer 1

4

You've declared convert_drawing_to_LEDS to take an int [] which is not a 2D array. This causes a mismatch between the parameter and how its being used in the function, and also a mismatch between the actual parameter being passed in and what the function is expecting.

You instead want:

void convert_drawing_to_LEDS(int drawing[3][3]) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.