Teensy Mouse With Encoder

Status
Not open for further replies.

AMaraklov

Active member
So I'm sure this a failure of my math skills, and I've tried to remove(subtract) the oldEncOne from the value coming in encOne.Read() and then the mouse just jumps all over the place, I also don't know how to do a 3'rd axis... as move only takes 2 values. What I'm trying to do is take 3 Encoders as steering wheels for games like Super Sprint, and have each drive a steering wheel, which in Mame needs a separate mouse axis, and Z apparently is a valid axis in mame on a mouse.
The below functionality is: as the encoder is moved the mouse starts accelerating, instead of moving and stopping when the encoder stops.

Code:
/* Simple USB Mouse Example
   Teensy becomes a USB mouse and moves via an encoder

   You must select Mouse from the "Tools > USB Type" menu
*/

#include <Encoder.h>

// Encoder Pin Assignment
Encoder encOne(23, 22);
Encoder encTwo(21, 20);
Encoder encThree(19, 18);

long newEncOne, newEncTwo, newEncThree;
void setup() { } // no setup needed
void loop() {

  // Read The Encoders
  
  newEncOne = encOne.read()*.01;
  newEncTwo = encTwo.read()*.01;
  newEncThree = encThree.read()*.01;
   
  Mouse.move(newEncOne, 0);
  Mouse.move(0, newEncTwo);    
  Mouse.scroll(newEncThree);  
}
 
So I'm sure this a failure of my math skills, and I've tried to remove(subtract) the oldEncOne from the value coming in encOne.Read() and then the mouse just jumps all over the place, I also don't know how to do a 3'rd axis... as move only takes 2 values. What I'm trying to do is take 3 Encoders as steering wheels for games like Super Sprint, and have each drive a steering wheel, which in Mame needs a separate mouse axis, and Z apparently is a valid axis in mame on a mouse.
The below functionality is: as the encoder is moved the mouse starts accelerating, instead of moving and stopping when the encoder stops.

Code:
/* Simple USB Mouse Example
   Teensy becomes a USB mouse and moves via an encoder

   You must select Mouse from the "Tools > USB Type" menu
*/

#include <Encoder.h>

// Encoder Pin Assignment
Encoder encOne(23, 22);
Encoder encTwo(21, 20);
Encoder encThree(19, 18);

long newEncOne, newEncTwo, newEncThree;
void setup() { } // no setup needed
void loop() {

  // Read The Encoders
  
  newEncOne = encOne.read()*.01;
  newEncTwo = encTwo.read()*.01;
  newEncThree = encThree.read()*.01;
   
  Mouse.move(newEncOne, 0);
  Mouse.move(0, newEncTwo);    
  Mouse.scroll(newEncThree);  
}

So Apparently I have to answer my own questions, so after a lot of print statements the lynch pin is a delay of 1-2ms... in the loop without which you get the same value in old and new... which makes subtracting one from the other pointless and doesn't work... so... here's my updated working code for a optical encoder.


Code:
/* Simple USB Mouse Example
   Teensy becomes a USB mouse and moves via an encoder

   You must select Mouse from the "Tools > USB Type" menu
*/

#include <Encoder.h>

// Encoder Pin Assignment
Encoder encOne(23, 22);
Encoder encTwo(21, 20);
Encoder encThree(19, 18);

static float multiplier = 0.01;
long newEncOne, newEncTwo, newEncThree;
long oldEncOne, oldEncTwo, oldEncThree;

// Debug enable / disable
bool debug = 0;

void setup() { 
  Serial.begin(9600);
  // Intiialize values
  newEncOne = 0;
  newEncTwo = 0;
  newEncThree = 0;
  oldEncOne = 0;
  oldEncTwo = 0;
  oldEncThree =0;
  } 
  
void loop() {

  // Read The Encoders    
  newEncOne = encOne.read()*multiplier;
  newEncTwo = encTwo.read()*multiplier;
  newEncThree = encThree.read()*multiplier;

  // Update Mouse position if it's changed
  if ((newEncOne - oldEncOne) != 0){
    Mouse.move(newEncOne - oldEncOne, 0);    
  }
  if ((newEncTwo - oldEncTwo) != 0){
    Mouse.move(0, newEncTwo - oldEncTwo);    
  }
  if ((newEncThree - oldEncThree) != 0){
    Mouse.scroll( newEncThree - oldEncThree);    
  }

  if (debug){
    Serial.print("oldEncOne : ");
    Serial.print(oldEncOne);
    Serial.println();   
    Serial.print("newEncOne : "); 
    Serial.print(newEncOne);  
    Serial.println(); 
    Serial.print("------------------------");
    Serial.println(); 
  }

  // Update Old Values after delay to allow new value to change
  delay (1);
  oldEncOne = newEncOne;
  oldEncTwo = newEncTwo;
  oldEncThree = newEncThree;
  
}
 
So Apparently I have to answer my own questions, so after a lot of print statements the lynch pin is a delay of 1-2ms... in the loop without which you get the same value in old and new... which makes subtracting one from the other pointless and doesn't work... so... here's my updated working code for a optical encoder.


Code:
/* Simple USB Mouse Example
   Teensy becomes a USB mouse and moves via an encoder

   You must select Mouse from the "Tools > USB Type" menu
*/

#include <Encoder.h>

// Encoder Pin Assignment
Encoder encOne(23, 22);
Encoder encTwo(21, 20);
Encoder encThree(19, 18);

static float multiplier = 0.01;
long newEncOne, newEncTwo, newEncThree;
long oldEncOne, oldEncTwo, oldEncThree;

// Debug enable / disable
bool debug = 0;

void setup() { 
  Serial.begin(9600);
  // Intiialize values
  newEncOne = 0;
  newEncTwo = 0;
  newEncThree = 0;
  oldEncOne = 0;
  oldEncTwo = 0;
  oldEncThree =0;
  } 
  
void loop() {

  // Read The Encoders    
  newEncOne = encOne.read()*multiplier;
  newEncTwo = encTwo.read()*multiplier;
  newEncThree = encThree.read()*multiplier;

  // Update Mouse position if it's changed
  if ((newEncOne - oldEncOne) != 0){
    Mouse.move(newEncOne - oldEncOne, 0);    
  }
  if ((newEncTwo - oldEncTwo) != 0){
    Mouse.move(0, newEncTwo - oldEncTwo);    
  }
  if ((newEncThree - oldEncThree) != 0){
    Mouse.scroll( newEncThree - oldEncThree);    
  }

  if (debug){
    Serial.print("oldEncOne : ");
    Serial.print(oldEncOne);
    Serial.println();   
    Serial.print("newEncOne : "); 
    Serial.print(newEncOne);  
    Serial.println(); 
    Serial.print("------------------------");
    Serial.println(); 
  }

  // Update Old Values after delay to allow new value to change
  delay (1);
  oldEncOne = newEncOne;
  oldEncTwo = newEncTwo;
  oldEncThree = newEncThree;
  
}

I would still like to know if there is support for a third axis for a mouse, I believe that is supported on PCB's like a U-HID but the question is, is that the scroll wheel or a true third axis... I don't know what HID Mouse devices support.
 
Status
Not open for further replies.
Back
Top