I am working in a little project that I have been coerced into.
Its a simple word and sentence construction learning aid for a friends children.
Eventually this will have multiple functions but one step at a time.
It will be primarily controlled with a rotary encoder, but is using a simple number incrementor for the time being.
I am struggling to find a way for the Arduino to automatically update the number of string in the array. It is currently set to five - with with 6 words in the string array.
String words[]={"Me", "Hello", "Chicken", "Saw", "You", "Job"};
int which_word = 0;
String i;
int x;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(words[which_word]); //find word
Serial.print(" ");
x = words[which_word].length(); //count number of letter in string
Serial.println(x);
which_word++;
//check for overflow.
//if this can be auto updated when words are added or removed, it would simplify use for the end user.
if (which_word >5){
which_word=0;
}
delay(1000);
}
At this point it will be a case of putting as many words in as possible, but I am trying to make as simple as adding a new word to the firmware and hitting upload if new words are needed (or for the that matter removed) as my friend isn't the most computer literate.
const char* words[] = {"Me", "Hello", "Chicken", "Saw", "You", "Job"};
const size_t nbWords = sizeof words / sizeof words[0];
size_t which_word = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(words[which_word]); //find word
Serial.print(" ");
Serial.println(strlen(words[which_word]));//count number of letter in string
which_word++;
//check for overflow.
//if this can be auto updated when words are added or removed, it would simplify use for the end user.
if (which_word >= nbWords) { // careful use >= not just >
which_word = 0;
}
delay(1000);
}
(don't use the String class if there is no real strong reason for doing so, don't go slow 9600 is something of the past).
the sizeof operator gives you the number of bytes used by a variable or a type. so if you look at the total memory used by you array of pointers to words and divide this by the size of one such pointer, you get the count of elements in the array. (works with all types of arrays)
size_t is the type usually used for the size of something (unsigned integral number).
Be careful as well to test the overflow, index starts at 0 and goes to count-1 so use >[color=red]=[/color] not just >
if (which_word >= nbWords) { // careful use >= not just >
That worked perfectly. Thank you so much.
I will try and use faster serial speed, its force of habit.
Edit: Just tested with 39 words and it appears to be working perfectly.
I'm sure there will be more to be added, but onto the next part of the project.
Thanks again.
if you have a lot of words you could consider storing them into FLASH memory (PROGMEM) to save RAM space (if you are on a small arduino)
An easy way to do so would be to allocate a fixed size array of bytes for the words - only part of which will be used by the actual words.
That could look like this:
const byte maxWordLength = 10; // make sure no word is longer than this
struct t_data {
char word[maxWordLength + 1]; // +1 for the trailing null char
// you could want to store other stuff here
};
static const t_data words[] PROGMEM = {
{"Me"}, // first structure in the array
{"Hello"}, // second one
{"Chicken"},
{"Saw"},
{"You"},
{"Job"}
};
const size_t nbWords = sizeof words / sizeof words[0];
size_t which_word = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print((__FlashStringHelper*) words[which_word].word); //find word. note the use of __FlashStringHelper* to deal with PROGMEM stuff
Serial.print(" ");
Serial.println(strlen_P(words[which_word].word)); //strlen_P (note the _P) count number of letter in string in PROGMEM
which_word++;
if (which_word >= nbWords) {
which_word = 0;
}
delay(1000);
}