Rotary encoders to control the mouse cursor.

I'm trying to create a etch a sketch-esque mouse with one rotary encoder for the x axis and another for the y axis. I've seen it be done whit potentiometers but I cant find anything about it being done with rotary encoders.

My problem is that I'm pretty new at this and I'm not that good at it yet...

I found this code for reading the encoder, http://www.kevindarrah.com/download/arduino_code/Encoder.pde . But how can I implement this code in to a mouse to get it to move? Any tips?

But how can I implement this code in to a mouse to get it to move? Any tips?

On which Arduino? What have YOU tried?

Keep in mind that the Mouse library only deals with relative movement of the mouse. And only for certain Arduinos.

Leonardo

This is where I am at the moment

#include <Mouse.h>

//////////////

  int val; 
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;


void setup(){

pinMode(1, INPUT);  //mouse.begin and LMB
pinMode(2, INPUT); // axis
pinMode(5, INPUT); // mouse movment x1/x10
 
 //////////////
 
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);




 
 }

void loop(){

//Mus av/på

if(digitalRead  (1) == HIGH) {
  Mouse.begin();
   }
if(digitalRead (1) == LOW) {
  Mouse.end();
  }


if (digitalRead(1) == HIGH) {
    Mouse.press(MOUSE_LEFT);
  }
  
if (digitalRead(1) == LOW) {
    Mouse.release(MOUSE_LEFT);
  }




 n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
    
   } 
   encoder0PinALast = n;


if (digitalRead(5) == HIGH); {
  if (digitalRead(2) == HIGH);{
   if (encoder0Pos--); {
     Mouse.move(+1,0,0);
    }
  
   if (encoder0Pos--); {
     Mouse.move(-1, 0,0);
    }
      }

  if (digitalRead(2) == LOW);{
   if (encoder0Pos--); {
     Mouse.move(0,+1,0);
    }
  
  if (encoder0Pos--); {
     Mouse.move(0, -1,0);
    }
      }
}
 
if (digitalRead(5) == LOW); {
  if (digitalRead(2) == HIGH);{
   if (encoder0Pos--); {
     Mouse.move(+10,0,0);
    }
  
    if (encoder0Pos--); {
     Mouse.move(-10, 0,0);
    }
      }

  if (digitalRead(2) == LOW);{
   if (encoder0Pos--); {
     Mouse.move(0,+10,0);
    }
  
   if (encoder0Pos--); {
   Mouse.move(0, -10,0);
    }
      }
}
     
}

I feel like I'm slowly moving forward with this project :slight_smile:

The new idea is to use this thing to drag stuff more precise on the screen and, well... Lets face it , I'm also gonna play a bunch of old games!

#include <Mouse.h>

// Encoder

  int val; 
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;


void setup(){

pinMode(1, INPUT);                                          // x
pinMode(2, INPUT);                                          // y
pinMode(5, INPUT);                                          // x1 x10 mouse cursour movement
 
 // Encoder setup
 
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);




 
 }

void loop(){

// x/y, lmb and mouse.begin

if(digitalRead(1) == HIGH && digitalRead(2) == LOW);{       // x axis, mouse.begin, mouse.press
  Mouse.begin();
  Mouse.press(MOUSE_LEFT);
    }

if(digitalRead(1) == LOW && digitalRead(2) == HIGH);{       // y axis, mouse.begin, mouse.press
  Mouse.begin();
  Mouse.press(MOUSE_LEFT);
    }

if(digitalRead(1) == LOW && digitalRead(2) == LOW);{        //  off
  Mouse.end();
  Mouse.release(MOUSE_LEFT);
    }


// Encoder

 n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
    
   } 
   encoder0PinALast = n;

// Cursour movment


if (digitalRead(5) == HIGH); {                                // x1 cursour movement
  if (digitalRead(1) == HIGH);{                                 // x+
   if (encoder0Pos--); {
     Mouse.move(+1,0,0);
    }
  
   if (encoder0Pos--); {                                        // x-
     Mouse.move(-1, 0,0);
    }
      }

  if (digitalRead(2) == HIGH);{                                 // y+
   if (encoder0Pos--); {
     Mouse.move(0,+1,0);
    }
  
  if (encoder0Pos--); {                                         // y-
     Mouse.move(0, -1,0);
    }
      }
}
 
