Help with logic for optical encoder?

int encoder(){
  
  int read = digitalRead(4);
 if(read != lastRead){
 	var = 1;
 }
  if((var == 1) && (read != lastRead)) {
    encoderPos++;
    var = 0;
    lastRead = read;
  }

 }

Is that wrong? Can someone tell me a right algorithm?

The sensor is working well, but looks like encoderPos isn't getting bigger.

There is more than one kind of encoder. Please post the vendor link for the encoder.
As an example here is a link for quadrature encoder , but does anyone but you know if that is what you are using ?
http://playground.arduino.cc/Main/RotaryEncoders

It's a optical sensor.

(deleted)

It's a optical sensor.

please post the vendor link for the encoder.

(is this too vague ? Do you understand what this means ?)

Someone?

(please explain to the OP that there is more than one kind of optical encoder in the world)

Have you heard of Google ?

http://multimechatronics.com/images/uploads/design/2012/Optical%20Encoder%20and%20the%20Arduino%202012.pdf
file:///C:/Users/Robert/Downloads/01-Encoder.pdf
[ https://www.youtube.com/watch?v=xgIubJHVRwg ](http:// https://www.youtube.com/watch?v=xgIubJHVRwg)

see Reply#14 in this post:
[ http://forum.arduino.cc/index.php/topic,44817.0.html ](http:// http://forum.arduino.cc/index.php/topic,44817.0.html)

it's just a normal optical sensor

The logic would be the same anyway, so, will you try to help me with the logic?

it's just a normal optical sensor

Then your post title is incorrect, isn't it ? (because your post has nothing to do with ENCODERs because you don't know a sensor from an encoder) (right ?)

If it is not an encoder, maybe you should stop treating it like one ? (or calling it one)

The sensor is working well, but looks like encoderPos isn't getting bigger.

I'm just sayin'......

You need to tell us (or post a photo) how you are using this reflective sensor. What is it installed on and what is it detecting ?
Also, what is the distance in mm from the sensor to the target object ?
Is being used as an RPM sensor ?
https://www.google.com/search?q=arduino+reflective+rpm+sensor&tbm=isch&imgil=nT45GiVoZqXm6M%253A%253By37hZ2zN5cCiFM%253Bhttp%25253A%25252F%25252Fwww.8051projects.net%25252Fnews-i178-contactless-digital-tachometer-using-pic-microcontroller.html&source=iu&fir=nT45GiVoZqXm6M%253A%252Cy37hZ2zN5cCiFM%252C_&usg=__zNtyRU8H9CdE4ixXrdvzlTtIOkU%3D&sa=X&ei=tEgCVKWdNauWjAL_24HIBQ&ved=0CCQQ9QEwAg&biw=1394&bih=650#facrc=_&imgdii=_&imgrc=nT45GiVoZqXm6M%253A%3By37hZ2zN5cCiFM%3Bhttp%253A%252F%252Fwww.8051projects.net%252Fimages%252Ftacho-logic.jpg%3Bhttp%253A%252F%252Fwww.8051projects.net%252Fnews-i178-contactless-digital-tachometer-using-pic-microcontroller.html%3B500%3B359

Have you seen the schematic at this link ?

How is your sensor wired ? Post a schematic of your sensor wiring. If necessary, draw it on a piece of printer paper and taka a photo and post that.

The sensor is working well

Based on what ?

Have you seen this tutorial ?
http://www.seeedstudio.com/wiki/Grove_-_Infrared_Reflective_Sensor

  #include <TimerOne.h>
unsigned int counter=0;
void blink()
{
  counter++;
} 
void timerIsr()
{
  Timer1.detachInterrupt();  //disable the timer1
  Serial.print("The speed of the motor: "); 
  Serial.print(counter,DEC);  
  Serial.println("round/s"); 
  counter=0;  
  Timer1.attachInterrupt( timerIsr );  //enable the timer1
}
void setup() 
{
  Serial.begin(9600);
  Timer1.initialize(1000000); // set a timer of length 1sec
  attachInterrupt(0, blink, RISING);  //INT0
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
} 
void loop()
{
  ;  //do nothing
}