Check the "strcmp" which came to buffer

Hello!

Arduino is connected to the computer. The keyboard enters the word "on" to light the diode, and the word "off" to turn off the LED. I used the "readBytesUntil," and to check what came to buffer the "strcmp". I wrote code

  int led = 13; 
  
  void setup()
  {
     pinMode(led, OUTPUT); 
     Serial.begin(9600);
     Serial.flush();
  }
 
  void loop()
  {
     char buf[4];
     byte nb;
     nb=Serial.readBytesUntil('\n',buf,4);

     String input = "";
 
     while (Serial.available() > 0)
     {
         input += (char) Serial.read(); 
         delay(5); 
     }
 
     if (strcmp(nb, "on") == 0) 

     {
         digitalWrite(led, HIGH); 
     }
     else if (strcmp(nb, "off") == 0)
     {
         digitalWrite(led, LOW); 
     }
 }

The code does not compile because:

sketch_nov24a.ino: In function ‘void loop()’:
sketch_nov24a.ino:25:25: error: invalid conversion from ‘byte {aka unsigned char}’ to ‘const char*’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:5:0,
                 from sketch_nov24a.ino:2:
/usr/lib/avr/include/string.h:125:12: error:   initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
 extern int strcmp(const char *, const char *) __ATTR_PURE__;
            ^
sketch_nov24a.ino:30:31: error: invalid conversion from ‘byte {aka unsigned char}’ to ‘const char*’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:5:0,
                 from sketch_nov24a.ino:2:
/usr/lib/avr/include/string.h:125:12: error:   initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
 extern int strcmp(const char *, const char *) __ATTR_PURE__;
            ^

How to improve the above code?

extern int strcmp(const char *, const char *) __ATTR_PURE__;

The error message is telling you that argument 1 to strcmp must be a pointer to a character string. You have supplied a character '\n'. Use "\n" instead.

Pete

char buf[4];
     byte nb;
     nb=Serial.readBytesUntil('\n',buf,4);
if (strcmp(nb, "on") == 0)

nb is the single byte where you are storing the number of characters received by readBytesUntil. buf is where you put what you read. So in your strcmp, you shouldn't be using the single byte nb but the buffer buf that holds the data you read from serial.

Get rid of the String variables...they waste too much memory and usually are not worth the "convenience". For example, try:

  int led = 13; 
  
  void setup()
  {
     pinMode(led, OUTPUT); 
     Serial.begin(9600);
     Serial.flush();
  }
 
  void loop()
  {
     char buf[4];
     int charsRead;
     
 
     while (Serial.available() > 0)
     {
       charsRead = Serial.readBytesUntil('\n', buf, 4);
       buf[charsRead] = '\0';                                       // Make it a string
     }
 
     if (strcmp(buf, "on") == 0) 
     {
         digitalWrite(led, HIGH); 
     }
     else if (strcmp(buf, "off") == 0)
     {
         digitalWrite(led, LOW); 
     }
 }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

If you really just want to control ON and OFF just send the single character ‘N’ or ‘F’ - it will make the Arduino code very much simpler.

…R

     Serial.begin(9600);
     Serial.flush();

Start the serial connection, and then immediately wait until all pending outgoing serial data has been sent. Here's a clue. There is NONE. The flush() is useless. Get rid of it.

Thank you for all your help.