Duemilanove and Processing interface problem- Serial.available() returns false

Hello all!

This is what I am looking at. The sketches I'm about to show are for my robot I'm building. So far I've built it in a superposition style- treads, arm, and sensors, each with its own corresponding arduino and processing sketches. The latest one I've made is having combines my code for the arm and treads, and I'm running into a problem that I've never seen before, and I'm at a loss as to what is going wrong.

Before I post my codes, here is what we are working with. My OS is OS X 10.6.3, I'm using Processing 1.2.1, Arduino 0022, and the Duemilanove with the Atmega328. The problem is the Processing code is the being sent properly, but it is not being received on the end of the arduino. I know this because I have a bit of code that causes an indicator LED that lights up when Serial.available() returns true, and the LED does not light up.

What confuses me is that I can still upload the older sketches, the ones that run just the treads or just the arm, and they work perfectly. This makes me think that it might not be a driver problem of some sort.

For some reason this one is simply not cooperating with me. Any ideas?

Here is my Arduino sketch:

/**************************************
***************************************
ArmTreadGUIArduino
By Matt Aud
Overview:  This program will allow the user to contol the robot's treads and
  arm via the keypad. Program will also incorporate the GUI
Works in Conjunction With: ArmTreadGUIProcessing
***************************************
***************************************/



/**************************************
***************************************
Set up the Servos
***************************************
***************************************/
//include the servos library
#include <Servo.h>

//now the servos themselves
Servo LEFTTREAD;
Servo RIGHTREAD;
Servo SHOULDER;
Servo ELBOW;
Servo GRIPPER;

/**************************************
***************************************
Set up the incoming byte array
***************************************
***************************************/
byte incoming[6];

//our servo initial positions
int Lspeed = 90; //left tread stop
int Rspeed = 90; //right tread stop
int shoulder = 90; //shoulder center
int elbow = 90; //elbow center
int gripper = 90; //gripper center

/**************************************
***************************************
Misc set up
***************************************
***************************************/

//our increment for the arm servos
//adjust this to adjust the speed
int increment = 3;

//set up out key
byte key = 4;


/*This byte "KEY" is one of the key obstacle to overcome. When a 2 byte array
was send over the serial cable, the data finds the proper place in the arduino code
without fail. Howver, once that byte array is increased in size to 3 or more bytes, the
incoming array in the arduino code will consistanty place the wrong data bytes into the 
improper slots. This key, however, is the way to get around this obstacle. The arduino code
loads the array and does not begin placing the data into the proper cells until the "KEY"
is loaded. This allows the data to be placed in the proper place without error.
*/

//define our tester LED
#define LED 13
//This light wil light up when the incoming data is being received


/**************************************
***************************************
The Setup
***************************************
***************************************/
void setup()
{

  /**************************************
  ATTACH THE SERVOS
  ***************************************/
  /*
  Layout:
  Servo ------- Pin Number
  Left tread ------ 9
  Right Tread ----- 10
  Shoulder -------- 5
  Elbow ----------- 6
  Gripper --------- 3
  */
  LEFTTREAD.attach(9);
  LEFTTREAD.write(Lspeed);
  RIGHTREAD.attach(10);
  RIGHTREAD.write(Rspeed);
  SHOULDER.attach(5);
  SHOULDER.write(shoulder);
  ELBOW.attach(6);
  ELBOW.write(elbow);
  GRIPPER.attach(3);
  GRIPPER.write(gripper);
  
  //our tester LED is an output
  pinMode(LED, OUTPUT);
    //turn on the serial port
  Serial.begin(9600);

}

