count and send pulses simultaneously

Hi everyone,

I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

Here is the code:

int encoderCount = 0;
boolean sampledPin;
boolean lastVal=0;

void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT);
pinMode(22,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

while(true)
{
sampledPin = !!(PIOB->PIO_PDSR & (1<<26));

if(lastVal ^ sampledPin)
{
encoderCount++;
lastVal = sampledPin;
}

if (encoderCount == 3000 )
{
encoderCount = 0;

PIOC->PIO_SODR=1<<28;
delayMicroseconds (1000) ;
PIOC->PIO_CODR=1<<28;
}
}

}