if (digitalRead(5) == LOW); {                                 // x10 cursour movment
  if (digitalRead(1) == HIGH);{                                 // x+
   if (encoder0Pos--); {
     Mouse.move(+10,0,0);
    }
  
    if (encoder0Pos--); {                                       // x-
     Mouse.move(-10, 0,0);
    }
      }

  if (digitalRead(2) == HIGH);{                                 // y+
   if (encoder0Pos--); {
     Mouse.move(0,+10,0);
    }
  
   if (encoder0Pos--); {                                        // y-
   Mouse.move(0, -10,0);
    }
      }
}
     
}
if(digitalRead(1) == HIGH && digitalRead(2) == LOW);{       // x axis, mouse.begin, mouse.press

Properly formatting this statement shows:

if(digitalRead(1) == HIGH && digitalRead(2) == LOW)
   ;   // Do nothing

{
       // x axis, mouse.begin, mouse.press

Is that what you want?

It is generally not a good idea to use pin 1 (or 0) for digital input as it is used by the hardware serial interface which you may need to use for debugging to print to the Serial monitor.

UKHeliBob:
It is generally not a good idea to use pin 1 (or 0) for digital input as it is used by the hardware serial interface which you may need to use for debugging to print to the Serial monitor.

On the Leonardo?

On the Leonardo?

Leonardo. Well, yes, you have a point......

Of course, should you need to use Serial1 for something then it is not a good idea to use pins 0 and 1 on the Leonardo, but that is a little more obscure, I agree.

PaulS:

if(digitalRead(1) == HIGH && digitalRead(2) == LOW);{       // x axis, mouse.begin, mouse.press

Properly formatting this statement shows:

if(digitalRead(1) == HIGH && digitalRead(2) == LOW)

;  // Do nothing

{
      // x axis, mouse.begin, mouse.press



Is that what you want?

I dont understand, wont it do anything? :confused:

I dont understand, wont it do anything? :confused:

The body of the if statement is the ;. That is a no-operation. It does nothing.

The useless if statement is then followed by a bunch of code in useless curly braces.

If you delete the ;, the story is completely different.

PaulS:
The body of the if statement is the ;. That is a no-operation. It does nothing.

The useless if statement is then followed by a bunch of code in useless curly braces.

If you delete the ;, the story is completely different.

Oh, I see. So i should take the away all ; before { in the if statements like this?

if(digitalRead(1) == HIGH && digitalRead(2) == LOW){       // x axis, mouse.begin, mouse.press
  Mouse.begin();
  Mouse.press(MOUSE_LEFT);

UKHeliBob:
Leonardo. Well, yes, you have a point......

Of course, should you need to use Serial1 for something then it is not a good idea to use pins 0 and 1 on the Leonardo, but that is a little more obscure, I agree.

I guess I can change it just in case i would need it in the future!

I also just found a litte little problem of me typing encoder0Pos-- insted of encoder0Pos++ on the x+ part :blush:

Oh, I see. So i should take the away all ; before { in the if statements like this?

If you want the code to work, yes.

Fredemo:
Oh, I see. So i should take the away all ; before { in the if statements like this?

if(digitalRead(1) == HIGH && digitalRead(2) == LOW){       // x axis, mouse.begin, mouse.press

Mouse.begin();
 Mouse.press(MOUSE_LEFT);

If you want to make PaulS happy, you should do it like this:

  if(digitalRead(1) == HIGH && digitalRead(2) == LOW)
  {
    // x axis, mouse.begin, mouse.press
    Mouse.begin();
    Mouse.press(MOUSE_LEFT);
    // other conditional code
  }

You get to make myself and other other programmers, with a good sense of style, happy at the same time. :slight_smile:

It's also easy to find matching braces this way.

DuaneDegn:
If you want to make PaulS happy, you should do it like this:

You get to make myself and other other programmers, with a good sense of style, happy at the same time. :slight_smile:

It's also easy to find matching braces this way.

Got it!

#include <Mouse.h>

// Encoder

  int val; 
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;


void setup(){


pinMode(5, INPUT);                                          // x1 x10 mouse cursour movement
pinMode(6, INPUT);                                          // x
pinMode(7, INPUT);                                          // y

 // Encoder setup
 
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);

 
 }

void loop(){

// x/y, lmb and mouse.begin

if(digitalRead(6) == HIGH && digitalRead(7) == LOW)
{       
  // x axis, mouse.begin, mouse.press
  Mouse.begin();
  Mouse.press(MOUSE_LEFT);
}

if(digitalRead(6) == LOW && digitalRead(7) == HIGH)
{       
  // y axis, mouse.begin, mouse.press
  Mouse.begin();
  Mouse.press(MOUSE_LEFT);
}

if(digitalRead(6) == LOW && digitalRead(7) == LOW)
{        
  //  off
  Mouse.release(MOUSE_LEFT);
  Mouse.end();
}


// Encoder

 n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) 
   {
     if (digitalRead(encoder0PinB) == LOW) 
     {
       encoder0Pos--;
     } 
     else 
     {
       encoder0Pos++;
     }
    
   } 
   encoder0PinALast = n;

// Cursour movment


if (digitalRead(5) == HIGH) 
{                                
  // x1 cursour movement
  if (digitalRead(6) == HIGH)
  {                                     
    // x+
   if (encoder0Pos++) 
   {
     Mouse.move(+1,0,0);
   }
  
   if (encoder0Pos--) 
   {                                              
    // x-
     Mouse.move(-1, 0,0);
   }
  }

  if (digitalRead(7) == HIGH)
  {                                   
   // y+
   if (encoder0Pos++) 
   {
     Mouse.move(0,+1,0);
    }
  
   if (encoder0Pos--) 
  {                                            
    // y-
     Mouse.move(0, -1,0);
   }
  }
}
 
if (digitalRead(5) == LOW) 
{                                 
  // x10 cursour movment
  if (digitalRead(6) == HIGH)
  {                                    
   // x+
   if (encoder0Pos++) 
   {
     Mouse.move(+10,0,0);
    }
  
    if (encoder0Pos--) 
    {                                             
     // x-
     Mouse.move(-10, 0,0);
    }
   }

  if (digitalRead(7) == HIGH)
  {                                   
   // y+
   if (encoder0Pos++) 
   {
     Mouse.move(0,+10,0);
    }
  
   if (encoder0Pos--) 
   {                                             
   // y-
   Mouse.move(0, -10,0);  
    }           
  }
}
     
}

I've hopefully made it more pretty now, fixed my little encoder0Pos++ mistake and I'm no longer using pin 1 :smiley:

I've tried to build it now and it doesn't work! The cursor just keep stuttering from left and right.

I used a light version of my code to test it out.

#include <Mouse.h>

// Encoder

  int val; 
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;


void setup(){


pinMode(5, INPUT);                                          // x1 x10 mouse cursour movement
pinMode(6, INPUT);                                          // x
pinMode(7, INPUT);                                          // y

 // Encoder setup
 
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);

 Mouse.begin();
 }

void loop(){


// Encoder

n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) 
   {
     if (digitalRead(encoder0PinB) == LOW) 
     {
       encoder0Pos--;
     
     } 
     else 
     {
       encoder0Pos++;
     }
    
   } 
   encoder0PinALast = n;

if (encoder0Pos--) {
  Mouse.move (-10, 0, 0);
}

if (encoder0Pos++) {
  Mouse.move (+10, 0, 0);
}

}

In some rare occasions when I turn the encoder it will move the way I turn it and after it goes back to stutter from left to right (about 1cm).

Is the code alright? Have I done something wrong with the hardware?

Edit: I can't see the picture I posted so I'm gonna post the link here http://postimg.org/image/va9rakg6r/.

if (encoder0Pos--) {
  Mouse.move (-10, 0, 0);
}

if (encoder0Pos++) {
  Mouse.move (+10, 0, 0);
}

What are you trying to do here? Unconditionally decrementing and incrementing encoder0Pos does not make sense to me.

PaulS:
What are you trying to do here? Unconditionally decrementing and incrementing encoder0Pos does not make sense to me.

I'm trying to get the mouse cursor to move as I turn the rotary encoder :confused:

Then, rather than incrementing and decrementing the encoder position, just move the mouse.

If that is too much trouble, you still need to make sure that the encoder position is positive before moving the mouse and decrementing, or is negative before moving the mouse and incrementing.

PaulS:
Then, rather than incrementing and decrementing the encoder position, just move the mouse.

If that is too much trouble, you still need to make sure that the encoder position is positive before moving the mouse and decrementing, or is negative before moving the mouse and incrementing.

Do you got any examples or know where I can find out more about how I would achieve that? I've tried replacing the encoder0Pos with Mouse.move and some other stuff like a new encoder code.

Remember I'm new at this and I appreciate everything that help me learn more.

I finaly got it to work! :smiley: