There are 26 letters in the alphabet. In a computer, they are stored as numbers (that is the only thing a computer knows). The letter 'A' has the value 0x41 (hex; 65 decimal), 'B' has the value 0x42 (hex; 66 decimal).
You can find this at e.g. http://www.asciitable.com/
So to determine which led must be on, you can simply subtract the value from the letter.
// the duration that each character must be displayed
#define DISPLAYTIME 1000UL
void displayCharacters(char *txt)
{
// inde into character array
int index = 0;
// display led number for each character in txt till end of string is detected
while (txt[index] != '\0')
{
// // print character and led number
Serial.print("character: "); Serial.print(txt[index]);
Serial.print(", led: "); Serial.println(txt[index] - 'A');
// prepare for next character
index++;
// display character for specified time
delay(DISPLAYTIME);
}
Serial.println("DONE");
}
void setup()
{
char text[] = "ABC";
Serial.begin(115200);
displayCharacters(text);
}
void loop()
{
}
Note: computers count from zero, so led 0 is the first led
The above code uses a function to display the characters. One of the advantages of the use of the function is that you only have to change that function based on the hardware that you decide to use.
The code uses delay to implement the wait time that a character must be displayed. The disadvantage is that this blocks processing, so if you e.g. need to process a button press to cancel, the detection of the button press will only happen once all characters are displayed. We can take that hurdle when needed
You might also want to add a visual indication to separate words (e.g. a longer delay); the below displayCharacters() function handles that.
void displayCharacters(char *txt)
{
// index into character array
int index = 0;
// display led number for each character in txt till end of string is detected
while (txt[index] != '\0')
{
if (txt[index] == ' ')
{
// wait longer for a space
delay(DISPLAYTIME * 5);
}
else
{
// print character and led number
Serial.print("character: "); Serial.print(txt[index]);
Serial.print(", led: "); Serial.println(txt[index] - 'A');
// wait a little while
delay(DISPLAYTIME);
// for 'stranger things', clear all leds
Serial.println("leds cleared");
}
// prepare for next character
index++;
}
}
Lastly, you can use multiple texts. You can do this by using an array (of pointers to) texts.
// the duration that each character must be displayed
#define DISPLAYTIME 1000UL
// the texts to display; you can expand 'unlimited'
char *texts[] =
{
"HELLO WORLD",
"STRANGER THINGS",
};
Your setup can now basically be empty as the displaying will be done from loop().
void displayCharacters(char *txt)
{
// as is
...
...
}
void setup()
{
Serial.begin(115200);
Serial.print("Number of texts: "); Serial.println(sizeof(texts) / sizeof(texts[0]));
}
void loop()
{
// keep track which text to display
static int index = 0;
Serial.print("Sending '"); Serial.print(texts[index]); Serial.println("'");
// display indicated text; also increment the index so the next time the next text will be displayed
displayCharacters(texts[index++]);
// delay between texts (10 seconds)
delay(10000);
// if end of texts is reached, reset
if (index >= sizeof(texts) / sizeof(texts[0]))
{
index = 0;
}
}
Hope this gets you a little on track.
Depending on needs, this can require further finetuning; getting rid of delays is one and possibly storing texts in eeprom would be a nice feature.