upper and lower limit on an encoder counter

hey guys, i am controlling some fans with an encoder, but i wanted to have limits on the counter for the encoder, i have tried the "constrict" method to no avail, thanks for all help and advice

#define outputA 3
#define outputB 4
int counter = 0;
int aState;
int aLastState;

const int buttonPin = 7;
int button = 0;

int uplimit = 512;
int lowlimit = -512;

int fanSpeed1 = 0;
int fanSpeed2 = 0;
int fan = 6;


void setup() 
{
  pinMode(outputA, INPUT);
  pinMode(outputB, INPUT);
  aLastState = digitalRead(outputA);

  pinMode(buttonPin, INPUT);
  
  pinMode(6, OUTPUT);
  analogWrite(6,0);
  Serial.begin(9600);
  Serial.println();
}

void loop()
{
  aState = digitalRead(outputA);
  fanSpeed1 = map(counter, -512, 512, 0, 255);
  fanSpeed2 = constrain(fanSpeed1, 10, 255);
    if (aState != aLastState)
    {
      if (digitalRead(outputB) != aState)
        {
          counter ++;
        }
      else
        {
          counter --;
        }
      Serial.print("POS:");
      Serial.print(counter);
      Serial.print(" FS:");
      Serial.println(fanSpeed2);
     }
  aLastState = aState;
  analogWrite(fan, fanSpeed2);
 }

Hi. Please modify your post above to use code tags.

ragginn1:
i have tried the "constrict" method to no avail

I suggest using more avail.

(Which is my sarcastic way of saying that your comment is unhelpful. What actually happens? What did you want/expect to happen?)

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

I suspect you’ve misunderstood your encoder and how it works, and immediately see that you also use ‘counter’, but omit setting it to any value.

PaulRB:
I suggest using more avail.

(Which is my sarcastic way of saying that your comment is unhelpful. What actually happens? What did you want/expect to happen?)

ofcouse, i was sleep deprived so i forgot to go deeper into the problem. So, the plan is to count the "steps" of the encoder and have it stop at -512 and 512, witch "constrict" does, but if i keep turning after the counter hits the limit i then have to turn the same amount of steps/turns before it has an effect on the counter. Also here are links to the schematics.

schematic: https://i.imgur.com/dyMiEgP.png

drawing: https://i.imgur.com/xVaeluD.png

Perhaps you need to set the counter back to zero when it hits a limit.

Paul

You have basically written the encoder library, but a quadrature detector is pretty basic.

I am wondering about this code:

 fanSpeed1 = map(counter, -512, 512, 0, 255);
  fanSpeed2 = constrain(fanSpeed1, 10, 255);

Why not just map the counter to 10,255 ?

But, I think I see your problem. Nowhere in the code are you limiting counter to -512 and +512, and if counter is outside of the from range, so will be the output of the map function. For example, if counter is =520, then the mapped output will be something proportionally greater than 255.

So, just limit the counter when it's adjusted:

if (counter++ > 512) counter=512;

SteveMann:
You have basically written the encoder library, but a quadrature detector is pretty basic.

I am wondering about this code:

 fanSpeed1 = map(counter, -512, 512, 0, 255);

fanSpeed2 = constrain(fanSpeed1, 10, 255);




Why not just map the counter to 10,255 ?

But, I think I see your problem. Nowhere in the code are you limiting *counter* to -512 and +512, and if counter is outside of the from range, so will be the output of the map function. For example, if *counter* is =520, then the mapped output will be something proportionally greater than 255.

So, just limit the counter when it's adjusted:



if (counter++ > 512) counter=512;

Thanks a bunch, this solved it, had a bit of a problem with the placement of the code piece you gave me, but i worked it out with a bit of messing. Code looks like this if you are wondering, but you probably knew that already, also thanks for the fanspeed tip. made the code nicer to look at :slight_smile:

{
  aState = digitalRead(outputA);
  fanSpeed1 = map(counter, 0, 1023, 10, 255);
    if (aState != aLastState)
    {
      if (digitalRead(outputB) != aState)
        {
          counter ++;
          if (counter++ > 1023) counter=1023;
        }
      else
        {
          counter --;
        }
        if (counter-- < -2) counter=0;
      Serial.print("POS:");
      Serial.print(counter);
      Serial.print(" FS:");
      Serial.println(fanSpeed1);
     }
  aLastState = aState;
  analogWrite(fan, fanSpeed1);
 }

SteveMann:

 fanSpeed1 = map(counter, -512, 512, 0, 255);

fanSpeed2 = constrain(fanSpeed1, 10, 255);




Why not just map the counter to 10,255 ?

map function does not constrain, if the input variable goes out side the two input values in the map functon, the output of the map function will go outside the two output values.
Try it with a pot and the IDE serial monitor.

Tom… :slight_smile:

ragginn1:
the plan is to count the "steps" of the encoder and have it stop at -512 and 512

Why? The fan speed you want is between 10 and 255, so why not stop at those values? Then you would not need to use map().

ragginn1:
Also here are links to the schematics.

Please post schematics and pictures as described in the forum guide. Thanks for correcting the code tags in your original post, +1 karma.

PaulRB:
Why? The fan speed you want is between 10 and 255, so why not stop at those values? Then you would not need to use map().
Please post schematics and pictures as described in the forum guide. Thanks for correcting the code tags in your original post, +1 karma.

yeah, i just use the “map()” to increase accuracy at the sacrifice of speed. i also used the built in button of the encoder to add multiple “modes” each with different resolution, also thanks for the 1st karma :slight_smile:

TomGeorge:
map function does not constrain, if the input variable goes out side the two input values in the map functon, the output of the map function will go outside the two output values.
Try it with a pot and the IDE serial monitor.

Tom… :slight_smile:

{
          counter ++;
          if (counter++ > 1023) counter=1023;
        }
      else
        {
          counter --;
        }
        if (counter-- < -2) counter=0;

here i just use two if conditions to restrict the counter with in the limit, with out the ++ & --, so it doesnt count twice

Hi,
OPs circuit;

Have you got any speed control at the moment?

Tom... :slight_smile:
PS, You need to draw a circuit diagram and use the appropriate symbols, with labels. You are not using a ROB08420 stepper. Hand drawing will give you ALL the component symbols you need.
If you need to know the symbol for a fan , google schematic fan symbol

TomGeorge:
Hi,
OPs circuit;

Have you got any speed control at the moment?

Tom... :slight_smile:
PS, You need to draw a circuit diagram and use the appropriate symbols, with labels. You are not using a ROB08420 stepper. Hand drawing will give you ALL the component symbols you need.
If you need to know the symbol for a fan , google schematic fan symbol

hey, yeah i have the speed control nailed. And yeah i for sure am not using a stepper, but that drawing was made very quickly and with alot of cringe of using a stepper symbol, my old teacher would not be happy if he saw that drawing, should have made it more clear, but i did specify the pins on the drawing.