Go Down

Topic: Rotary encoder speed control, is encoder turning fast? update the counter +10 (Read 3174 times) previous topic - next topic

ZeTeX

Hi,

I have a rotary encoder library that I have downloaded from the internet, and the code for reading the rotary encoder works perfectly but there is something that I would like to add to the code to make it better, what I want to add is "speed control" for the encoder, so for example, if I spin the encoder fast, instead of updating the counter once every reading of the encoder changing position, I would increase the counter x2 faster, this way if I'm using the encoder to set a value between 0-1000 instead of rotating the encoder 1000 times to get to the 1000 val I would only need to spin for example 100 times if I spin it fast, if I would spin it slow it would only update the encoder like usual, once per step.

So I taught about checking if the (if) function that runs in a loop that is checking if the encoder is rotating is accomplished for 5 times at least (5 updates to the counter) in less then 250ms for example, it would update the counter by 5 every step. the numbers are not critical as I can adjust them if I find the encoder too sensitive to speed.

My question is simple, How do I do this? I've tried various method like checking time elapsed and things like that, I also taught about maybe having a for loop and checking if the loop reached to 50 for example and the if statement accomplished for 5 time but I'm pretty new to arduino so I also had problems with that.

This is the code:
Code: [Select]

// Designed by ZeTeX -- for battery powered PSU -- 07/1/17 //

#include <Rotary.h>
Rotary rotary = Rotary(5, 6); // Connect Encoder To Pin 5, 6.

unsigned int counter;

void setup() {
  Serial.begin(57600);
  Serial.println("Start");
  pinMode(11, OUTPUT);
  }

  void loop() {
  unsigned char result = rotary.process();
  if (result == DIR_CW && counter < 255) { // Is the encoder rotating CW? if yes, counter ++
        counter++;
        Serial.println(counter);
  }
  //
    else if (result == DIR_CCW && counter > 0) { // Is the encoder rotating CCW? if yes, counter --
    counter--;
    Serial.println(counter);
  }
  //

   analogWrite(11, counter); // Output a voltage of the encoder reading
  }




ZeTeX

This blog post from some time ago explains one way that you could do this:
https://arduinoplusplus.wordpress.com/2016/04/20/adaptive-user-input-with-a-rotary-encoder/
The Idea is nice but I'm having more of a problem of implementing it in the code, like measuring the time and things like that, the idea he does is basically what I taught of doing, but because I'm simply new to arduino I have never worked with measuring time and things like that so I just can't get it working.
I would like first to know how exactly can I measure the time between each pulse of the encoder (each (if) checking), how would you do such things? all I need to do is to check the time between each encoder pulse reading and if the time is smaller then 250ms for example this means I'm turning the encoder fast, so then I could just make the counter jump in 25 (25, 50, 75, 100..) or any other value like that.
the idea of doing it logarithmic is nice but really I only need to step it from 0-255 and not 0-20000.

Robin2

"taught" is what a teacher did yesterday. "thought" is what my brain did yesterday (well I think it did).

Without knowing how your library works I cannot say how you can time the pulses from the encoder while using the library.

If you had your own Interrupt Service Routine (ISR) to detect the pulses it would be easy to get it to record the value of micros() for every pulse and if the difference between the value for the previous pulse and for the current pulse was less than X you could increment the counter by more the 1.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

marco_c

Quote
'm having more of a problem of implementing it in the code
Well, you have a working library that you can use or look at how it works.

Quote
measure the time between each pulse of the encoder
... or measure number of encoder ticks in a time period

Quote
I only need to step it from 0-255 and not 0-20000
As per the blog article, the step size is highly tunable to what you need it to be.

Go Up