0

I have an 8x8 LED array and a timer module connected to an Arduino. I can read the time as integers into minutes m1 and m2 and hours h1 and h2. I want to create a functon that can generate an 8x8 array of 1s and 0s that corresponds to the shape of number of the integer for m1, m2, h1, and h2 so that the array can be used to turn on or off the LEDs.

But I'm stuck with trying to return the array to the calling function and using it. I understand that a function in C can't return an array, and one has to use pointers, but I can't get my head around how to return an array and then use it. This is part of the code I have, it generates the error, "cannot convert 'int ()[8]' to 'int' for argument '2' to 'int* setLED(int, int*)'"

Can anyone point (no pun intended) me in the right direction?

int firstchar[8][8]; //declare array for first character
int character[8][8]; //declare array for generated character
int m1 = 2; //put in a test value for m1
int (*p_character)[8][8] = &character; //pointer to character array address

void setup() {
//setup serial monitor
Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
} // end of setup


void loop() {
int x;
int y;

setLED(m1,*p_character); //call function setLED to assign 8x8 array to character
//depending on value of m1

for (x = 0; x < 8; x++) { //do something with the returned character array
      for (y = 0; y < 8; y++) {
      firstchar[x][y] = *p_character[x][y];
}
}
//view results for test purposes
Serial.print("m1 = ");
Serial.print(m1);
Serial.println();
Serial.print("firstchar = ");
for (x = 0; x < 8; x++) {
      for (y = 0; y < 8; y++) {
Serial.print(firstchar[x][y]);
      }
}
Serial.println(); 
delay(3000);

} //end of loop

//Functions

int *setLED(int number, int *p_character) { //function to create the 8 element arrays
//depending on value of number
int character[8][8]; //declare local 8x8 array
if (number == 1)
  {
int character[8][8] = { //assign shape of character to array
{0,0,0,0,0,0,0,0}, //1
{0,0,0,0,1,1,1,0},
{0,0,0,1,0,1,1,0},
{0,0,1,0,0,1,1,0},
{0,1,0,0,0,1,1,0},
{0,0,0,0,0,1,1,0},
{0,0,0,0,0,1,1,0},
{0,0,0,0,0,0,0,0}
  };
  }
  else if (number == 2)
  {
int character[8][8] = {
{0,0,0,0,0,0,0,0}, //2
{0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0},
{0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0}
  };
  }
  else {
int character[8][8] = {
{1,1,1,1,1,1,1,1}, //T for test purposes if not 1 or 2 then character is T
{1,0,1,1,1,1,0,1},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0}
  };   
    }
return *p_character; //return array that corresponds to number 
}

1 Answers1

0

You seem to dereference the p_character when calling setLED this might be the problem. Also you dereference p_character when returning this also could lead to issues as it's probably not what you want. Try changing setLED to:

int (*setLED(int number, int (*p_character)[8][8]))[8][8]

This is not really necessary but the compiler should warn you about the diffrent types if you don't do it. And returning

return p_character;

not *p_character

  • Thanks, but I then get the error: cannot convert 'int (*)[8]' to 'int (*)[8][8]' for argument '2' to 'int* setLED(int, int (*)[8][8])' at line 25. – angeloftruth Feb 15 '21 at 17:59
  • did you change your call to setLED too? –  Feb 16 '21 at 12:20
  • Ah, no - but now I have, and I get the error at line 50 (the function definition): expected constructor, destructor, or type conversion before ';' token – angeloftruth Feb 17 '21 at 13:41