Go Down

Topic: Serial.read more than letter (Read 655 times) previous topic - next topic

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
Code: [Select]
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);
}
}
}

AWOL

Code: [Select]
c[3] = Serial.read();
Don't write to the fourth element of a three element array
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

MostafaHamdy


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

sorry i can't understand you  :~

AWOL

You've declared a three element array.
You're writing to the fourth element.
Don't do it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

billroy

#4
May 06, 2013, 07:19 pm Last Edit: May 06, 2013, 07:20 pm by AWOL Reason: 1
C array indexes start at zero.  Your c[3] array has three elements numbered [0], [1], and [2].

-br


Moderator edit:Smileys disabled

MostafaHamdy

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

AWOL

Read it one character at a time, or check readBytes in the Serial reference.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

MostafaHamdy


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

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


PeterH


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.

Code: [Select]

// 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
}
}
I only provide help via the forum - please do not contact me for private consultancy.

Go Up