Counting pulses from an encoder without needing to use interruptions

Hello. I need to count the pulses from one channel of a rotary encoder in my Arduino Fio but without needing to use interruptions. So far, I came up with the following sketch and I was wondering if this could be the best approach. Any other ideas or comments? Thank you.

const int pulsePin = 2;
int counter=0;
int now = 0;
int last = 0;
void setup() {
  Serial.begin(115200);
}

void loop(){
  now = digitalRead(pulsePin);
    if (now != last) {    
    counter++;
    last = now;      
  } 
  else{
    last = now;
    }
  Serial.println(counter);
}

Looks fine although it will count on both rising and falling edges. Is that what you want?

Grumpy_Mike: Looks fine although it will count on both rising and falling edges.

Hello Mike, Yes. You are right. Silly of me. Here the corrected code that counts rising edges.

const int pulsePin = 2;
int counter=0;
int now = 0;
int last = 0;
void setup() {
  Serial.begin(115200);
}

void loop(){
  now = digitalRead(pulsePin);
    if (now == 1 && last == 0) {    
    counter++;
    last = now;      
  } 
  else{
    last = now;
      }
  Serial.println(counter);
}

Given that my encoder (100pulses/rev) has a clean pulse train, it is OK but I believe that my code is not much useful with a mechanical switch that generates "switch bounce".

Put the last equaled now outside the scope of the if and you only need have it once and so you don't need the else.