encoder

thank you very much for your help. you teach me a lot, men. i am very greatful!
yes, i will try my best to correct my sketch.
You've mentioned about testing whether encoder counter is greater than 40.

Val = analogRead(0);
if (val < 20)
{
if(encoder0Pos > 40){ // testing if the encoder count is greater than 40
digitalWrite(3, HIGH);
delay(1000);
}
}

how can i change this testing statement into a sketch for "testing if the encoder counter is increased(or decreased) until 40"?
i hope to find out how to make the encoder counter to start to count till 40(ex : from 240 to 280) at the timing point of analog input receiption.