Hi!
I have a function that is meant to return an array with 180 (NUM) values from 0-255. Those values are provided as the parameter modifier and returned as a pointer. When walk through this array like this:
...
#define NUM=190;
char modes[3];
...
void loop() {
char vals[15];
callMode(0,vals);
}
void callMode(int selectedMode, char vals[15]) {
char matrixClr[NUM];
char *matrixPointer;
matrixPointer=getMatrixByMode(byte(modes[0]),1,vals);
//Serial Part 3
for(int i=0; i<NUM;i++) {
matrixClr[i]=matrixPointer[i];
}
}
char* getMatrixByMode(int mode, int channel, char vals[15]) {
int modificator=66;
char* matrixArray;
char myArray[NUM];
switch(mode) {
case 65:
matrixArray=styleSingle(vals,modificator);
for(int i=0; i<NUM;i++) {
myArray[i]=matrixArray[i];
}
//Serial Part 2
for(int i=0; i<NUM;i++) {
Serial.print(myArray[i]);
}
Serial.println("");
break;
}
return myArray;
}
char *styleSingle(char musicVals[15], uint8_t modifier) {
char matrix[NUM];
for(int i=0;i<NUM;i++) {
matrix[i]=modifier;
}
//Serial Part 1
for(int i=0;i<NUM;i++) {
Serial.print(matrix[i]);
}
Serial.println("---");
return matrix;
}
When I remove the code in Serial Part 1 it still works as long as i keep the code underneath Serial Part 2. If I remove that code as well I get a different result.
EXPECTED RESULT: Is a row with 190 B's (as B is the Value for 66). This result is achieved when Serial Part 1 or Part 2 is left inside the code.
RESULT WITHOUT Serial.print loops as underneath //Serial Part 1/2 : Something like this:
:dÿÿ¾2?h?>?ØÀFH2»LQ?b«KBABSVARSVARS:SAT=BRI=CL
and that is not equal to BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
Does anyone have an idea what causes this strange behavior? I suspect it to be some sort of memory issue or pointer based problem, but I can't seem to get it fixed. In previous versions I passed the pointer directly in the getMatrixByMode, but that did not work either, so i added myMatrix[] since I thought this way the values would be copied to a different space in memory, but that doesn't seem to work either.
On a side-note: In a previous Version I was not using getMatrixByMode but I requested the matrix directly in callMode like so:
void callMode(int selectedMode, char vals[15]) {
switch(selectedMode) {
case MODE_KILLER:
copyMatrix(styleSingle(musicVals, sSaturation),2);
break;
}
and that always worked. Problems seem to start with getMatrixByMode, but I can't see what I'm doing wrong.
Statistics:
I am on an Arduino Mega using this code to check on free RAM:
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
It returns some 5k+ RAM available, and it seems legit to me. Entire code size is 20k (256k available)
Need your help ![]()