Quadrature encoder help

Status
Not open for further replies.

Shipy

New member
Hello all,

I wrote a simple program for 2 encoders and 2 buttons using encoder public library, my question is if is possible to change encoders " println" assignment using a momentary push buton.

I would like to press a button and change key assignment for Enc A (c,d) & B (x,y) to different keys. This way I could use 2 encoders (save $$ ) It should go like this:

Default MyEnc A (c,d) & MyEnc(x,y).........press button once switch to different keyboard keys..........press again for default........I just need a little guidance for my project, should I use " state changed button" examples or

" edge detection" for this ? I'm new to Arduino programming this is my first post.


#include <Encoder.h>
#include <Bounce.h>

Bounce button0 = Bounce(0, 100);
Bounce button1 = Bounce(1, 100); // 10 = 10 ms debounce time, increase time if button too sensitive

Encoder myEncA(2, 3);
Encoder myEncB(4, 5);

void setup() {

pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);

}
long oldPositionA = -999;
long oldPositionB = -999;

void loop() {

button0.update();
button1.update();

if (button0.fallingEdge()) {
Keyboard.println("a");
}
if (button1.fallingEdge()) {
Keyboard.println("b");
}
// Encoder A
long newPositionA = myEncA.read();
if (newPositionA > oldPositionA) {
oldPositionA = newPositionA;
Keyboard.println("c");
delay(150);
}
if (newPositionA < oldPositionA) {
oldPositionA = newPositionA;
Keyboard.println("d");
delay(150);
}
// Encoder B
long newPositionB = myEncB.read();
if (newPositionB > oldPositionB) {
oldPositionB = newPositionB;
Keyboard.println("x");
delay(150);
}
if (newPositionB < oldPositionB) {
oldPositionB = newPositionB;
Keyboard.println("y");
delay(150);
}

Thank you very much I appreciate your help.
 
Not quite clear what you are trying to do there but if aim is to have the two buttons toggle between two possible sets of keys when you press them you can use a variable to store which mode you are in"
byte keymode=0;

then in your node where you check the buttons for falling edge do something like
if (keymode==0) keymode=1; else keymode=0;//note two equals is comapre, single equals sets a value, so if(keymode=0) will always be true as you set keymode during the if check
Then in your encoder values you if keymode to work out which letter to send

If you want to reduce the amount of duplicate code and the errors that can lead to, an improvement is to use arrays to store which key to send.
https://www.arduino.cc/en/Reference/Array
So then your encoder if statement looks at an array to work out which key should be sent.
 
Like what the previous poster said, but You can also write your code so every time you press the button, you increment a counter.

initiate an array to be 2 units, like: char EncAkeys[2]

If the count is 0, change EncAkeys[0] = a; EncAkeys[1] = b;
If the count is 1, change EncAkeys[0] = c; EncAkeys[1] = d;
If the count is 2, change EncAkeys[0] = e; EncAkeys[1] = f;
If the count is 3, change EncAkeys[0] = g; EncAkeys[1] = h;
If the count is 4, change the counter to 0 (to start over)

Then instead of "Keyboard.println("d");" you'll always be using Keyboard.println(EncAkeys[0]); or Keyboard.println(EncAkeys[1]); depending on which way you're turning the encoder.
 
Status
Not open for further replies.
Back
Top