DanMan1:
Two questions that I think are related to strings and char:
- Although the code is working, I ge teh following error and am unsure why are how to fix it:
C:\Users\Documents\Arduino\Testlayout5e2\Testlayout5e2.ino:88:75: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char *Array2[ITEM_ARRAY_SIZE] = {"dog", "cat", "Horse", "Ant", "Firetruck"};
You can ignore that message without any problems, the compiler is telling you that the array has the type char * but you have populated it with strings (char arrays) instead of char pointers. The compiler understands that it should create the strings separately, then put the pointers to the strings in the array, but you can do it yourself if you really want to eliminate the warning. If you ever decide to store the text in PROGMEM, you will need to use this method, otherwise only the array of char * will be in PROGMEM, the actual text would be stored in dynamic memory.
char text0[] = "dog";
char text1[] = "cat";
char text2[] = "Horse";
char text3[] = "Ant";
char text4[] = "Firetruck";
char *Array2[ITEM_ARRAY_SIZE] = {text0, text1, text2, text3, text4};
- David, the following worked very well: type(Array2[keypad_value - 1]);
For future note, if I wanted to assign (Array2[keypad_value - 1]) to a variable and pass that variable into the "type" function. How would I do that? Type is looking for a string.
Since Array2 is an array of char pointers, the simplest way to assign (Array2[keypad_value - 1]) to a variable is to assign it to a char pointer:
char * testing = Array2[keypad_value - 1];
You could also just copy it to another char array and pass that to the function, but unless you plan on doing some editing of the text that is rather wasteful of memory.
As for the function itself, it is generally not a good idea to use the String type on an arduino, that often leads to problems with memory corruption, better to use null terminated char arrays (usually referred to as string with a lower-case "s"). You are only passing the char * to the function anyway, so might as well use that.
void type(char * message) {
///Usage type("dog");
char letter;
reset();
//message.toLowerCase();
//for (int i = 0; i < message.length(); i++) {
for (byte i = 0; i < strlen(message); i++) {
letter = message[i];
//convert upper case to lower case
// by adding the difference in ASCII representation
// between the upper case and lower case letters
if ((letter >= 'A') && (letter <= 'Z')) {
letter = letter + ('a' - 'A');
}
displayLetter(letter);
}
reset();
}