Hell folks,
I need your insight and help. I am working on a project to use the data from two encoder to control the mouse cursor in my PC. The two encoder data from a hand robot: one records my finger range of motion data, one records my thumb range of motion data. Two encoder data send out from the robot computer via RS232 and logic level convertor to my Arduino Leonardo. And Leonardo is connected to another computer where the encoder data controls the mouse cursor to play game. One encoder data controls the x coordinate, another controls the y coordinate.
My problem lies here. The two encoder data directly correspond to the fingers and thumb movement data. When fingers and thumb move at a moderate speed, the mouse cursor moves in a kind of synchronized manner (by naked eye standard). When fingers and thumb move faster, there is quite obvious delay in the mouse cursor movement, although there is no encoder data lost while Arduino and game computer process the data. In other words, the mouse cursor will follow the same movement done by fingers and thumb, but is quite a delay response. And moreover, when fingers and thumb move even faster to a certain level, the mouse cursor movement starts going wild and seems to me that there is kind of overflow problem happening to Arduino. And it also seems to me this problem is non versatile since it persists even after I reloads the code and turn off and on Arduino. But the problem usually disappears after I restart my game computer.
Hope anyone shares some insight and idea on it. Thanks a lot in advance. The arduino code is attached here. In the code, f and t are defined to be the encoder data of fingers and the encoder data of thumb.
const int switchPin = 2;
const int mouseButton = 3;
const int ledPin = 13;
int value = 0;
boolean mouseIsActive = false;
int prevSwitchState = HIGH;
int prevButtonState = LOW;
int toggle = 0;
int flag = 0;
int f;
int t=0;
float f1;
float t1;
int f_prev = 0;
int t_prev = 0;
// dont forget to change back
int datain_Byte[6];
int th;
int fi;
int z;
void setup()
{
pinMode(switchPin,INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(115200);
Serial1.begin(115200);
Mouse.begin();
while(!Serial);
}
void loop()
{
mouseActiveFcn();
dataReading();
if (mouseIsActive == true){
switch(z){
case 1:{
while(z == 1){
dataReading();
if(value == 1){
angryBirdGame();
}
angryBirdMouseButton();
mouseActiveFcn();
if(z != 1 || mouseIsActive == false){
break;
}
}
}
case 2: {
while (z == 2){
dataReading();
if(value == 1){
bubbleShootGame();
}
clickMouse();
mouseActiveFcn();
if(z != 2 || mouseIsActive == false){
break;
}
}
}
case 3:{
while (z == 3){
dataReading();
if(value == 1){
bowlingGame();
}
mouseActiveFcn();
if (z != 3 || mouseIsActive == false){
break;
}
}
}
case 4:{
while (z == 4){
dataReading();
if(value == 1){
shoppingGame();
}
clickMouse();
mouseActiveFcn();
if (z != 4 || mouseIsActive == false){
break;
}
}
}
case 5:{
while (z == 5){
dataReading();
if(value == 1){
golfGame();
}
clickMouse();
mouseActiveFcn();
if (z != 5 || mouseIsActive == false){
break;
}
}
}
case 6: {
while (z == 6){
dataReading();
if(value == 1){
panggame();
}
mouseActiveFcn();
if (z != 6 || mouseIsActive == false){
break;
}
}
}
default:{
while(1){
dataReading();
generalMove();
mouseActiveFcn();
if( z == 1 ){
break;
}
else if (z == 2){
break;
}
else if (z == 3) {
break;
}
else if (z == 4) {
break;
}
else if (z == 5) {
break;
}
else if (z == 6){
break;
}
else if (mouseIsActive == false){
break;
}
}
}
}
delay(10);
}
}
void mouseActiveFcn(){
int switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
if (switchState == LOW) {
mouseIsActive = !mouseIsActive;
digitalWrite(ledPin, mouseIsActive);
}
}
prevSwitchState = switchState;
delay(10);
}
void dataReading()
{
if(Serial1.available()>5) {
for (int n = 0; n < 6; n++)
{
datain_Byte[n] = Serial1.read();
value = 1;
}
z = 256*datain_Byte[1] + datain_Byte[0];
f = (256*datain_Byte[3] + datain_Byte[2])/10;
t = (256*datain_Byte[5] + datain_Byte[4])/10;
//Serial.println(f);
//Serial.println(t);
f_prev = f;
t_prev = t;
}
else {
value = 0;
f = f_prev;
t = t_prev;
}
}
void angryBirdGame(){
//th = map(t,0,20,38,22);
th = map(t,0,18,28,13);
//fi = map(f,0,42,70,90);
fi = map(f,0,48,70,88);
Mouse.move(th, fi, 0);
}
void angryBirdMouseButton(){
int buttonState = digitalRead(mouseButton);
if (buttonState == HIGH && prevButtonState == LOW) {
// if the mouse is not pressed, press it:
toggle++;
if(toggle == 1)
{
if(t < 5 && f < 6){
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
//Serial.println(toggle);
}
else
{ toggle = 0;
//Serial.println(toggle);
}
}
else if(toggle == 2)
{
// else the mouse button is not pressed:
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
toggle = 0;
}
}
prevButtonState = buttonState;
}
void bubbleShootGame(){
th = map(t,0,18,80,0);
fi = map(f,0,48,100,0);
Mouse.move(th, fi, 0);
}
void clickMouse(){
int buttonState = digitalRead(mouseButton);
if (buttonState == HIGH && prevButtonState == LOW) {
Mouse.click();
}
prevButtonState = buttonState;
delay(10);
}
void shoppingGame(){
th = map(t,0,18,115,0);
fi = map(f,0,48,108,0);
Mouse.move(th,fi,0);
}
void golfGame(){
th = map(t,0,7,65,60);
fi = map(f,0,17,95,100);
Mouse.move(th, fi, 0);
//delay(10);
}
void panggame(){
Serial.write(t);
Serial.write(f);
//delay(100);
}
void generalMove(){
th = map(t,0,7,0,120);
fi = map(f,0,17,0,120);
Mouse.move(th, fi, 0);
int buttonState = digitalRead(mouseButton);
if (buttonState == HIGH && prevButtonState == LOW) {
Mouse.click();
prevButtonState = buttonState;
}
delay(10);
}
void holdMouse(){
int buttonState = digitalRead(mouseButton);
if (buttonState == HIGH && prevButtonState == LOW) {
// if the mouse is not pressed, press it:
toggle++;
if(toggle == 1)
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else if(toggle == 2)
{
// else the mouse button is not pressed:
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
toggle = 0;
}
prevButtonState = buttonState;
delay(10);
}