Programing Encoders

I was wondering how to program the Optical Shaft encoders (not the quad shaft encoders…) and how to set up more than 1 of them in the variables table… meaning having one encoder for each side of the robot… left and right…

I am curently using the newest version of EasyC V2 and am also using the old vexnet adapter… i have learned robotC and from that i learned that the encoders should be in pair with the motors meaning that if a motor is in motor port 1 the encoder should be in analog/digital port 1(i also dont understand the analog/digital either…) my question is i want it to have the robot run the autonomous but on the driving portion use the input of the encoders to be more accurate… i cant seem to figure out how to get the robot to get out of the loop and move on to the next command… i dont understand where to start the encoder and where to preset it bc i would have to use the encoder multiple times… as in go forward(using encoders) then raise arm(no encoder) lower arm(no encoder) then go backward (using encoder)… etc… Also when would i want to stop all input form the encoder

also same with the limit switches i have got it to work in a while loop that is forever such in user controll but in the autonomous i cant get the robot to, when the limit is hit to stop the motors and run the next command…

Can someone please help by giving me an example on how to set any of these up… or anything would be nice.

Well I dont know much about EasyC because we stopped using it after the first competition in new zealand. it was a real bummer program, not enough power. anyway I can give you a few tips because these apply to both easyC and RobotC.

  1. for stopping loops: inside the loop have an if statement that will be used to determine when you want it to stop, and inside that if statement have break.
    this example shows you how to stop the infinite while loop after the encoder has made more than or equal to 1000 ticks.

while (1)
   if (example_encoder1 >= 1000)
  1. with the encoders, there should be commands which reset them, when they reset they are put back to 0, so at the top of each autonomous step, reset the required encoders and then continue, this way you only have to program from 0.

  2. for the limit switches, just do the same from the first tip but in the if statement, have something like this instead:

if (limit_switch == 1)
  1. analog and digital are two different things, its in the waveform but we wont get into details. You just need to know that analog sensors provide numbers of any range while digital sensors provide only on/off (1/0).
    encoder is analog, limit switch is digital.

Hope this helps!

I am puzzled as to to why you would use the break statement. It seems far more clear to do this:

while(SensorValue(encoder) < 5000){
  motor[port1] = 127;

My understanding is that encoders are digital. This is because they don’t actually sense a large range of values, they only return 1s and 0s (highs and lows?) to the microcontroller, depending on whether the infrared light inside is shining shrough the rotating slotted disk. The program then counts these changes to give the range of values we see. Please correct me if I’m not understanding this properly :slight_smile:

You are correct but when you program it, you use analog because it does not return on and off.

the encoder is an analog sensor
it returns values from 0 to 100000000…
so magicode’s code will be the simplest and most efficient

btw in easyC there needs to be a “get encoder” block in the loop
did you forget it, or is it not needed in robotC?

^ report ^

magicode’s code is useful and is another way of doing it but it may not be the most efficient, it depends on what you want.

also, RobotC does have something like the “get encoder” block, but you dont use it the same, you just copy the value from an array instead.

The code I wrote was for example purposes only, do not expect it to work straight-out-of-the-box. The syntax is correct, you just need to edit it to include what you need and to edit the variables for the respective platform.

The encoder is a digital sensor; each cable reports one of two possible states, and is connected to a digital input (interrupts are digital). The conversion to a large number is done in software.

You do use it in your program like an analog sensor, but it really is digital.


  • Dean