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:
// 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
}