Pages: [1]   Go Down
Author Topic: Serial.read more than letter  (Read 504 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
}
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
c[3] = Serial.read();
Don't write to the fourth element of a three element array
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't write to the fourth element of a three element array
sorry i can't understand you  smiley-confuse
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-br


Moderator edit:Smileys disabled
« Last Edit: May 06, 2013, 12:20:00 pm by AWOL » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your help
smiley-grin
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12524
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
}
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: