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?