Serial.read more than letter

i try to make led high when enter 'on' in serial monitor and low when write 'off' i need to know why this code not operate

int ledPin=13;
  char a[2] = {'o', 'n'};
  char b[3] = {'o', 'f', 'f'};
  char c[3] ;


void setup () 
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);

}
void loop ()
{
c[3] = Serial.read();
{
if ((a[0]==c[0])&&(a[1]==c[1])) 
{digitalWrite(ledPin,HIGH);
}
if (b[0] == c[0]&&b[1] == c[1]&&b[2] == c[2]) 
{digitalWrite(ledPin,LOW);
}
}
}
c[3] = Serial.read();

Don't write to the fourth element of a three element array

AWOL:
Don't write to the fourth element of a three element array

sorry i can't understand you :~

You've declared a three element array.
You're writing to the fourth element.
Don't do it.

C array indexes start at zero. Your c[3] array has three elements numbered [0], [1], and [2].

-br

Moderator edit:Smileys disabled

i don't know how i take value from serial and put it in array can you adjustment my code and post it thanks

Read it one character at a time, or check readBytes in the Serial reference.

AWOL:
Read it one character at a time, or check readBytes in the Serial reference.

please can you post any code to help me because i try to understand readbytes but i can't
thanks alot

please can you post any code to help me because i try to understand readbytes but i can't

Sorry, try the Arduino homework helpline.

thanks for your help
:smiley:

MostafaHamdy:
i try to make led high when enter 'on' in serial monitor and low when write 'off' i need to know why this code not operate

I suggest you buffer the incoming message in a null-terminated c-string, and use the standard string functions to manipulate it.

// incomplete, untested

void handleSerial()
{
    const int BUFF_SIZE = 32; // make it big enough to hold your longest command
    static char buffer[BUFF_SIZE+1]; // +1 allows space for the null terminator
    static int length = 0; // number of characters currently in the buffer

    if(Serial.available())
    {
        char c = Serial.read();
        if((c == '\r') || (c == '\n'))
        {
            // end-of-line received
            if(length > 0)
            {
                handleReceivedMessage(buffer);
            }
            length = 0;
        }
        else
        {
            if(length < BUFF_SIZE)
            {
                buffer[length++] = c; // append the received character to the array
                buffer[length] = 0; // append the null terminator
            }
            else
            {
                // buffer full - discard the received character
            }
        }
    }
}

void handleReceivedMessage(char *msg)
{
	if(strcmp(msg, "on") == 0)
	{
		// handle the command "on"
	}
	else
	{
		// etc
	}
}