Does my Arduino ignore its serial buffer?

I am having a problem with my code. There, I said it. I am trying to do an Arduino controlled Xbee Robot… The problem I am having relates to the Arduino and its handling of information sent to it via SoftwareSerial. Here is my code:

#include <SoftwareSerial.h>
SoftwareSerial XBEE(2, 4);

#include <Robot.h>
Robot robot(5, 8, 6, 9);
void setup(){
  //Serial.begin(9600);
  XBEE.begin(9600);
}
void loop(){
  //XBEE.println("Checking");
  if(XBEE.available() > 0){
    XBEE.println("Available");
    if(char(XBEE.read()) == 'A'){
      XBEE.println("Detected A");
      int AVal = XBEE.parseInt();
      if(AVal < 0){
        robot.Reverse(1, abs(AVal));
      }
      else{
        robot.Forward(1, abs(AVal));
      }char B = XBEE.read();
      
      //if(char(XBEE.read()) == 'B'){
       XBEE.println("Detected b");
        int BVal = XBEE.parseInt();
        if(BVal < 0){
          robot.Reverse(2, abs(BVal));
        }
        else{
          robot.Forward(2, abs(BVal));
        }
      //}
    }
  }
}

I send ‘A’ + the value for that motor + ‘B’ + plus the value for the second motor, and then the arduino should apply that with the library. The code above is slightly modified for diagnostics. Here is the original:

#include <SoftwareSerial.h>
SoftwareSerial XBEE(2, 4);

#include <Robot.h>
Robot robot(3, 12, 11, 13);
void setup(){
  //Serial.begin(9600);
  XBEE.begin(9600);
}
void loop(){
  //Serial.println("Checking");
  if(XBEE.available() > 0){
    //Serial.println("Available");
    if(char(XBEE.read()) == 'A'){
      // Serial.println("Detected A");
      int AVal = XBEE.parseInt();
      if(AVal < 0){
        robot.Reverse(1, abs(AVal));
      }
      else{
        robot.Forward(1, abs(AVal));
      }
      if(char(XBEE.read()) == 'B'){
        //  Serial.println("Detected b");
        int BVal = XBEE.parseInt();
        if(BVal < 0){
          robot.Reverse(2, abs(BVal));
        }
        else{
          robot.Forward(2, abs(BVal));
        }
      }
    }
  }
}

I have tested it on another robot(thecentralthinkingunit.blogspot.com explains it all) and the program works. The only change is that I am using:
A: the arduino motor shield
B: Xbee Series 2 rather than S1
All the arduino does is say, when running the first program, "Available
Detected A
Detected b
" but not turn on the motors…
The Robot library(another one of my recent works) is attached.

All help is appreciated.
Thanks,
L

keywords.txt (65 Bytes)

Robot.cpp (1.43 KB)

Robot.h (476 Bytes)

You're making a classic mistake. You check that there is a byte to read in the serial buffer then try to read a whole bunch of them. Serial data comes slow, much much slower than your Arduino executes code. So you get a byte and you read it and it is an A and you go to parseInt but the number hasn't shown up yet.

You should either check to make sure all of your characters have arrived by checking Serial.available against a larger number or restructure the code to just check the serial buffer each time through the loop and hang on to what it's got until it gets a complete message and only act once it has processed the whole thing.

You can either spin around in a while loop reading the characters or, if you know what terminates the input stream (often a newline character, '\n'), you can use Serial.readBytesUntil().

Your code looks like crap. Use Tools + Auto Format to properly indent the code.

      }char B = XBEE.read();

Bullshit. NOTHING goes on the line after a }. EVER!

Commented out code is NOT part of the problem. DELETE IT!

Read and store the "command" character. DO NOT COMPARE WHAT YOU HAVE JUST READ TO 'A'. When you do this, and it isn't a match, you read again, and see if that is a 'B'. What if the character read the first time was a 'B'? That is not an 'A', so you should check to see if it is a 'B'. But, you can't, because you've already thrown it away.

You never use the value in B, so that statement is useless. More than that, it is wrong.