I have this idea of writing a string of word in Serial, and after entering the word, it'll do some function inside, in this case, i named the function 'scanInput'.
The problem is that since i can't put readString inside the switch case, i have to put it inside the void loop and declare a variable to it. But i don't know how to declare it, since declaring it as either char, int, or long is wrong.
Is there any other method for this, or this is just one step?
Any help will be appreciated.
void loop() {
Input = Serial.readString();
switch (Input) {
case "Frequency" : {
unsigned int freq = scanInput(26, "Freq =");
Serial.println(freq);
}
break;
case "Amplitude" : {
unsigned int amp = scanInput(35, "Amp =");
Serial.println(amp);
}
break;
}
}
I think a CASE statement only works with a number or a single character.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
Sorry, my bad.
i forgot there exists a string data type
Don't confuse Strings (uppercase S), which are objects used with the functions of the String library and strings (lowercase s) which are zero terminated arrays of chars used with the C str* functions
In general, the latter are to be preferred in the small memory environment of the Arduino