Encoder problems

I was using an encoder to test a motor, and the encoder was giving me huge values after I had done some math with them. I changed my code to spit out the raw data, and found that the encoder was giving me values with an absolute value between 1.06 - 1.08 billion, even with a rotation of just a few ticks. I’m using EasyC, and I have the encoder plugged into digital ports 1 and 2. I’ve attached pictures of my setup.

This is my code:


#include "Main.h"

void main ( void )
{
      PresetQuadEncoder ( 1 , 2 , 0) ;
      StartQuadEncoder ( 1 , 2 , 0 ) ;
      Wait ( 5000 ) ;
      while ( 1 )
      {
            //SetMotor ( 1 , power ) ;
            encOld = GetQuadEncoder ( 1 , 2 ) ;
            Wait ( 100 ) ;
            encNew = GetQuadEncoder ( 1 , 2 ) ;
            power += 1 ;
            velocity = (encNew) ;
            //PrintToScreen ( "%d" , power ) ;
            PrintToScreen ( " %d\n" , velocity ) ;
      }
}

Please help.
2015-10-14_15-58-55_948.jpg
2015-10-14_15-59-10_914.jpg

Just bumping this up to the top. Please help me.

I program my encoders with raw data and I think you need to put the variable encOld or encNew into the Print to Screen instead of velocity and power and it should work just fine if you want to see the enconder ticks

What types are you variables? Is velocity an integer? Velocity will be proportional to the difference between encOld and encNew.

Power, encOld, and encNew are ints, velocity is a double. I adapted this from my previous code, velocity used to be (encNew-encOld). In this, it is not actually velocity. Sorry about the lack of clarity.

So I think you gave us the solution there.

This statement


PrintToScreen ( " %d\n" , velocity ) ;

is expecting the parameter to be an integer, and you are giving it a double. The format tag for a double would be %f, for example.


PrintToScreen("%6.2f\n", velocity);

%6.2f means use 6 places for the output with 2 places after the decimal point. Depending on the size of the number this may or may not be what you want.

Ok, thanks. That makes sense. I’ll try it when I get to a cortex, probably tomorrow or Monday.

It worked! Thank you very much!