void loop()
{
  //BRACE YOURSELVES!
 
  /**************************************
  LISTEN FOR OUT INCOMING ARRAY
  ***************************************/
  if(Serial.available() == true)
  {
    digitalWrite(LED, HIGH);
  }

while(Serial.available() == 6)
{
    //once 5 bits are had, turn our out signal LED
  //  digitalWrite(LED, HIGH);
    
    //look for our key before loading our array
    if(Serial.read() == key)
    {
      //key has been found, load 'er up!
      for(int n = 1; n < 6; n++)
      {
        incoming[n] = Serial.read();
      }
    }
  }

  
  //The array has bee loaded!
  //Now we will interperet the data!
  
  /**************************************
  ***************************************
  INCOMING ARRAY
  Array Order:
  0 - key
  1 - left tread
  2 - right tread
  3 - shoulder servo
  4 - elbow servo
  5 - gripper servo
  ***************************************
  ***************************************/
  
  
  /**************************************
  ***************************************
  BEGIN INPUT PROCESSING FOR THE TREADS
  ***************************************
  ***************************************/
 if(incoming[1] == 0)
 {
   Lspeed = 178;
   LEFTTREAD.write(Lspeed);
 }
 if(incoming[1] ==1)
 {
   Lspeed = 2;
   LEFTTREAD.write(Lspeed);
 }
 if(incoming[1] == 2)
 {
   Lspeed = 90;
   LEFTTREAD.write(Lspeed);
 }
 
 
 if(incoming[2] == 0)
 {
   Rspeed = 2;
   RIGHTREAD.write(Rspeed);
 }
 if(incoming[2] ==1)
 {
   Rspeed = 178;
   RIGHTREAD.write(Rspeed);
 }
 if(incoming[2] == 2)
 {
   Rspeed = 90;
   RIGHTREAD.write(Rspeed);
 }
 
 /**************************************
  ***************************************
  BEGIN INPUT PROCESSING FOR THE ARM
  ***************************************
  ***************************************/
  //shoulder servo
  if( incoming[3] == 0 && shoulder > 0)
  {
    shoulder -= increment;
    SHOULDER.write(shoulder);
    delay(10);
  }
  if( incoming[3] == 1 && shoulder < 180)
  {
    shoulder += increment;
    SHOULDER.write(shoulder);
    delay(10);
  }
 
  //elbow servo
  if(incoming[4] == 0 && elbow > 0)
  {
    elbow -= increment;
    ELBOW.write(elbow);
    delay(10);
  }
  if(incoming[4] == 1 && elbow < 180)
  {
    elbow += increment;
    ELBOW.write(elbow);
    delay(10);
  }

  
  //gripper servo
  if(incoming[5] == 0 && gripper > 0)
  {
    gripper -= increment;
    GRIPPER.write(gripper);
    delay(10);
  }
  if(incoming[5] == 1 &&gripper < 180)
  {
    gripper += increment;
    GRIPPER.write(gripper);
    delay(10);
  }
  
}

Sorry for the double post, but I could not get this to fit without hitting the character limit.

Here is my Processing sketch with the GUI stripped out.

/**************************************
***************************************
ArmTreadGUIProcessing
By Matt Aud
Overview:  This program will allow the user to contol the robot's treads and
  arm via the keypad.
Works in Conjunction With: ArmTreadGUIArduino
***************************************
***************************************/

//set us up a serial cable
import processing.serial.*;
Serial port;

//set up the font
PFont font;

/**************************************
***************************************
Set up the Byte Array
***************************************
***************************************/

//create our byte array and its components
byte[] send = new byte [6]; //6 blocks- one key, two treads, three arm variables
byte rTread; //right tread
byte lTread; //left tread
byte shoulder; //shoulder
byte elbow; //elbow
byte gripper; //gripper

byte KEY = 4; 

/**************************************
***************************************
Set up our info bytes
***************************************
***************************************/

//the bytes for left tread forward and back
byte leftF = 0;
byte leftB = 1;

//bytes for right tread forward and back
byte rightF = 0;
byte rightB = 1;

//bytes for the shoulder forward and back
byte shoulderF = 0;
byte shoulderB = 1;

//bytes for the elbow up and down
byte elbowU = 0;
byte elbowD = 1;

//bytes for the gripper open and closed
byte gripperC = 0;
byte gripperO = 1;

/**************************************************************
***************************************************************
Setup Loop
***************************************************************
**************************************************************/

void setup()
{
  //Set up our GUI
  size(800, 700);
  font = loadFont("Courier-24.vlw");
  textFont(font, 20);
  
  //serial port
  String arduinoPort = Serial.list()[0];
  port = new Serial(this, arduinoPort, 9600);
  
}

