Multi encoder sketch with acceleration
madias Mon Apr 27, 2015 3:51 pm
madias Mon Apr 27, 2015 3:51 pm
mausi_mick Sat Nov 12, 2016 3:08 am
…
if ( (gpio_read_bit(PIN_MAP[encoderpinB[counter]].gpio_device, PIN_MAP[encoderpinB[counter]].gpio_bit) ? HIGH : LOW) != B_set[counter] )
{
B_set[counter] = !B_set[counter];
if ( B_set[counter] && !A_set[counter] )
// <###################################### {
if (millis() – encodertimer > 3)
encoderpos[counter] -= 1;
else
encoderpos[counter] -= 5;
// <######################################## }
encodertimer = millis();
}
mausi_mick
RogerClark Sat Nov 12, 2016 6:18 am
RogerClark Sat Dec 03, 2016 7:56 am
BTW.
I tried you code and it worked fine, but I found I needed to use the hardware timer for another purpose, so I found it doesnt work too bad if you simply turn on the internal pullup resistor and use a 100nF capacitor between the output of the rotary encoder and GND.
I know this isnt as good as the 2 resistors and one capacitor debounce method, but it worked OK for me.
The only drawback with this simple method (using attachinterrupt on input A), is that if you turn really quickly, it looses some steps and you don’t get the 5x acceleration. But I could add acceleration by getting the millis in the interrupt handler and if it was less than some small value, since the last interrupt, then I could add or subtract 5 instead of 1.
But I do like the external analog debounce method.
Pito Mon Dec 05, 2016 9:40 am
max_sl Thu Dec 15, 2016 2:51 pm
Pito Thu Dec 15, 2016 7:02 pm
#define ENCODER_RATE 1000 // in microseconds; RogerClark Thu Dec 15, 2016 7:23 pm
As long as you use hardware debounce, e.g an internal pullup resistor and a small capacitor from the encoder output to gnd, this worked well.
But if you rotate quickly, it still misses steps because of the debounce time.
I tuned the capacitor value for my particular encoder, as it only seemed to need 50nF with the internal 22k pullup.
But even then, I could make it loose steps, so one approach to hiding this, is to increase the amount that you change the position variable if you detect the encoder is being rotated quickly.
Pito Thu Dec 15, 2016 7:35 pm
RogerClark Thu Dec 15, 2016 10:05 pm
I tried a few capacitors starting much higher but I didn’t have anything less than 50nF in my box of spares (well I had some 22pf etc but they were much too low)
I agree perhaps 10nF would be OK.
max_sl Fri Dec 16, 2016 9:12 am
u16 cnt = TIM3->CNT;
Delay(100);
But there are no available descriptions of translation stm32 functions to stm32duino ( in Internet)
P.S.
Transition “millis” to “micros” cannot give an effect for my device.
Pito Fri Dec 16, 2016 9:52 am
max_sl Fri Dec 16, 2016 10:27 am
if ( (gpio_read_bit(PIN_MAP[encoderpinA].gpio_device, PIN_MAP[encoderpinA].gpio_bit) ? HIGH : LOW) != A_set )
{
A_set = !A_set;
if ( A_set && !B_set )
{
//if (millis() - encodertimer > 3)
encoderpos += 1;
//else
//encoderpos += 5;
}
encodertimer = micros();
}