Mouse control using serial data + labtop touch pad control

Hello folks,

sorry the subject title is a little confusing, I am working on a project using serial data from two encoders to control the mouse cursor of laptop to play Angry bird game. Each of two encoders is connected to a motor. There is some similar code on web. Overall my code is working just up to meet the goals I set. Basically there is a push button to turn on and off the power of my Arduino Leonardo (pin 2) and another push button works as the left click key of the mouse (pin 3). The code is attached in the end.

The only goal I haven’t achieved so much is that I am not able to use the touch pad to take the control of mouse again when the serial data coming in from encoders are zero., when the Andruino is ON. In other words, I expect that I can still use the touch pad to move the cursor to anywhere I want on the laptop screen when no serial data is sending in, even though my Arduino is turned on all the time. I am running of ideas to solve this problem. Thank you so much for your suggestions in advance.

const int switchPin = 2;
const int mouseButton = 3;
const int ledPin = 13;

boolean mouseIsActive = false;
int prevSwitchState = HIGH;
int prevButtonState = LOW;

int toggle = 0;

int f;
int t;
// dont forget to change back 
int datain_Byte[4];
int th;
int fi;

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){  
      dataReading();
      angryBirdGame(); 
      mouseActiveFcn();    
   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()>3) {
      
      for (int n = 0; n < 4; n++)
      {
        datain_Byte[n] = Serial1.read();
      }
         
          f = 256*datain_Byte[1] + datain_Byte[0];
          t = 256*datain_Byte[3] + datain_Byte[2]; }
          
 
  }
  
  
  void angryBirdGame(){
//here the mapping to make the mouse able to be at the top of the bird on the slingshot
       th = map(t,0,20,30,15);
       fi = map(f,0,42,70,90);
       Mouse.move(th, fi, 0);
       int buttonState = digitalRead(mouseButton);
       if (buttonState == HIGH && prevButtonState == LOW) {
     // if the mouse is not pressed, press it:
     toggle++;
     if(toggle == 1)
     {
     if(t < 2 && 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;  
  }

I don't know anything about the leonardo but I notice in your setup function you use Mouse.begin(); I'm just wondering if Mouse.end() would detach it from your laptop port and allow your laptop touch pad to function again.

TBH I'm just shooting in the dark though.

Thanks, KenF. I am trying it right now and I will let you soon.

I just tried using Mouse.end() after Mouse.move(), KenF. It is no luck.

Anybody has new ideas. Thanks a lot.