Estoy teniendo problemas para desde una función devolver un array de caracteres.
Cada vez que uso la función, todas las variables que han usado la función actualizan su valor a la ultima ejecución.
Entiendo que es un problema de como trata la memoria Arduino pero no consigo resolverlo. Con otros tipos de variables, esto no me sucede..
Un ejemplo de código donde reproduzco el problema:
char* randChar() {
static char randChar[10];
const char *letters = "abcdefghijklmnopqrstuvwxyz0123456789";
for(int i = 0; i<10; i++) {
randChar[i] = letters[random(0, 36)];
}
return randChar;
}
int randInt() {
return random(0,1000);
}
String randStr() {
String randString;
const char *letters = "abcdefghijklmnopqrstuvwxyz0123456789";
for(int i = 0; i<10; i++) {
randString = randString + letters[random(0, 36)];
}
return randString;
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("[char]");
char * chrA = randChar();
Serial.println((String)" chrA: "+chrA);
char * chrB = randChar();
Serial.println((String)" chrB: "+chrB);
Serial.println((String)" chrA: "+chrA+" chrB: "+chrB);
Serial.println("\n[int]");
int intA = randInt();
Serial.println((String)" intA: "+intA);
int intB = randInt();
Serial.println((String)" intB: "+intB);
Serial.println((String)" intA: "+intA+" intB: "+intB);
Serial.println("\n[String]");
String strA = randStr();
Serial.println((String)" strA: "+strA);
String strB = randStr();
Serial.println((String)" strB: "+strB);
Serial.println((String)" strA: "+strA+" strB: "+strB);
}
void loop() {
// put your main code here, to run repeatedly:
}
La salida:
[char]
chrA: rswyfz3whc
chrB: gokp808225
chrA: gokp808225 chrB: gokp808225
[int]
intA: 995
intB: 825
intA: 995 intB: 825
[String]
strA: fufbm68246
strB: eyo2o7rthr
strA: fufbm68246 strB: eyo2o7rthr
Es en la tercera línea donde se puede ver el error, entiendo que chrA y chrB deberían de tener valores diferentes.