Skip to main content

Leonardo Experiments: Control Your Mouse Using Keyboard

HELLO THERE!!!!

  How are you guys? I hope everyone is fine and good always. This week, I will share with you guys an experiment that is made using Arduino Leonardo. I had just bought Arduino Leonardo and tried tinkering with it. The objective of this experiment is to control the mouse movement  using keyboard output via Leonardo. Well, lets see what hardware and software needed for this experiment:


Software:
1. Arduino IDE
2. Processing IDE

Hardware:
1. Arduino Leonardo




There are two parts of programming in this experiment, one in the processing IDE, one in the Arduino IDE. Now the code for the Processing IDE:

import processing.serial.*;// so that we can connect to arduino

Serial port;
void setup()
{
  size(200,200);//size of the window created
port = new Serial(this, "COM16", 9600);//declaring our port that arduino uses
}

void draw()
{
}

void keyPressed()//this part shows the program for the keyboard key to be pressed
{
  if(key == 'w'){//example when key "w" is pressed
    port.write('w');//the processing IDE sends data "w" to arduino
  }
   if(key == 'a'){
    port.write('a');
  }
  if(key == 's'){
    port.write('s');
  }
   if(key == 'd'){
    port.write('d');
  }
}

Now for the Arduino code:



void setup() { // initialize the buttons' inputs
  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
  
}

void loop() {
  // use serial input to control the mouse:
  if (Serial.available() > 0) {  //if serial available
    char inChar = Serial.read(); //read the serial input

    switch (inChar) {
      case 'w':
        // move mouse up
        Mouse.move(0, -40);//you can change the value
        break;
      case 's':
        // move mouse down
        Mouse.move(0, 40);
        break;
      case 'a':
        // move mouse left
        Mouse.move(-40, 0);
        break;
      case 'd':
        // move mouse right
        Mouse.move(40, 0);
        break;
     
    }
  }
}


Upload the Arduino code into the Leonardo and then run the Processing code. Now, press the keyboard buttons to test the experiment. The experiment video is as shown below:










Comments