Reading a two bytes command over the serial bus problem

I’m trying to create my Hello word app in Arduino, basically my Arduino will respond OK over the serial bus when it receives a two bytes string containing it names (already stored in a variable). My idea is to store each byte arrived and when the dimension is two (two bytes) compare the result to the name, but I think I’m doing more code than the necessary, and also I’m getting an error when compiling, this is what a get so far:

const char MyName[] = "AA"; // Name of micro const int LedPin = 13; // Status Led const int ClockPin = 12; // Clock output const int ClockPeriod = 200; // Clock period in ms char command[1]; int cindex = 0;

void setup() { Serial.begin(9600); pinMode(LedPin, OUTPUT); pinMode(ClockPin, OUTPUT); }

void loop() { cindex = 0; if(Serial.available()) { command[cindex] = Serial.read(); cindex++; if(cindex == 2) { if((strcmp(command[0],MyName[0]) == 0) && (strcmp(command[1],MyName[1]) == 0)) { Serial.print("OK"); } } } }

And this is the error: master:19: error: initializing argument 1 of 'int strcmp(const char*, const char*)' master:19: error: invalid conversion from 'const byte' to 'const char*' master:19: error: initializing argument 2 of 'int strcmp(const char*, const char*)'

Take in consideration that this is my firts code in Arduino, so it maybe need a lot of optimization, so please let me know all my errors! Thanks for any help!

Several things wrong here:- 1) When posting code please select the code and hit the # icon, this will put it in a box. 2) When you gather bytes from the serial port you are not putting the null string terminator on the end. 3) The code line cindex = 0; will put every byte in the same place unless the bytes are already waiting in the buffer (unlikely) 4) The comparison should be:-

if((strcmp(command,MyName) == 0) && (strcmp(command,MyName) == 0)) {

The comparison should be:-

No, sorry, not this time, Mike.

OP: the strcmp() function takes two arguments, both strings. The first argument, MyName, is a string. The second argument, command, is NOT a string.

The difference is that MyName is a NULL terminated array of chars, and command is an array of chars. That NULL terminator is very important, and the array needs to be sized to have room for it.

The declaration of command creates an array that can hold one character. That's not a very useful size array, is it? The value in the [] needs to be at least 3.

Then, the match test should be simply

if((strcmp(command, MyName) == 0)

Mike's other comments are correct.

No, sorry, not this time, Mike.

Well two out of three. :)

I never did like strings in C.

Thanks.