This is simple but I don't know how to. I just want to send some text to process, for example, i wanna send the text "on" to switch the led and "off" to turn it off but when i do this with this code
if( myTxt == "on")
Evaluates the myTxt for string "on" and will be true if the string is ONLY "on".
Using Monitor enter "on" together then do Send, the Serial.read() will read one character at a time and your code will add them together doing myTxt +=c .
You may want to experiment with substring so you can "pick out" your "on" string from other characters.
Such as "123on456".
I'm not a big fan of the String class, as it adds a lot of bloat to most programs. Here's an alternative; my guess is that it's smaller than the alternative:
#define MAXBUFF 20
const int LEDPIN = 13;
char message[MAXBUFF];
void setup() {
Serial.begin(9600);
pinMode(LEDPIN, OUTPUT);
}
void loop() {
int charsRead;
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', message, MAXBUFF - 1); // Gather the input
message[charsRead] = '\0'; // Make it a C string
Serial.println(message);
}
if (strcmp(message, "on") == 0) // Did it say on?
{
digitalWrite(LEDPIN, HIGH);
} else {
if (strcmp(message, "off") == 0) // or off
digitalWrite(LEDPIN, LOW);
}
}
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}