Arduino Esplora Mouse

4,548

87

10

Introduction: Arduino Esplora Mouse

About: Building and coding is what I do.

Hello everyone, This is K'NEXnCoding bringing you another cool tutorial for you guys. This project lets you use your Arduino Esplora board to completely act like a mouse. It can move, left click, right click, and even scroll. The best part is that you don't even need to do anything.

However, it is a good idea to read to learn how to use this project, even though it is pretty straight forward.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: How to Move the Mouse

To move the mouse, just use the joystick. Move it up, down, left, right... You know, like normal. Also, you can press down on the joystick to left click.

Step 2: Linear Potentiometer

Use the linear potentiometer to control the sensitivity of the mouse movement.

Step 3: Left and Right Click

Again, straightforward. The left button left-clicks, and the right button right-clicks.

Step 4: Scrolling

To scroll, use the top and bottom buttons.

Step 5: The Code

Here is the code. Just copy and paste and your good to go!

Note: You can display the coordinates the mouse is at using the serial monitor, but sometimes it will cause Arduino to crash.

#include <Esplora.h>
#include <Mouse.h>
void setup()
{
  Mouse.begin();
}
int oldScrollUp = 0;
int oldScrollDown = 0;
int stateTime = 0;
#define THRESHOLD 20
void loop()
{
  int xValue = Esplora.readJoystickX();
  int yValue = Esplora.readJoystickY();
  
  Serial.print("Joystick X: ");                // print a label for the X value
  Serial.print(xValue);                        // print the X value
  Serial.print("\tY: ");                       // print a tab character and a label for the Y value
  Serial.print(yValue);                        // print the Y value
  
  int leftButton = Esplora.readJoystickSwitch() && Esplora.readButton(SWITCH_LEFT);
  int rightButton = Esplora.readButton(SWITCH_RIGHT);
  
  int scrollUp = Esplora.readButton(SWITCH_UP);
  int scrollDown = Esplora.readButton(SWITCH_DOWN);
  
  int range = Esplora.readSlider();
  
  int mouseRange = map(range, 0, 1023, 2, 10);
  int mouseX = (xValue < THRESHOLD && xValue > -THRESHOLD) ? 0 : map(xValue, -512, 512, mouseRange, -mouseRange);
  int mouseY = (yValue < THRESHOLD && yValue > -THRESHOLD) ? 0 : map(yValue, -512, 512, -mouseRange, mouseRange);
  int wheel = 0;
  if (oldScrollUp != scrollUp || oldScrollDown != scrollDown)
  {
    wheel = (scrollUp != scrollDown) ? (scrollUp ? -mouseRange : mouseRange) : 0;
    oldScrollUp = scrollUp;
    oldScrollDown = scrollDown;
    stateTime = millis();
  }
  else if ((millis() - stateTime) % 100 == 0 && (millis() - stateTime) >= 1000)
  {
    wheel = (scrollUp != scrollDown) ? (scrollUp ? -mouseRange : mouseRange) : 0;
  }
  Mouse.move(mouseX, mouseY, wheel);
  
  if (!leftButton)
    Mouse.press(MOUSE_LEFT);
  else
    Mouse.release(MOUSE_LEFT);
  
  if (!rightButton)
    Mouse.press(MOUSE_RIGHT);
  else
    Mouse.release(MOUSE_RIGHT);
}

Let me know if you tried it! Comment below.

Buy an Arduino Esplora

Other cool Esplora Projects

Coded Creations

Participated in the
Coded Creations

Be the First to Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020

    10 Discussions

    0
    Mr AbAk
    Mr AbAk

    5 years ago

    Awesome mouse

    0
    tomatoskins
    tomatoskins

    5 years ago on Introduction

    Such a cool idea! I love making my own periphials if at all possible!

    0
    Nat992nbd
    Nat992nbd

    5 years ago on Introduction

    would there be a way to make the espolra do rapid fire click

    0
    KNEXnCoding
    KNEXnCoding

    Reply 5 years ago on Introduction

    I'm sure there is, somehow. I don't entirely know how.

    0
    Proto G
    Proto G

    Reply 5 years ago on Introduction

    Yes you can using a while loop. It will rapid fire click as long as the button is held. You can adjust the delay to change the frequency.

    while(!leftButton){

    Mouse.press(MOUSE_LEFT);

    delay(100);

    Mouse.release(MOUSE_LEFT);

    delay(100);

    }

    0
    Proto G
    Proto G

    Reply 5 years ago on Introduction

    Yes you can using a while loop. It will rapid fire click as long as the button is held. You can adjust the delay to change the frequency.

    while(!leftButton){

    Mouse.press(MOUSE_LEFT);

    delay(100);

    Mouse.release(MOUSE_LEFT);

    delay(100);

    }

    0
    Proto G
    Proto G

    5 years ago on Introduction

    The Arduino Leonardo can also be used as an HID keyboard and mouse.

    0
    KNEXnCoding
    KNEXnCoding

    Reply 5 years ago on Introduction

    Yeah, unfortunately I don't have the Leonardo yet.