Hello,
I am trying to write a sketch to create an additional keyboard to pilot some softwares I am using with rotary encoders.
The software accepts keys shortcut so I just need arduino to send the same letter (or combination of letters) to the software in order to interact with it: basically, every time I turn the rotary encoder one way it should send the exact same signal.
I am using this sketch:
#include "Keyboard.h"
int encoderPin1 = 4;
int encoderPin2 = 5;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
void setup() {
Serial.begin (9600);
Keyboard.begin();
pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop(){
Serial.println(encoderValue);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
// PRESS Q EVERY TIME TURNED
Keyboard.write('Q');
Keyboard.end();
}
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
}
The problem I got is that, every turn, Arduino sends 'Q' twice.
I don't get why that happens...
Also, I have read to be careful using Keyboard.h as Arduino board might get stucked in that state.
So, everytime I use a function with "Keyboard.write()" shall it be followed by the function Keyboard.end()?
Sorry for the noob questions...
Is there a more convenient way to manage rotary encoders on Arduino DUE?
On Arduino UNO I was using "Encoder_Polling_V2.h" which worked like magic but it seems like it doesn't work on Arduino DUE.
Many many thanks as usual for your precious support!
My best,
Alessandro
PS: Eventually I should be able to read 43 Rotary Encoder plus some other buttons.
Could I use something like THIS to save pins with so many rotary encoders on Arduino DUE?