Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: teensy 3.6 and library encoder not work

  1. #1

    teensy 3.6 and library encoder not work

    hi,

    i am testing Encoder library but not work.

    when i turn encoder to left i have 0 4 8 12 16 ...
    when i turn encoder to right i have always the last value.

    encoder work fine on mbed (STM32F767ZI) when an library encoder from mbed
    i have 0 1 2 3 4 5 ... and 4 3 2 1 0

    the code:

    Code:
    #include <Encoder.h>
    
    Encoder enc(26, 25);
    
    int current_value;
    int previous_value = -999;
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      current_value = enc.read();
      
      if(previous_value != current_value)      
      {   
        
      
        Serial.println(current_value);
        previous_value = current_value;
      }
    }
    i am tying to convert library from mbed to arduino but not work because attachinterrupt necessite a static function:

    Code:
    #ifndef ROTARY_ENCODER_H
    #define ROTARY_ENCODER_H
    
    #include "Arduino.h"
    
    /**
     */
    class RotaryEncoder {
    public:
        /**
         * Create rotary encoder.
         *
         * @param pin1_name
         * @param pin2_name
         * @param min Minimum value.
         * @param max Maximum value.
         * @param val Default value.
         */
        RotaryEncoder(int pin1_name, int pin2_name, int min = 0, int max = 100, int val = 50);
    
        /**
         * Dispose.
         */
        ~RotaryEncoder();
    
        /**
         * Get the minimum value.
         *
         * @return The minimum value.
         */
        int getMin() const {
            return minVal;
        }
    
        /**
         * Get the maximum value.
         *
         * @return The maximum value.
         */
        int getMax() const {
            return maxVal;
        }
    
        /**
         * Get the value.
         *
         * @param The value.
         */
        int getVal() const {
            return val;
        }
    
    private:   
        int pin1;
        int pin2;
        const int minVal;
        const int maxVal;
        int val;
      
        /**
         * Internal tick function.
         */
        void func_ticker();
    };
    
    #endif
    and

    Code:
    #include "RotaryEncoder.h"
    
    /**
     * Create rotary encoder.
     *
     * @param pin1_name
     * @param pin2_name
     * @param min Minimum value.
     * @param max Maximum value.
     * @param val Default value.
     */
    RotaryEncoder::RotaryEncoder(int pin1_name, int pin2_name, int minVal, int maxVal, int val): pin1(pin1_name), pin2(pin2_name), minVal(minVal), maxVal(maxVal), val(val) {
          
        pinMode(pin1, INPUT_PULLUP);
        pinMode(pin2, INPUT_PULLUP);
        attachInterrupt(0, func_ticker,  CHANGE);
    }
    
    /**
     * Dispose.
     */
    RotaryEncoder::~RotaryEncoder() {
      
    }
    
    /**
     * Internal tick function.
     */
    void RotaryEncoder::func_ticker() {
        static uint8_t code;   
        code = (code << 2) + (((digitalRead(pin1) << 1) | (digitalRead(pin2) << 0)) & 3);
        code &= 15;
        switch (code) {
            case 0x7:
                if (minVal < val) {
                    val--;
                } else {
                    val = minVal;
                }
                break;
            case 0xd:
                if (val < maxVal) {
                    val++;
                } else {
                    val = maxVal;
                }
                break;
        }
        
    }
    any idea ?
    thanks for your help.

  2. #2
    i have modified the code below

    enc.read() to enc.read() >> 2
    now the value is incrementd to 1, but cannot decrement when i turn the encoder

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,837
    Encoders are different but the one here changes by 4 between full clicks as well.

    Generally when there is a native library for Teensy it has been optimized to best utilize the features and functions of the Teensy in use - even differentiating between the specific properties of the MCU used on each Teensy.

    Since that is a signed values using right shift 2 : ">>2" is not right for negative numbers. Using divide by 4 works as I see it - with occasional 'print bunches' jitter on transitions, the jitter I suppose is from updating too fast.

    This code has a timer to 'debounce' preventing reading too fast:
    Code:
    #include <Encoder.h>
    
    Encoder enc(26, 25);
    
    int current_value;
    int previous_value = -999;
    
    void setup() {
    	Serial.begin(9600);
    	while (!Serial && millis() < 4000 );
    	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
    }
    
    elapsedMicros eusT;
    void loop() {
    	if ( eusT > 100 ) {
    		current_value = enc.read() / 4;
    		if (previous_value != current_value)
    		{
    			Serial.println(current_value);
    			previous_value = current_value;
    			eusT = 0;
    		}
    	}
    }

  4. #4
    the problem is that it works but only in one direction,
    why it work only in one direction ?

  5. #5
    it work now, a pin was inverted it's for that, thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •