i have problems with the sensors QTr

Hello i have a problem with a function that i create, this function let me know if the sensors see black or white, but the problem is that i sometimes see that my varieble last have negative valor,so i dont know why this happen, this is my code

unsigned long time;
int color=0,last,last_color;

int x,y;

  void setup(){
// put your setup code here, to run once:
pinMode(10,INPUT); //Sensor de linea de la Izquierda
pinMode(9,INPUT); //Sensor de linea de la derecha
digitalWrite(10,HIGH); //Configurar la resistencia puesta en vcc
digitalWrite(9,HIGH); //Configurar la resistencia puesta en vcc
int QTR(int x){

// Serial.println(color);
// delay(1000);
while(digitalRead(x)) {}

 if(last>1000 ){ color=1;}
     else  {color=0;}
//   if(color!=last_color) Sserial.print(color),Serial.println(" ");
//   last_color=color;
return color;
void loop(){
while(QTR(9)==1 & QTR(10)==1){
Serial.println("ambos pines detectan negro");
//  delay(1000);
 while(QTR(9)==0 & QTR(10)==1){
Serial.println("9 detecta blanco 10 detecta negro");
  while(QTR(9)==1 & QTR(10)==0){
Serial.println("9 detecta negro 10 detecta blanco");
 while(QTR(9)==0 & QTR(10)==0){
Serial.println("9 detecta blanco 10 detecta blanco");
  last = micros() - time;

last is declared as a [u]signed[/u] int micros() returns an unsigned long time is an unsigned long

All the timing variables should be unsigned long

You seem to be trying to read an ultrasonic range finder. You are returning 0 or 1 based on the PREVIOUS value of 'last' and then calculating a new value for 'last'. Is that what you wanted to use? The previous value?

Note: Since the '>' operator returns 1 for true and 0 for false you can reduce:

 if (last > 1000 )
  {color = 1;}
  {color = 0;}


  color = (last > 1000 );