Pour le switch/case:
--> mettre tous les mots possibles dans un tableau et parcourir le tableau en comparant avec l'entrée reçue et si on trouve utiliser l'indice du tableau trouvé pour le switch/case
voilà un exemple que j'avais posté récemment dans le forum anglais
réglez la console à 115200 bauds et envoyez CRLF en fin de ligne. tapez "Hallo" ou "Salut" par exemple
const char * stringsOfInterst[] = {
"Hello", "Salut", "Hallo", "Ciao", "Ahoj", "Bog", "Marhaba", "Hej", "Ni Hao", "Shalom", "Namaste"
};
// to simplify the swicth case
enum {
_Hello_, _Salut_, _Hallo_, _Ciao_, _Ahoj_, _Bog_, _Marhaba_, _Hej_, _NiHao_, _Shalom_, _Namaste_
};
// or to handle things through index
const char * language[] = {
"English", "French", "German", "Italian", "Czech", "Croatian", "Turkish", "Swedish", "Chinese", "Hebrew", "Hindi"
};
const byte nbWords = sizeof(stringsOfInterst) / sizeof(stringsOfInterst[0]);
// ************************************************************************
// ************ small function to read the Serial input *******************
// ************************************************************************
// when entry is complete (found the endMark), function returns true
// and the c-string message has the text we entered
// ************************************************************************
const byte maxInput = 20;
char message[maxInput + 1]; // +1 for a trailing '\0'
boolean getInputTillEndMarker(const char endMark)
{
boolean endOfMessage = false;
static byte index = 0;
if (Serial.available()) {
int r = Serial.read();
if (r != -1) {
if (r == endMark) {
endOfMessage = true;
index = 0; // get ready for next one
}
else {
if (r != '\r') { // Ignore CR
message[index++] = (char) r;
message[index] = '\0'; // denote the end of the c-string
if (index >= maxInput) index = maxInput - 1; // don't overflow if input too long for bufer
}
}
}
}
return endOfMessage;
}
// ************************************************************************
void setup() {
Serial.begin(115200);
Serial.println("*** Hello tester ***");
for (byte i = 0; i < nbWords; i++) {
Serial.print(stringsOfInterst[i]); Serial.print(" ");
}
Serial.print("\n\nEnter Hello in one language: ");
}
// ************************************************************************
void loop() {
// read an input from the Serial line
if (getInputTillEndMarker('\n')) {
int languageIndex;
boolean found = false;
Serial.print("\""); Serial.print(message); Serial.println("\"");
// try to find which word we entered
for (languageIndex = 0; languageIndex < nbWords; languageIndex++) {
if (!strcmp(message, stringsOfInterst[languageIndex])) {
found = true;
break;
}
}
if (found) {
Serial.print("This is "); Serial.println(language[languageIndex]);
// you can also use languageIndex for more complicated stuff now per language in a switch / case and the enum makes it readable
switch (languageIndex) {
case _Hello_ :
Serial.println("I live in London");
break;
case _Salut_ :
Serial.println("je vis en France");
break;
case _Hallo_ :
Serial.println("Sauerkraut!");
break;
case _Ciao_ :
Serial.println("I love pizza");
break;
case _Ahoj_ :
Serial.println("You get the idea");
break;
case _Bog_ :
break;
case _Marhaba_ :
break;
case _Hej_ :
break;
case _NiHao_ :
break;
case _Shalom_ :
break;
case _Namaste_ :
break;
}
} else {
Serial.print("Sorry \""); Serial.print(message); Serial.println("\" is not in my dictionnary.");
}
Serial.print("\nEnter 'Hello' in one language: ");
}
}