Billwilliams1952 / KY-040-Encoder-Library---Arduino

Arduino library for the KY-040 Encoder
40 stars 4 forks source link

Capacitor of 0.33uF #2

Closed gustavomassa closed 3 years ago

gustavomassa commented 6 years ago

Hello,

Awesome library! Can I use 0.33uF capacitor instead of 0.47uF? Is it enough for the switch bounces?

Billwilliams1952 commented 6 years ago

Hi from Shanghai China!

0.33uF should be fine. Give it a try and let me know if you have any issues.

Bill

Sent from my iPhone

On May 18, 2018, at 1:00 PM, Gustavo Massaneiro notifications@github.com wrote:

Hello,

Awesome library! Can I use 0.33uF capacitor instead of 0.47uF? Is it enough for the switch bounces?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

gustavomassa commented 6 years ago

Hi @Billwilliams1952

0.33uF capacitors works fine. What about adding some callbacks functions on the API for min/max values? Ex: When rotary counter reaches the maximum value it callback some hook function.

I'm using the rotary encoder to control some dc motors, so I can use the min/max values as angles and if it reaches the max value I can use the callbacks as emergency breaks.

Billwilliams1952 commented 6 years ago

Hi!

An excellent idea. I’m on vacation, but when I get back I will implement that.

Thanks, Bill

Sent from my iPhone

On May 21, 2018, at 12:26 PM, Gustavo Massaneiro notifications@github.com wrote:

Hi @Billwilliams1952

0.33uF capacitors works fine. What about adding some callbacks functions on the API for min/max values? Ex: When rotary counter reaches the maximum value it callback some hook function.

I'm using the rotary encoder to control some dc motors, so I can use the min/max values as angles and if it reaches the max value I can use the callbacks as emergency breaks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

gustavomassa commented 6 years ago

Hi Bill,

I've made some changes and tests on the lib.

typedef void (*encoderCallback)(int16_t value);

void registerRotaryMinCallback(encoderCallback cb);
void registerRotaryMaxCallback(encoderCallback cb);

//========================================================

static encoderCallback onRotaryMinValueCallback = NULL;
static encoderCallback onRotaryMaxValueCallback = NULL;

void ky040 ::registerRotaryMinCallback(encoderCallback cb)
{
    onRotaryMinValueCallback = cb;
}

void ky040 ::registerRotaryMaxCallback(encoderCallback cb)
{
    onRotaryMaxValueCallback = cb;
}

/*
 * Generic procedure to increment/decrement the rotary counter
 */
void ky040 ::UpdateRotaryCount(uint8_t pin, volatile encoderParams *params)
{
    //static unsigned long lastCallbackTime = 0; // the last time callback was called
    params->changed = true;
    /* TODO: Could use a direct port read here.... save some time.
     * TODO: Could just increment / decrement a counter. The actual value
     * could then be calculated in the GetRotaryValue function. This I
     * have to think about a bit more.
     */

    if (digitalRead(pin) == HIGH)
    {
        params->currentVal += params->inc;
        if (params->currentVal >= params->maxVal)
        {
            //noInterrupts();

            //Adjust max value
            if (params->currentVal > params->maxVal)
            {
                params->currentVal = params->rollOver ? params->minVal : params->maxVal;
            }
            //TODO: Rotary Max Value callbacks, send the current value. Should we stop interrupts before the callback?
            //if (onRotaryMaxValueCallback != NULL && (millis() - lastCallbackTime) > 50)
            if (onRotaryMaxValueCallback != NULL)
            {
                //noInterrupts();
                //lastCallbackTime = millis();
                onRotaryMaxValueCallback(params->currentVal);
                //interrupts();
            }
            //interrupts();
        }
    }
    else
    {
        params->currentVal -= params->inc;
        if (params->currentVal <= params->minVal)
        {
            //noInterrupts();

            //Adjust min value
            if (params->currentVal < params->minVal)
            {
                params->currentVal = params->rollOver ? params->maxVal : params->minVal;
            }
            //TODO: Rotary Min Value callbacks, send the current value. Should we stop interrupts before the callback?
            //if (onRotaryMinValueCallback != NULL && (millis() - lastCallbackTime) > 50)
            if (onRotaryMinValueCallback != NULL)
            {
                //noInterrupts();
                //lastCallbackTime = millis();
                onRotaryMinValueCallback(params->currentVal);
                //interrupts();
            }
            //interrupts();
        }
    }
}

This implementation of the callbacks is not working as expected, when it reaches max value it call the callback function, but when the encoder changes direction, it will pass through max value again, calling the callback twice. I tried to add some debounce time, but it did not help much.

I would like to have your advice/help about the rotary encoder, how can I simulate the behavior of the ky-40 to be similar as a 10K potentiometer, where arduino reads the analog input with range of 0-1024. I would like to create a rotary that can simulate that behavior.

I'm trying to use this rotary encoder in place of 10K pot for controlling motor feedback used on 6DOF car/flight simulator. All the projects that I've researched about uses pot/hall effect sensors. One issue regarding rotary encoder is that they don't retain their positional information at power down, but they have much better resolution. Here is an example of a prototype using pots: https://www.youtube.com/watch?v=HH5k8hnZokw

I've also tried the Arduino freeRTOS with some tasks and this lib, but I don't think the freeRTOS will fit well for this kind of application, sice the minimun slice time for the Arduino Uno is about 15ms. Maybe creating a task for each encoder? Here is the repository of the Arduino freeRTOS: https://github.com/feilipu/Arduino_FreeRTOS_Library

I will keep running tests and posting the results here.

Please enjoy your vacations :)