through beam sensor stopwatch help

hello all.

i am using arduino yun board.
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)

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.

The first test should be to determine if the current state and the previous state are not the same.

When you know that they are not, you print one message if the state changed from unbroken to broken and another if the state changed from broken to unbroken.

Do not apply boolean operators to non-boolean variables. if(state == LOW) takes a bit more typing, but makes your comparison crystal clear.