Serial.read question ? solved; but why? ? (n00b)

Hi

I set up a little 2x2 matrix of LEDs that I control by “Row Column Scanning”.
Works great so far (in fact I’m very proud since I’m completely new to electronics and C programming)

As a next step I want to control the LED by sending a serial 4 byte string (“1111” to turn them on, “0001” turn off all but the last one etc)

First I read the string with this code:

void getData() {
  if (Serial.available() > 0) {
    while (pointer < 4) {
      buffer[pointer] = Serial.read();
      pointer++;
    }
    pointer = 0;
    pxlWrite(buffer[0], buffer[1], buffer[2], buffer[3]);
    Serial.print(buffer[0]);
    Serial.print(buffer[1]);
    Serial.print(buffer[2]);
    Serial.println(buffer[3]);
  }
}

wich returned strange results:

1ÿÿÿ
111ÿ

Then I accidentally added a delay (line 5):

void getData() {
  if (Serial.available() > 0) {
    while (pointer < 4) {
      buffer[pointer] = Serial.read();
      delay(1); //why is this necessary?
      pointer++;
    }
    pointer = 0;
    pxlWrite(buffer[0], buffer[1], buffer[2], buffer[3]);
    Serial.print(buffer[0]);
    Serial.print(buffer[1]);
    Serial.print(buffer[2]);
    Serial.println(buffer[3]);
  }
}

what made it working.
But why?

Thanks mana

This is your problem:

if (Serial.available() > 0) {
    while (pointer < 4) {
      buffer[pointer] = Serial.read();
      pointer++;
    }

What if “Serial.available” returns “1”?
You read that single character from the buffer, and the next time you call “Serial.read” the buffer is empty, so you get “-1”. (the odd ÿ characters)

Now, at 9600 bps, 1 character takes just over 1ms to transmit.
Your delay allows you time to receive the next character before calling “Serial.read”.

I wouldn’t rely on this (it won’t work if you decide to reduce the line speed), so you need to fix the logic of reading available characters.
So, no, not solved, sorry. :wink:

Without the delay, its becoming corrupted probably because of a timing issue. Your code doesn’t include what speed serial you’re using. It would probably work without the delay if you slowed it down.

Hi

Thanks for your responses.

Currently I'm using 9600 Baud.

I don't know what Serial.available() returns. If I do a Serial.println(Serial.available()) only a blank line is shown (should'nt it be 4 when I enter 4 chars, e.g. "1010"?)

so you need to fix the logic of reading available characters

I see, but how?

I don’t know what Serial.available() returns.

http://arduino.cc/en/Serial/Available

If Serial.available returns a “1”, there’s no point reading out four characters.

for (pointer = 0; pointer < 4; ++pointer) {
  while (Serial.available () == 0) 
     {;}
  buffer [pointer] = Serial.read ();
}

:sunglasses:
Now it works!
Thanks a lot

I don't know what Serial.available() returns.

It returns the number of bytes available to read.

It returns the number of bytes available to read.

That's what the ref says. I can read, too.
But as noted above running

Serial.println(Serial.available())

returns nothing in my sketch.

Try this:

  Serial.println(Serial.available(), DEC) ;