Hi,I am looking for a way to make a program that,if it recieves let's say "LEDON" on serial to send back "LEDOFF" on serial,I was told that I need to use strcmp,Can anyone give me example code for this?
So,when it recieves "something" to send back "something",I'm not sure if it matteres,but I'll be using multiple strcmp.Sorry for my bad english,I hope you will understand me,english is my second language
Try here
Have you looked at the "serial input basics" thread? It has a lot of great information on this sort of thing.
http://forum.arduino.cc/index.php?topic=396450.0
Thanks,I already asked that question,but I couldn't find that post.
I have already seen that post but I don't understand how to do it
Nikola19992:
I have already seen that post but I don't understand how to do it
Just keep working at it.
This is what I got so far
void setup() {
Serial1.begin(9600);
}
void loop() {
char data = Serial1.read();
if(strcmp(data,"ERROR") == 0){
Serial1.print("You recieved error")
}
}
But it doesn't work
But it doesn't work
No,it doesn't compile.
Different thing entirely
See reply #4
Don't read that post - the code OP posted has numerous problems, and I don't see any corrected code in that thread.
Read the serial input basics thread (linked to in reply 4 of that thread, which I think is what AWOL is referring to) - it's an excellent guide, really covers what you need to know and shows examples that work for a variety of common tasks.
What do I need to change for it to work?
You could do a.lot worse than reading Robin2's excellent serial input basics thread
I have read it but I can't seem to find what I'm looking for
You're looking for the difference between a char and a zero-terminated array of char.
Okay,thanks I'll try to find a solution
If you read Robin's tutorial, you should learn that Serial.read() only returns one character, so how can it possibly equal "LEDON" (i.e., 5 characters)? Also, Serial.read() sends that character when it's ready, not necessarily when you're ready. Finally, type in this program a learn how it works.
void loop()
{
char message[20];
int charsRead;
if (Serial.available() > 0) { // Is the Serial object sending something?
charsRead = Serial.readBytesUntil('\n', message, sizeof(message) - 1); // Yep, so read it...
message[charsRead] = '\0'; // Now make it a C string...
Serial.print("You entered: "); // ...and show it
Serial.println(message);
}
}
When you look at the Serial monitor, make sure the bottom listbox shows "Newline" in it and that the baud rate for the monitor matches the baud rate in your program. Once you figure out what is going on here, look at this.
Thanks,I was looking for someone to explain this to me with a example and you did it but I have a little problem,This code doesn't do what it should,it should replay to LEDON with LEDOFF and it doesn't do anything
void setup() {
Serial1.begin(9600);
}
void loop()
{
char message[20];
int charsRead;
char LEDON[10];
if (Serial1.available() > 0) { // Is the Serial object sending something?
charsRead = Serial1.readBytesUntil('\n', message, sizeof(message) - 1); // Yep, so read it...
message[charsRead] = '\0'; // Now make it a C string...
if (message == LEDON){
Serial1.print("LEDOFF");
}
}
}
if (message == LEDON){Remind me - what is the subject of this thread?
I finally did it right after like 5-6 hours
![]()
Thank you guys for fast replays and help
void setup() {
Serial1.begin(9600);
}
void loop()
{
char message[20];
int charsRead;
if (Serial1.available() > 0) {
charsRead = Serial1.readBytesUntil('\n', message, sizeof(message) - 1);
message[charsRead] = '\0';
if (strcmp(message, "LEDON") == 0)
{
Serial1.print("LEDOFF");
}
}
}