Serial.available() questions

Hi All, I am having trouble with serial again!? I'll try to explain the best I can... My code looks somewhat like this:

int bytesToRead = 0;

bytesToRead = Serial.available();
Serial.print("bytesToRead: ");
Serial.println(bytesToRead);

If I input say "r1500" on the serial port bytesToRead outputs 1 through the first case of the loop and 4 the second time it passes through. Any thoughts? I've tried two different boards and I am getting the same from each.

Many thanks,
Symir

Since you haven't put the serial rate, let's assume you're running at 9600 bps.
One character takes just over a millisecond to transmit.
The first character 'r' arrives at the Arduino, so "available" returns "1" when the character is in the rx buffer.
You then spend over 15ms printing the message whilst the remaining characters "1500" are received.

Does that explain what you're seeing?
What did you expect to see?

(PS, best to show your whole sketch)

Groove, I suppode that is kinda what I am seeing. I din't post the whole sketch because it is quite large. Do you suggest I use a delay in there somewhere?

Thanks,
Symir

Why would you want a delay?
Just keep reading characters until you get a delimiter (comma, carriage return/line feed, whatever) or until you've read as many characters as you're expecting (in this case, apparently 5).

I haven't been using a delimeter character at this point. You are right though, that would work quite well. Are there any tutorials that you may know of that would walk someone through implementing this?

Thanks again!

Here's all the code I have so far:

#include <Servo.h>

Servo actuator;  //the actuator
int actPin = 10;  //the pin that the servo is attached to
int actPosition = 0;  //variable to store anlaog output of actuator
int actPositionPin = 0;  //pin to connect analog output of actuator
int contactPin = 13; //pin to check water contact
int contact = 0;  //contact input value
char ch = 't';  //character input for serial
int actMin = 1250;  //minimum pulsewidth for actuator
int actMax = 1750;  //maximum pulsewidth for actuator
int bytesToRead = 0;  //number of bytes in serial buffer
int nnnn = 0;  // used to build a number whn using r

void setup()
{
  actuator.attach(actPin);  //attach actuator
  Serial.begin(9600);  //serial baud rate
  Serial.flush();
  pinMode(contactPin, INPUT);  //set contact pin as input
}

void loop()
{
  delay(200);
  bytesToRead = Serial.available();

  if(bytesToRead > 0)  //if any bytes are available, run
  {
    ch = Serial.read();

    if(bytesToRead > 1)
    {
      nnnn = 0;
      for(int i = 0; i < bytesToRead - 2; i++)  //get each byte and build number, nnnn
      {
        char ch1 = Serial.read();
        delay(50);
        int mult = 1;
        for(int j = 1; j < bytesToRead - i; j++)
        {
          mult = mult * 10;
        }
        nnnn = nnnn + (ch1 - 48) * mult;
      }
    Serial.print("nnnn: ");
    Serial.println(nnnn);
    }

    switch(ch){
    case 'p':
      {
        actPosition = analogRead(actPositionPin);
        Serial.println(actPosition);
        break;
      }
    case 'z':
      {
        actuator.attach(10, actMin, actMax);  // attach servo to pin 10
        actuator.write(actMin);
        delay(5000);
        actuator.detach();
        actPosition = analogRead(actPositionPin);
        Serial.println(actPosition);
        break;
      }
    case 'f':    
      {
        actuator.attach(10, actMin, actMax);  // attach servo to pin 10
        contact = digitalRead(contactPin);
        if(contact == 0)
          actuator.write(actMax);  //send actuator down/out

        while (contact == 0)  //while pins are out of water
        {
          contact = digitalRead(contactPin);  //check pin
          if (contact == 1)  //if pin has found water, record position and backup
          {
            actPosition = analogRead(actPositionPin);
            actuator.write(actMin);
            delay(200);
          }
        }
        Serial.println(actPosition);
        actuator.detach();
        break;
      }
    case 'r':
      {
        if(nnnn >= actMin && nnnn <= actMax)  //write the pulse to the servo
        {
          actuator.attach(10, actMin, actMax);  // attach servo to pin 10
          actuator.write(nnnn);
          delay(5000);
        }
        //Serial.println(actPosition);
        actuator.detach();

        break;
      }
    default:
      {
        Serial.println("Invalid input");  //if the input is not acceptable
        break;
      }

    }
    delay(50);
  }
}

Without knowing how you "input" that string (Are you typing at a keyboard? Sending an email that the PC transfers to the Arduino? Reading from a serial-interfaced sensor?), it's impossible to do much more than guess.

Thanks to The Miracle of Open Source®, you can look at the code, and see there's nothing tricky about it.

The most likely cause is that your code spins until the first character arrives, immediately executes the print() statements, and the other four arrive in the receive buffer while you're printing.

I am inputting via keyboard from a small C# program I have written or via the IDE.