setting the last serial line to zero if no new information is received

Hi, I have been working on this code that reads a processing code that outputs x and y coordinates in bytes. It is based off of a few examples that I have found around the internet. Basically what I want to do for now is to read the x coordinate given by processing, read on the serial port and if it is greater than a certain value, lets say zero, then it will turn on a LED, and if it is anything but that set value condition then I want to change the led color. (I am using an RGB LED). My problem is if the last serial information I got is above my set value the xPosition variable is going to stay that value and wont drop back down to anything other than greater than zero. So my question is can anyone help me redefine the “xPosition” variable and set it to zero after the program sees that it is not getting any new serial information. It seems like it shouldnt be that hard, but everything that I have tried has not worked.
Thanks

void loop() {   
  if (Serial.available() >= 10) {      
    indicator = Serial.read();        
    if(indicator == 'a') {    
      delay(100);    
      idle = false; 
      idleCounter = 0;
      x100byte = Serial.read();         
      x010byte = Serial.read();         
      x001byte = Serial.read();         
      y100byte = Serial.read();         
      y010byte = Serial.read();         
      y001byte = Serial.read();         

    }
  }
  if(idle) {                         
    Serial.write('T');                
  }    
  else{

    xPosition1 = (100*(int(x100byte)-48)) + (10*(int(x010byte)-48)) + (int(x001byte)-48);   
    yPosition1 = (100*(int(y100byte)-48)) + (10*(int(y010byte)-48)) + (int(y001byte)-48);  
  }
  }
  while(xPosition1 > 0){
    analogWrite(2,255);
    analogWrite(3,0);
    analogWrite(4,255);
   
  }
  while(xPosition1 < 0){ 
    analogWrite(2,255);
    analogWrite(3,255);
    analogWrite(4,0);
  }
}

You wait till you have 10 or more bytes and then you read 7. Why?

Nick, thank you for pointing that out. That was from one of the examples that I copied and I forgot to change that. However now that I did change it I am still having the problem with the last read serial byte remaining as the variable. My LED now is green (from the last else statement), until I get a serial value, it then turns blue and stays blue until I reload the code on the board. I will post the modified code below.

I've been trying to go along the lines of making a delay and if the xPosition still equals the xPosition1 then set xPosition1 to zero. But Im not having much luck with that.

void loop() {   
  if (Serial.available() >= 7) {      
    indicator = Serial.read();        
    if(indicator == 'a') {    
      delay(100);    
      idle = false; 
      idleCounter = 0;
      x100byte = Serial.read();         
      x010byte = Serial.read();         
      x001byte = Serial.read();         
      y100byte = Serial.read();         
      y010byte = Serial.read();         
      y001byte = Serial.read();         

    }
  }
  if(idle) {                         
    Serial.write('T');                
  }    
  else{

    xPosition1 = (100*(int(x100byte)-48)) + (10*(int(x010byte)-48)) + (int(x001byte)-48);   
    yPosition1 = (100*(int(y100byte)-48)) + (10*(int(y010byte)-48)) + (int(y001byte)-48);  
  }
  if(xPosition1 > 0){
    analogWrite(2,255);
    analogWrite(3,0);
    analogWrite(4,255);
   
  }
  else
  {
    analogWrite(2,255);
    analogWrite(3,255);
    analogWrite(4,0);
  }
}
    analogWrite(2,255);
    analogWrite(3,255);
    analogWrite(4,0);

What Arduino do you have? On my Uno pins 2 and 4 are not PWM outputs.

And in any case it isn't clear why you are writing 0 or 255 (off or on) using analogWrite when you could be using digitalWrite.

I am using a Mega 2560. From what I understood from http://arduino.cc/en/Hacking/PinMapping2560 pins 2,3 and 4 are all PWM. But of course I could be mistaken. I am able to get the LED to light up and change color when I want it too, I just cannot figure out how to get it to change back based off of the serial information.

And I was using analogWrite because I thought that it would make it easier to mix and fade the colors of the RGB LED. If I changed from analogWrite to digitalWrite, what would be the advantage in this situation?

Thank you for helping me along

OK, well it helps to state that sort of thing, because different boards can behave differently.

Now can you please state what you are sending to the board (the serial data).

Sorry for not clearing that up originally.
I am sending a string from processing that contains an identifier and the x,y coordinates of an object on a webcam processed in processing, and example would be
coordinates of the object:
x = 223
y = 216

sending the information from processing:

     arduinoPort.write('a' + strTargetx + strTargety );
     println('a' + strTargetx + strTargety);

sent over serial:

"a223216"

so
identifier = a
x = 223
y = 216

Now let the arduino decode it and turn it back into three digit x,y coordinates

    xPosition1 = (100*(int(x100byte)-48)) + (10*(int(x010byte)-48)) + (int(x001byte)-48);   
    yPosition1 = (100*(int(y100byte)-48)) + (10*(int(y010byte)-48)) + (int(y001byte)-48);

