school project

I'm doing a project where I have to find time enters one end of a rug the other end (2 rugs are) but this is not the values ??of the time as it should appear on the LCD.

Anyone can discover the error in the program?

ps: I can not change the pins that are attached sensors

#include <Wire.h>
#include <LiquidCrystal.h>



LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


  int lcd_key     = 0;
  int adc_key_in  = 0;
  int sensor1 = 2;
  int sensor2 = A5;
  int sensor3 = 3;
  int sensor4 = 13;
  unsigned long time1 = 0;
  unsigned long time2 = 0;
  unsigned long time3 = 0;
  unsigned long time4 = 0;
  


void setup()
  {  
   pinMode(sensor1, INPUT);
   pinMode(sensor2, INPUT);
   pinMode(sensor3, INPUT);
   pinMode(sensor4, INPUT);
   lcd.begin(16, 2);              
   lcd.setCursor(0,0);
   lcd.print ("* Queda Graves *");
   lcd.setCursor(0,1);
   lcd.print (" V1.0      2014 ");
   delay (1500);
  }


void loop ()
  {
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print ("Tb1           ms");
  lcd.setCursor (0,1);
  lcd.print ("Tb2           ms");
  if (digitalRead(sensor1) == HIGH) {
    time1 = millis ();
    if (analogRead(sensor2) == HIGH) {
      time2 = millis();
      lcd.setCursor (5, 0);
      delay(100);
      lcd.print (time1 - time2); 
      Serial.println(time1 - time2);
      }
    }
 else if (digitalRead(sensor3) == HIGH) {
    time3 = millis();
  if (digitalRead(sensor4) == HIGH) {
    time4 = millis();
    lcd.setCursor (5, 1);
    delay(100);
    lcd.print (time3 - time4);
    Serial.println(time3 - time4);
    }
   }
  }

Perhaps you just need to do "time2 - time1". The second reading of millis() will always be the biggest one. Integer arithmetic give strange answers when you subtract a bi number from a small number.

You might have spotted this yourself if you used more meaningful names such as startTime and endTime.

Another thing that would help is to print out the actual values of time2 and time1 so you could check them with a calculator.

...R

Avlis:
I have to find time enters one end of a rug the other end (2 rugs are)

Can you describe that a bit more clearly? I can't make sense of that. The code looks as if you are expecting four input events to occur in sequence and derive some timing from them, but it's not clear what. However, the values you're getting for time1 .. time4 seem rather pointless at the moment since these values will keep getting updated as long as the corresponding inputs remain HIGH so any values you derive from them will not tell you anything about when the input changed to HIGH.