Mohammed Adnan Khan
Published

Macro Mouse

Versatile handheld device merging keyboard and mouse functionality offering enhanced control. Plug and play device works via Bluetooth

BeginnerFull instructions providedOver 1 day253

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×2
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×2
TP4056
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Handheld part (sender)

USB (receiver)

Code

Sender Code

C/C++
Sends the packet in Json format (to avoid type conversions while transfer)
Each JSON packet has mouse movement and button info (pressed or released).
#include <ArduinoJson.h>
#include "Wire.h"
#include <MPU6050_light.h>
#include <SoftwareSerial.h>

SoftwareSerial BTserial(2, 3); 
MPU6050 mpu(Wire);
StaticJsonDocument<64> doc;


#define S0 4
#define S1 5
#define S2 6
#define S3 7

void setup()
{
  pinMode(S0, INPUT_PULLUP);
  pinMode(S1, INPUT_PULLUP);
  pinMode(S2, INPUT_PULLUP);
  pinMode(S3, INPUT_PULLUP);
  BTserial.begin(57600);
  Wire.begin();
  mpu.begin();
  delay(500);
}

void loop()
{
  String output;
  doc["S0"] = 0;
  doc["S1"] = 0;
  doc["S2"] = 0;
  doc["S3"] = 0;
  
  if (digitalRead(S0) == LOW)
    doc["S0"] = 1;

  if (digitalRead(S1) == LOW)
    doc["S1"] = 1;

   if (digitalRead(S2) == LOW)
    doc["S2"] = 1;

   if (digitalRead(S3) == LOW)
    doc["S3"] = 1;

  
  mpu.update();

  doc["x"] = mpu.getGyroX();
  doc["y"] = mpu.getGyroY();
  doc["z"] = mpu.getGyroZ();
  
  serializeJson(doc, output);
  BTserial.print(output);
  BTserial.write('\\');
}

ReceiverCode

C/C++
Receives the Json packet and then applies the value to corresponding function.
There is button pressed and button released function for all buttons.

The functions can be customized to fit the users needs
#include <ArduinoJson.h>
#include <Keyboard.h>
#include "Mouse.h"

StaticJsonDocument<96> doc;
String input = "";

int x = 0, y = 0, z = 0;
bool S0 = 0,S1 = 0,S2 = 0,S3 = 0;
bool f0 = 0, f1 = 0, f2 = 0, f3 = 0;

void setup() {
  Serial1.begin(57600);
  Mouse.begin();
  Keyboard.begin();
}




void loop() {
  while(Serial1.available()) {
    char inByte = Serial1.read();
    if(inByte!='\\')
     input = input + inByte;
    else{
      deserializeJson(doc, input);
      x = doc["x"]; 
      y = doc["y"]; 
      z = doc["z"]; 
      S0 = doc["S0"];
      S1 = doc["S1"];
      S2 = doc["S2"];
      S3 = doc["S3"];
  
      if(S0 && !f0)
          s0press();         
      else if(!S0 && f0)
        s0release();

      if(S1 && !f1)
          s1press();           
      else if(!S1 && f1)
        s1release();


      if(S2 && !f2)
          s2press();         
      else if(!S2 && f2)
        s2release();

      if(S3 && !f3)
          s3press();           
      else if(!S3 && f3)
        s3release();
        
      Mouse.move(-z/3,-y/5);
      input = "";
    }
  }
}




void s0press() {
  f0=1;
  Mouse.press(MOUSE_LEFT);
}


void s0release(){
  f0 = 0;
  Mouse.release(MOUSE_LEFT);
}

void s1press() {
  f1=1;
  Mouse.press(MOUSE_RIGHT);
}

void s1release(){
  f1 = 0;
  Mouse.release(MOUSE_RIGHT);
}

void s2press() {
  f2=1;
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_F4);
  Keyboard.release(KEY_LEFT_ALT);
  Keyboard.release(KEY_F4);
}

void s2release(){
  f2 = 0;
}

void s3press() {
  f3=1;
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_TAB);
}

void s3release(){
  f3 = 0;
  Keyboard.release(KEY_LEFT_ALT);
  Keyboard.release(KEY_TAB);
}

Credits

Mohammed Adnan Khan

Mohammed Adnan Khan

2 projects • 8 followers
Embedded systems engineer

Comments