need guidance on IR Sensor stopwatch coding.

hello all.

i am new to ardunio and i am trying to build a ir sensor stopwatch.
I am using omron EE-SPW311 through beam sensor. < http://www.ia.omron.com/data_pdf/cat/ee-spw311_411_ds_e_6_1_csm472.pdf > and an liquid lcd display with 1 push button to start/reset the stopwatch.

i connect the output of the sensor to a 4050b ic input ( non-inverting buffer) and the output for the ic is connected to the arduino analog input.

i have a stopwatch code and set up that is working.
i need help on how to test the sensor currenting i am using this code for sensor but i do not really understand:

sensor code :

int LEDPIN = 8;
int SENSORPIN = A0;

// variables will change:
int sensorState = 0, lastState = 0; // variable for reading the pushbutton status

void setup() {

pinMode(LEDPIN, OUTPUT);// initialize the LED pin as an output:

pinMode(SENSORPIN, INPUT);// initialize the sensor pin as an input:
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Serial.begin(9600);
}

void loop() {

sensorState = digitalRead(SENSORPIN);// read the state of the pushbutton value:

// if it is, the sensorState is LOW:
if (sensorState == LOW)// check if the sensor beam is broken
{
digitalWrite(LEDPIN, HIGH);// turn LED on:

}
else {

digitalWrite(LEDPIN, LOW);// turn LED off:
}

if (sensorState && !lastState) {
Serial.println(“Unbroken”);
}
if (!sensorState && lastState) {
Serial.println(“Broken”);
}
lastState = sensorState;
}

//end of code.

the problem with this code is that the serial monitor always automatically displays "broken and unbroken"at the same time whenever the sensor beam is blocked.

I am having trouble to combine it with a sensor program. ( i need the sensor to trigger the stopwatch to stop running when the through beam sensor detects.

the attached is a stopwatch code i am using.

stopwatch_test.ino (1007 Bytes)

Please provide schematics, and edit your post adding code tags </> around your code to make it readable.

Hello sir i have to design a viscometer. can i use IR sensor for this. Will the IR sensor gives the good results.
Actually i have to find the efflux time when liquid moves from level 1 to level 2 and display the time on lcd.

iqbaldar:
Hello sir i have to design a viscometer. can i use IR sensor for this. Will the IR sensor gives the good results.
Actually i have to find the efflux time when liquid moves from level 1 to level 2 and display the time on lcd.

First of all, your question has nothing to do with the topic at hand. In this case normally I'd suggest you to start a new topic. However in this case it also doesn't have anything to do with Arduino (at this stage), but everything with physics and physical chemistry.
I'm a chemical engineer, and I have no idea what you're trying to do, or whether it's even possible to use IR to measure the viscosity of a liquid. That is what your question seems to really be about, and that is totally out of the scope of this forum.