Arduino Mouse Encoder

Hi
I’m Trying To Make A Mouse Using Arduino Leonardo But I Couldn’t Solve The Encoder Part

Problematic Code I’m Working on:

#include “Mouse.h”
#include <Encoder.h>
#define Encoder_h_

int encoderA = 3;
int encoderB = 4;

volatile int previous = 0;
volatile int counter = 0;

void setup() {
pinMode(encoderA, INPUT_PULLUP);
pinMode(encoderB, INPUT_PULLUP);

attachInterrupt(digitalPinToInterrupt(encoderA), changed, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderB), changed, CHANGE);

Mouse.begin();
}

void changed() {
int A = digitalRead(encoderA);
int B = digitalRead(encoderB);

int current = (A << 1) | B;
int combined = (previous << 2) | current;

if(combined == 0b0010 ||
combined == 0b0100 ||
combined == 0b1011 ||
combined == 0b1101) {
counter++;
}

if(combined == 0b0001 ||
combined == 0b0111 ||
combined == 0b1000 ||
combined == 0b1110) {
counter–;
}

previous = current;
}

void loop(){
if(counter >= 4) {
Mouse.move(0,0,1);
counter -= 4;
} else if(counter <= -4) {
Mouse.move(0,0,-1);
counter += 4;

}
}

A far more efficient way to read an encoder would be to note the patterns in all those bit patters you're building:

// The two pins from the encoder
const byte epinA = 2;
const byte epinB = 3;

int printInterval = 500;

volatile int encoderCount = 0;

void setup(){

  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(epinA), eintA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(epinB), eintB, CHANGE);
  
}

void loop(){

  static unsigned long pt = millis();
  unsigned long ct = millis();
  
  int encoderRead = 0;

  if(ct - pt >= printInterval){
    cli();
    encoderRead = encoderCount;
    sei();
    Serial.println(encoderRead);
  }
  
}
/*
  This is actually too slow for fast encoders.
  Should use direct port reads instead of 
  digitalRead.  But this illustrates the point.
  if your encoder ends up going backwards
  just switch the ++ and -- in both ISR's. 
*/ 

void eintA(){
  // if the pins read the same we're going one direction
  // else we're going the other direction.
  if(digitalRead(epinA) == digitalRead(epinB)){
    encoderCount++;
  }
  else {
    encoderCount--;
  }
}

void eintB(){
  // Same as above, except the directions are switched for
  // the other pin.  
  if(digitalRead(epinB) == digitalRead(epinA)){
    encoderCount--;
  }
  else {
    encoderCount++;
  }
}

To make it even faster, take the digitalRead lines and make them direct port reads.

Problem Appears in Video

errore.mpg (621 KB)

Post the error. I don’t recognize that extension so I’m not clicking that. You copy and post it here.

Problem Appears in Video

Ok maybe someone at YouTube will help you. When you’re ready to get help here post the error message and describe the problem.

Delta_G:
A far more efficient way to read an encoder would be to note the patterns in all those bit patters you're building:

// The two pins from the encoder

const byte epinA = 2;
const byte epinB = 3;

int printInterval = 500;

volatile int encoderCount = 0;

void setup(){

Serial.begin(115200);

attachInterrupt(digitalPinToInterrupt(epinA), eintA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(epinB), eintB, CHANGE);
 
}

void loop(){

static unsigned long pt = millis();
  unsigned long ct = millis();
 
  int encoderRead = 0;

if(ct - pt >= printInterval){
    cli();
    encoderRead = encoderCount;
    sei();
    Serial.println(encoderRead);
  }
 
}
/*
  This is actually too slow for fast encoders.
  Should use direct port reads instead of
  digitalRead.  But this illustrates the point.
  if your encoder ends up going backwards
  just switch the ++ and -- in both ISR's.
*/

void eintA(){
  // if the pins read the same we're going one direction
  // else we're going the other direction.
  if(digitalRead(epinA) == digitalRead(epinB)){
    encoderCount++;
  }
  else {
    encoderCount--;
  }
}

void eintB(){
  // Same as above, except the directions are switched for
  // the other pin. 
  if(digitalRead(epinB) == digitalRead(epinA)){
    encoderCount--;
  }
  else {
    encoderCount++;
  }
}





To make it even faster, take the digitalRead lines and make them direct port reads.

This Code Does Not Work as Mouse Encoder

It wasn’t supposed to. It was supposed to work to read an encoder. It was a better way to read an encoder than what you had.

And it does work for that, thigh as mentioned in the text also in that post it could be better. It was to illustrate a concept, not as a ready made solution to your problem. You were supposed to learn something from it, not just copy it.