So now in xPosition and yPosition keep getting values as long as the object is in the field of view on the webcam.

So when I run the processing sketch the LED is green from

 else
  {
    analogWrite(2,255);
    analogWrite(3,255);
    analogWrite(4,0);
  }

Then when I introduce the object to the webcam FOV the LED immediately turns blue because the x value is greater than zero

 if(xPosition1 > 0){
    analogWrite(2,255);
    analogWrite(3,0);
    analogWrite(4,255);
  }

Now what I would like it to do is for the led to be green when the object is out of the FOV and be blue when the object is in the FOV. I am assuming that it has something to do with the last serial value staying with the xPosition variable even after the object has left the screen. So basically I think the solution would be to reset the xPositon value after the arduino sees that no more serial information is coming in. I am just not sure the best way to do it, or if that is even a practical solution at all.
Thanks

    xPosition1 = (100*(int(x100byte)-48)) + (10*(int(x010byte)-48)) + (int(x001byte)-48);   
    yPosition1 = (100*(int(y100byte)-48)) + (10*(int(y010byte)-48)) + (int(y001byte)-48);

Make that easier to read, huh?

    xPosition1 = (100*(int(x100byte)-'0')) + (10*(int(x010byte)-'0')) + (int(x001byte)-'0');   
    yPosition1 = (100*(int(y100byte)-'0')) + (10*(int(y010byte)-'0')) + (int(y001byte)-'0');

Serial.read() returns int, so why do you need to cast it anyway?

http://snippets-r-us.com/

So basically I think the solution would be to reset the xPositon value after the arduino sees that no more serial information is coming in.

OK, well you can't really test for "no more serial" per se, you need to do something like "if I don't see any serial data for 5 seconds, turn the LED off".

What are you going to do when the position of the object is less than 100 in x or y?

OK, well you can’t really test for “no more serial” per se, you need to do something like “if I don’t see any serial data for 5 seconds, turn the LED off”.

Do you have any suggestions on how to go about doing this, that is the part that I am stuck on.

and PaulS

I eventually want to change the color of the led based on the position of an object on the screen, but for now I just want be able to turn off the led when the serial information is not new.

The whole code, please disregard the stray variables i have been struggling to get this and have wound up with a strange and messy version. Thanks

const int cameraW = 640;
const int cameraH = 480;
const int tolerance = 50;
int previousMillis;
int ledON = 255;
int ledStatus;
int ledOFF = 0;
int ledPin = 2;
int current_xPosition;
int currentMillis;
int x_pos = 0;
int y_pos = 0;
typedef struct config_t
{
  int camWidth;
  int camHeight;
  double propX;
  double propY;
  double xRatio;
  double yRatio;
  double xMin;
  double xMax;
  double yMin;
  double yMax;
}

configuration;
configuration configuration1;
int xPosition;                        
int yPosition; 
int xPosition1;                        
int yPosition1;
int idleCounter = 0;
int watchdog = 0;
boolean idle = true;
byte indicator;                       
byte x100byte;                       
byte x010byte;
byte x001byte;
byte y100byte;
byte y010byte;
byte y001byte;
void setup(){

  Serial.begin(4800);                    

}

void loop() {   

  if (Serial.available() >= 10) {      
    watchdog = 0;
    indicator = Serial.read();        
    if(indicator == 'a') {    
      delay(1000);    
      idle = false; 
      idleCounter = 0;
      x100byte = Serial.read();         
      x010byte = Serial.read();         
      x001byte = Serial.read();         
      y100byte = Serial.read();         
      y010byte = Serial.read();         
      y001byte = Serial.read();         

    }
  }
 if(idle) {                          // when Arduino is not getting commands from computer...
  Serial.write('T');                // tell the computer that Arduino is here
    idleCounter++;                              //  periodically blink the USB indicator LED
    if(idleCounter > 1000) {                   

      idleCounter = 0;                          
    }                                           

  }    
  else{                       
    xPosition = (100*(int(x100byte)-48)) + (10*(int(x010byte)-48)) + (int(x001byte)-48);  
    yPosition = (100*(int(y100byte)-48)) + (10*(int(y010byte)-48)) + (int(y001byte)-48);    
  }
x_pos = xPosition;
if (x_pos > 0 && x_pos < cameraW && y_pos > 0 && y_pos < cameraH){
  ledStatus = ledON;
}
else{
  ledStatus = ledOFF;
}
analogWrite(2,ledStatus);
}

Do you have any suggestions on how to go about doing this, that is the part that I am stuck on.

When you receive data you note the time. eg.

if (Serial.available ())
  lastDataReceived = millis ();  // last time something was available

In your main loop you see if a timeout has elapsed.

void loop ()
  {

  if (millis () - lastDataReceived >= 5000)
    {
    // uh oh, no data received for 5 seconds
    }