PDA

View Full Version : Encoder version 1.1 library with Arduino Micro(Leonardo)



slomobile
04-01-2013, 04:56 PM
The library comments state that it was untested with the Leonardo. So far it appears to work fine with Arduino 1.0.4 provided you do not define ENCODER_OPTIMIZE_INTERRUPTS. If you do, as with the code below, the following compile time errors are generated. Since this is the same chip as the Teensy 2.0, would it be difficult to get the same optimizations working? I'm having difficulty tracing what is happening with the library code. My Arduino micro is available for any code you need tested.

In file included from StepPlatform.ino:32:
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h: In function 'void __vector_1()':
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:184: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:914: error: within this context
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h: In function 'void __vector_2()':
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:184: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:917: error: within this context
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h: In function 'void __vector_3()':
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:184: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:920: error: within this context
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h: In function 'void __vector_4()':
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:184: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\slo\Documents\Arduino\libraries\Encoder/Encoder.h:923: error: within this context



#include <Wire.h>
// This optional setting causes Encoder to use more optimized code,
// It must be defined before Encoder.h is included.
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>
//INPUT
static const uint8_t BUTTON_PIN = 7;
static const uint8_t SPEED_POT_PIN = A0;
static const uint8_t JOY_X_PIN = A1;
static const uint8_t JOY_Y_PIN = A2;
static const uint8_t ROTATE_POT_PIN = A3;
Encoder knobX(0, A4);
Encoder knobY(1, A5);
int32_t positionX = -999;
int32_t positionY = -999;
//OUTPUT
static const uint8_t DUINO_LED_PIN = 13;

void setup()
{
Serial.begin(9600);//USB
// Serial1.begin(9600);// TTL serial pins are used for encoder interrupts. Software serial ties up interrupts so it is not a good workaround.
Wire.begin(); //I2C pins are the only remaining interrupt pins. It is a tough choice to loose either TTL serial or i2c for the sake of fast encoder performance.

// ramp up LED brightness then blink once to signal end of setup and give time to launch IDE serial monitor
pinMode(DUINO_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(DUINO_LED_PIN, LOW);
for ( int i = 0; i < 255; i++ )
{
analogWrite(DUINO_LED_PIN, i);
delay(30);
}
digitalWrite(DUINO_LED_PIN, LOW);
delay(750);
digitalWrite(DUINO_LED_PIN, HIGH);
}

void loop()
{
long newX, newY;
newX = knobX.read();
newY = knobY.read();
if (newX != positionX || newY != positionY) {
Serial.print("X = ");
Serial.print(newX);
Serial.print(", Y = ");
Serial.print(newY);
Serial.println();
positionX = newX;
positionY = newY;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knobX.write(0);
knobY.write(0);
}
}

PaulStoffregen
04-06-2013, 04:37 PM
I don't support Arduino Leonardo.

But if you get it working well and submit a good patch, I'll merge it for the next release.

Akilan
04-05-2015, 05:22 PM
Hi,
I am using Arduino UNO. Still running into these errors while using optimized interrupts (#define ENCODER_OPTIMIZE_INTERRUPTS). Its working fine otherwise.

In file included from test.ino:2:
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h: In function 'void __vector_1()':
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h:185: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h:915: error: within this context
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h: In function 'void __vector_2()':
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h:185: error: 'static void Encoder::update(Encoder_internal_state_t*)' is private
C:\Users\AkilanG\Documents\Arduino\libraries\Encod er/Encoder.h:918: error: within this context

#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>


#define motor_step 4 //0x10
#define motor_direction 5 // 0x20

volatile long encoder;
unsigned long microsNow = micros();
unsigned long timeStamp = 0;
char lastCommand = '0';
char motorStop = '0';

long encoder_time;
long encoder_diff; //Meritev časa med impulzi enkoderja
byte inputA;
byte inputB;

Encoder fastEncoder(2, 3);
void setup () {
DDRD &= ~0x04;
DDRD &= ~0x08;
PIND = 0x04;
PIND = 0x08;
pinMode(motor_step, OUTPUT);
pinMode(motor_direction, OUTPUT);
digitalWrite(motor_direction, LOW);
}

void loop() {
encoder = fastEncoder.read();
if ((encoder > 0) ) {
PORTD = PORTD | 0x04;
PORTD = PORTD & 0xEF;
PORTD = PORTD | 0X10;
}
else if ((encoder < 0) ) {
PORTD = PORTD | 0x04;
PORTD = PORTD & 0xEF;
PORTD = PORTD | 0X10;
}
}

Thank you

Akilan
04-05-2015, 05:34 PM
Sorry. I found the solution here http://forum.arduino.cc/index.php?topic=163847.0