encoder driving me crazy, help

this should be simple, but evidently its way over my head.

I'm using the new encoder library, loaded the basic code and it works great. So now I want to DO something with it, great.

So I set up pin 13 as an out put and analog write the value from the encoder to it, sorta works.

first issue is obvious, the basic encoder example sets up the variables oldPosition and newPosition as longs, so obviosly this will cause issues on the eight bit PMW.

Easy fix, i just changed the longs to bytes, sweet.

Now the only thing that i cant seem to fix is it will roll over at 255 to 0, or backwards from 0 to 255.

I tried going back to a long and using the constrain function, this kept it from rolling over, but if i turned the encoder 3 full turns past 255, i had to turn it back 3 full turns before it would start counting down.

I would really like to use an encoder for lots of projects, but if i cannot force usable minimums and maximums to the data coming from it, its kind of useless.

any help??

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

 byte oldPosition  = 0;

void loop() {
  byte newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

It's not obvious what you are trying to achieve.

The reason the encoder values are long, is so that you can count a lot of rotations from the device without having
to deal with count rollover too often.

If you want to get a number between 0 and 255 from that, you could just take the low byte from the long.

Instead of just copying new position to oldposition, only do it if the newpos is less than your upper limit.

Better still work on the difference between old and new, and add the difference in only when your are within bounds. Using the difference has the advantage that going down can happen immediately.

Treat the long counter as just an indication on what the encoder is doing and use the information to maintain your own user interface number. By splitting the two you have more control.

It's not obvious what you are trying to achieve.

The reason the encoder values are long, is so that you can count a lot of rotations from the device without having
to deal with count rollover too often.

If you want to get a number between 0 and 255 from that, you could just take the low byte from the long.

Sorry, I will try to be more clear. I have several projects in mind that could benefit from the use of an encoder. For now, I am starting simple, i want the encoder to act like most modern "volume" controls. If I turn it "up", a variable in arduino will increment up to a specific point, in the case of a simple PWM output, the maximum high value will be 255. Once the maximum value has been reached, any further "up" ticks from the encoder will be ignored, and/or read as 255. Same rules would apply to down ticks, once the variable has incremented down to 0, further down ticks would be ignored.
Right now, it will either roll over (if i limit the variable to a byte), or the variable will keep incrementing in the background if i constrain the long to 0, 255.

Once I can get the variable that is input by the encoder to have limits, and not roll over, then can use that variable to control various I2c chips (digital pots, 4-20ma current Ic's, dedicated 12-16 bit PWM ic's) etc.

thanks for the help

Instead of just copying new position to oldposition, only do it if the newpos is less than your upper limit.

Better still work on the difference between old and new, and add the difference in only when your are within bounds. Using the difference has the advantage that going down can happen immediately.

Treat the long counter as just an indication on what the encoder is doing and use the information to maintain your own user interface number. By splitting the two you have more control.

This sounds like it might work, and is kind of what i have been trying. i have been trying various "if" or "if else" statements in the loop, but nothing worked, now that you pointed out that this code copies newposition to oldposition (don't know why i didn't see that before) its a little clearer why trying to constrain one or both was not working, i think..... :~ .

I will try to split the newpos and oldpos, i think that will get me what i want.

Thanks,

joe

The interface to that encoder library appears to me to be rather unhelpful, because it assumes you want to control a full 32-bit value, whereas in practice you always want to limit the range of the variable that the encoder controls. My own encoder library (at GitHub - dc42/arduino: Reusable modules, drivers and patches for the Arduino platform) doesn't return an absolute value, instead it returns the number of clicks that the encoder has rotated since you last made a call to it, so that you can apply your own limits.

If you do want to use that library that you linked to, then I suggest you do what has already been suggested, i.e. keep track of the last value you read from it so that you can calculate the change between readings.

The interface to that encoder library appears to me to be rather unhelpful, because it assumes you want to control a full 32-bit value, whereas in practice you always want to limit the range of the variable that the encoder controls. My own encoder library (at GitHub - dc42/arduino: Reusable modules, drivers and patches for the Arduino platform) doesn't return an absolute value, instead it returns the number of clicks that the encoder has rotated since you last made a call to it, so that you can apply your own limits.

If you do want to use that library that you linked to, then I suggest you do what has already been suggested, i.e. keep track of the last value you read from it so that you can calculate the change between readings.

Thanks, I downloaded your library, I will try it in the morning.