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:
i am tying to convert library from mbed to arduino but not work because attachinterrupt necessite a static function:
and
any idea ?
thanks for your help.
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.