void draw() 
{
  //set up the background
  background(0, 125, 250);
  fill(255);
  stroke(255);
  
  /**************************************
  ENTER THE LOOP!
  ***************************************/
  //This is the part that processes the input
  //then sends the proper corresponding outputs
  //to the sister arduino code.
  if(keyPressed == true)
  {
    if(key == CODED)
    {
      /**************************************
      TREAD CONTROL
      ***************************************/
      if(keyCode == UP)
      {
        lTread = leftF;
        rTread = rightF;
      }
      if(keyCode == DOWN)
      {
        lTread = leftB;
        rTread = rightB;
      }
      if(keyCode == LEFT)
      {
        lTread = leftB;
        rTread = rightF;
      }
      if(keyCode == RIGHT)
      {
        lTread = leftF;
        rTread = rightB;
      }
    }
    
    /**************************************
    OTHER SERVOS
    ***************************************/
   if(keyPressed) 
   {
     /*How this is going to work: 
     D - shoulder forward
     X - shoulder back
     F - Elbow up
     C - elbow back
     R - gripper closed
     E - Gripper open
     */
   
    /**************************************
    Shoulder Servos
    ***************************************/  
    //Move shoulder forward
    if(key == 'd' || key == 'D')
    {
      shoulder = shoulderF;
    }
    //Move shoulder back
   if(key == 'x' || key == 'X')
    {
      shoulder = shoulderB;
    }
   
    /**************************************
    ELBOW SERVOS
    ***************************************/
    //move elbow up
    if(key == 'f' || key == 'F')
    {
      elbow = elbowU;
    }
    //move elbow down
    if(key == 'c' || key == 'C')
    {
      elbow = elbowD;
    }
    
    /**************************************
    GRIPPER SERVOS
    ***************************************/
    //close gripper
    if(key == 'r' || key == 'R')
    {
      gripper = gripperO;
    }
    //open gripper
    if(key =='e' || key == 'E')
    {
      gripper = gripperC;
    }  
   }
  }
 else
  {
    //if nothing else is pushed, make the send variables "2"
    lTread = 2;
    rTread = 2;
    //text for the visual output
    shoulder = 2;
    elbow = 2;
    gripper = 2;
    
  }
  
  /**************************************
  OUTPUT TEXT
  ***************************************/  
  text("TREADS", 10, 40);
  text("ARM", 190, 40);
  
  text("LEFT:", 20, 60);
  text(lTread, 90, 60);
  
  text("RIGHT:", 20, 80);
  text(rTread, 90, 80);
  
  text("SHOULDER:", 160, 60);
  text(shoulder, 270, 60);   

  text("ELBOW", 180, 80);  
  text(elbow, 270, 80);
  
  text("GRIPPER:", 160, 100);
  text(gripper, 270, 100);
 
  /**************************************
  Build our send array
  ***************************************/
  send[0] = 4; 
  send[1] = lTread;
  send[2] = rTread;
  send[3] = shoulder;
  send[4] = elbow;
  send[5] = gripper;
    
  /**************************************
  Send the Byte array
  **************************************/
  
  port.write(send);
  /**************************************
  And we're done.
  **************************************/
 
}
  if(Serial.available() == true)
  {
    digitalWrite(LED, HIGH);
  }

The Serial.available() function returns the number of bytes available to be read, not true/false - there is/is not data available to read.

while(Serial.available() == 6)
{

What happens if there are 5 characters in the buffer, and 2 more arrive before you get around to checking again? If that happens, you'll never be able to read serial data again. This statement should have >= as the operator, not ==.

PaulS:

  if(Serial.available() == true)

{
    digitalWrite(LED, HIGH);
  }



The Serial.available() function returns the number of bytes available to be read, not true/false - there is/is not data available to read.

Ah, wow. Yup, that is very true. Thanks.

And you were right with the buffer, too! I'm all set.

I've been stalking this forum for a while and this isn't the first time you've solved one of my problems, PaulS. Thanks, buddy! I appreciate it!