Hello again,
I'm trying to minimize my RAM usage, but I'dont understand much about PROGMEM yet, since I never tried it before. But I have one particular function (below) that eats 374 bytes of RAM that I think I could use PROGMEM.
void name(char *command, int value) {
int commandCode[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
char *commandName[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
int commandSize = sizeof(commandCode) / sizeof(commandCode[0]);
for (int i = 0; i < commandSize; i++) {
if (atoi(command) == commandCode[i]) {
command = commandName[i];
break;
}
}
Serial.print("Parameters: ");
Serial.print(command);
Serial.print(" = ");
if (value == 1) Serial.println("On");
else if (value == 0) Serial.println("Off");
}
I use this function to find an int value in the first array (commandCode), and print it's name equivalent from the second array (commandName).
I'm trying to store the arrays on the Flash Memory using the modified code above, but I don't know how to compare the first array (numbers) with the atoi(command) even using the info on the Arduino - PROGMEM page, it doesn't work.
void name(char *command, int value) {
static const int commandCode[] PROGMEM = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static const char commandName[][20] PROGMEM = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
int commandSize = sizeof(commandCode) / sizeof(commandCode[0]);
for (int i = 0; i < commandSize; i++) {
if (atoi(command) == pgm_read_word(commandCode[i])) {
strcpy_P(command, (char*)pgm_read_word(&(commandName[i])));
break;
}
}
Serial.print("Parameters: ");
Serial.print(command);
Serial.print(" = ");
if (value == 1) Serial.println("On");
else if (value == 0) Serial.println("Off");
}
When I parse command = 1 and value = 1 or command = 1 and value = 0 (for example) it print's:
Parameters: 1 = On
Parameters: 1 = Off
Can anyone point me what I'm doing wrong and how it should be done?