Hello,
I am trying to store an array of strings to have the Arduino dynamically come up with a name. I tried doing it with SRAM, but was having trouble with my program not running with no error generated. It turns out that my string arrays containing the names were too big for the memory. So, I switched to PROGMEM and everything loads fine, but now doesn't retrieve the names from the array correctly.
Here is my code:
#include <avr/pgmspace.h>
char* name_male[] PROGMEM = {"Tony", "Warren", "Wendell", "Will", "William"};
char* name_last[] PROGMEM = {"Vinsant", "Ward", "Wheeler", "Wolfe", "Young"};
#define array_male_length ((sizeof(name_male)/sizeof(char *)))
#define array_last_length ((sizeof(name_last)/sizeof(char *)))
char* first_name;
char* last_name;
int randomNumber;
int index;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(index < array_last_length)
{
generateName();
}
}
void generateName()
{
randomNumber = random(5);
first_name = name_male[randomNumber];
last_name = name_last[randomNumber];
Serial.print("\nIndex:");
Serial.print(index);
Serial.print("\t Random:");
Serial.println(randomNumber);
Serial.print("Indexed name is ");
Serial.print(name_male[index]);
Serial.print(" ");
Serial.println(name_last[index]);
Serial.print("Generated name is ");
Serial.print(first_name);
Serial.print(" ");
Serial.println(last_name);
Serial.print("Direct name is ");
Serial.print(name_male[0]);
Serial.print(" ");
Serial.println(name_last[0]);
index++;
delay(1000);
}
and here is the output:
Index:0 Random:2
Indexed name is m:2
Indexed name is
Generated name is
Direct name is Tony Vinsant
Index:1 Random:4
Indexed name is ×ÿ÷ÿÿÿ is ×ÿ÷ÿÿÿ is ×ÿ÷ÿÿÿ is ×ÿ÷ÿÿÿ
Generated name is
Direct name is Tony Vinsant
Index:2 Random:3
Indexed name is Random:3
Indexed name is Random:3
Indexed namee
Generated name is
Direct name is Tony Vinsant
Index:3 Random:3
Indexed name is
Generated name is
Direct name is Tony Vinsant
Index:4 Random:0
Indexed name is
Generated name is
Direct name is Tony Vinsant
It works if I address the array element directly, but not when I dynamically point to the array. Any ideas? I feel like I have tried everything. I am getting some crazy Serial output, so my guess is it is another memory issue.
Thanks!
Ryan