Hi Everybody
Hi, vffgaston, what is your serial speed?
Set it to maximum, 115200, this will speed the sketch up.
This is true, if I change serial speed to 115200 in this code, it will speed up, and get a little more acurracy, but it's no working well:
volatile boolean fired;
volatile boolean up;
#define PINA 20
#define PINB 21
#define INTERRUPT 3 // that is, pin 20
// Interrupt Service Routine for a change to encoder pin A
void isr ()
{
if (digitalRead (PINA))
up = digitalRead (PINB);
else
up = !digitalRead (PINB);
fired = true;
} // end of isr
void setup ()
{
digitalWrite (PINA, HIGH); // enable pull-ups
digitalWrite (PINB, HIGH);
attachInterrupt (INTERRUPT, isr, CHANGE); // interrupt 0 is pin 2, interrupt 1 is pin 3
Serial.begin (9600);
} // end of setup
void loop ()
{
static long rotaryCount = 0;
if (fired)
{
if (up)
rotaryCount++;
else
rotaryCount--;
fired = false;
Serial.print ("Count = ");
Serial.println (rotaryCount);
} // end if fired
} // end of loop
So its better to set up serial speed to 115200 in all sketch up?? 
I've tested this code and works very well for me, has anybody tested it?
//PIN's definition
#define encoder0PinA 2
#define encoder0PinB 3
volatile int encoder0Pos = 0;
volatile boolean PastA = 0;
volatile boolean PastB = 0;
void setup()
{
Serial.begin (9600);
pinMode(encoder0PinA, INPUT);
//turn on pullup resistor
//digitalWrite(encoder0PinA, HIGH); //ONLY FOR SOME ENCODER(MAGNETIC)!!!!
pinMode(encoder0PinB, INPUT);
//turn on pullup resistor
//digitalWrite(encoder0PinB, HIGH); //ONLY FOR SOME ENCODER(MAGNETIC)!!!!
PastA = (boolean)digitalRead(encoder0PinA); //initial value of channel A;
PastB = (boolean)digitalRead(encoder0PinB); //and channel B
//To speed up even more, you may define manually the ISRs
// encoder A channel on interrupt 0 (arduino's pin 2)
attachInterrupt(0, doEncoderA, RISING);
// encoder B channel pin on interrupt 1 (arduino's pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
}
void loop()
{
Serial.print ("Count = ");
Serial.println ( encoder0Pos);
delay(10);
}
//you may easily modify the code get quadrature..
//..but be sure this whouldn't let Arduino back!
void doEncoderA()
{
PastB ? encoder0Pos--: encoder0Pos++;
}
void doEncoderB()
{
PastB = !PastB;
}
Best regards
Ivan Barquero
P.D.
vffgaston, sí ,hablo español. Soy de Tudela, navarra, ( me habrás reconocido por mi cutre-ingles xd), no se si en la comunidad les molestará que escribamos en Castellano cuando este Post esta originalmente escrito en inglés, (pero desde luego me explicaría mejor)
El tema de la precision del Encoder, con el segundo código esta solucionado ( creando las ISR manualmente), aunque me gustaría que alguien más pudiera testear los codigos.
Otra cuestion es que he encontrado poca informacion sobre "Using pin change interrupts (PCINT)", ya que mi mega 2560 solo tiene 6 interrupts y me gustaría poder tener más. no se si alguien me podrá explicar ( igual debería postear en otro post relacionado con el tema,
Saludos!