Arduino Esplora Mouse

The Arduino Esplora is one of the newest Arduino Boards. It is shaped like a game controller, and has built in inputs and outputs. You can read more about it here. You can buy Arduino Esplora from here

After playing with my Esplora, I have decided to build a mouse. It has all the features of a real mouse except that you can easily change the cursor speed, it has a joystick, and it is all open source.

Using the mouse is simple:

To setup the Esplora you need to wait until the Led turns blue. Then without touching the joystick, press the top button. Touch the top button until the light turns green, and you are ready to go.

The Joystick moves the cursor
The top button enables/disables the mouse
The right button, bottom button and left button match up with the corresponding mouse buttons
the slider changes the cursor speed

The Led lights are as following:
Blue: awaiting calibration
Red: mouse is disabled
Green: mouse is enabled

Here is how the code works:

1
2
3
4
5
Esplora.writeRGB(0, 0, 10);                  
int start = Esplora.readButton(SWITCH_3);    
while(start == HIGH) {                       
  start = Esplora.readButton(SWITCH_3);
}

The Esplora turns sets the light blue to signal it is ready to go. It then sets an infinite loop until the top button is pressed.

JoyX = Esplora.readJoystickX();
JoyY = Esplora.readJoystickY();
Esplora.writeRGB(10, 0, 0);

The Esplora then sets the drift on the X and Y axis on the Joystick and sets the led to red to signal that is done.

1
2
3
4
int xValue = Esplora.readJoystickX();
int yValue = Esplora.readJoystickY();       
xValue = xValue - JoyX;                     
yValue = yValue - JoyY;                     

The Esplora reads the joystick values and removes the drift.

1
2
3
4
5
6
int JoyButton = Esplora.readJoystickButton();
 int button1 = Esplora.readButton(SWITCH_1);  
 int button2 = Esplora.readButton(SWITCH_2);  
 int button3 = Esplora.readButton(SWITCH_3);  
 int button4 = Esplora.readButton(SWITCH_4);  
 int slide = Esplora.readSlider();            

The Esplora then reads the buttons

1
2
3
int mousespeed = map(slide, 0, 1023, 10, 0);
int mouseX = map( xValue,-512, 512, mousespeed, -mousespeed); 
int mouseY = map( yValue,-512, 512, -mousespeed, mousespeed); 

The Esplora now maps out the max speed of the mouse, and maps out the value of the mouse movement

1
2
3
4
5
if(button3 == LOW){                       
    if(activate == 1) activate = 0;
    else activate = 1;
    delay(500);
  }

If the top button is pressed, the Esplora swaps the value of the variable

if(activate == 1){

If the previously set variable is equal to one, the Esplora runs the loop

1
2
Esplora.writeRGB(0 , 10, 0 );                
Mouse.begin();                               

The light is set to green and the Mouse is run

1
2
3
4
5
6
7
8
9
10
11
if(JoyButton == LOW || button2 == LOW) Mouse.press(MOUSE_LEFT);
    else Mouse.release(MOUSE_LEFT);
if(button1 == LOW) Mouse.press(MOUSE_MIDDLE);               
else Mouse.release(MOUSE_MIDDLE);
if(button4 == LOW)  Mouse.press(MOUSE_RIGHT);  
else Mouse.release(MOUSE_RIGHT);
Mouse.move(mouseX, mouseY, 0);

The Esplora presses and releases the buttons and moves the mouse using the previously set varialbes

1
2
3
4
else {
    Mouse.end();
    Esplora.writeRGB(255, 0, 0 ); 
  }

If the mouse is disabled, the Mouse connection is closed, and the led is set to red.

Here is entire code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
  Esplora Calibrated Joystick Mouse
 
 WARNING: this sketch will take over your mouse movement. If you lose control
 of your mouse do the following:
 1) unplug the Esplora.
 2) open the EsploraBlink sketch
 3) hold the reset button down while plugging your Esplora back in
 4) while holding reset, click "Upload"
 5) when you see the message "Done compiling", release the reset button.
 
 This will stop your Esplora from controlling your mouse while you upload a sketch
 that doesn't take control of the mouse.
 
 Made by Michael on 16 May 2013
 */
#include
int JoyX;                                        // These Variables are global
int JoyY;
int activate = 0;
void setup()
{
  Esplora.writeRGB(0, 0, 10);                   // Signal Esplora is waiting to calibrate
  int start = Esplora.readButton(SWITCH_3);      // Read First Button
  while(start == HIGH) {                         // Wait until button is pressed
    start = Esplora.readButton(SWITCH_3);
  }
  JoyX = Esplora.readJoystickX();                // Set drift variable X
  JoyY = Esplora.readJoystickY();                // Set drift variable Y
  Esplora.writeRGB(10, 0, 0);                   // Calibration is done, waiting to run
}
void loop()
{
  int xValue = Esplora.readJoystickX();          // read the joystick's X position
  int yValue = Esplora.readJoystickY();          // read the joystick's Y position
  xValue = xValue - JoyX;                        // Remove drift on X position
  yValue = yValue - JoyY;                        // Remove drift on Y position
  int JoyButton = Esplora.readJoystickButton();
  int button1 = Esplora.readButton(SWITCH_1);    // Read Buttons
  int button2 = Esplora.readButton(SWITCH_2);    // Read Buttons
  int button3 = Esplora.readButton(SWITCH_3);    // Read Buttons
  int button4 = Esplora.readButton(SWITCH_4);    // Read Buttons
  int slide = Esplora.readSlider();              // Read Slider Values
  int mousespeed = map(slide, 0, 1023, 10, 0);                  // Max Mouse Speed is Calculated from slider
  int mouseX = map( xValue,-512, 512, mousespeed, -mousespeed);  // map the X value to a range of movement for the mouse X, Changes input range to include new values
  int mouseY = map( yValue,-512, 512, -mousespeed, mousespeed);  // map the Y value to a range of movement for the mouse Y, Changes input range to include new values
  if(button3 == LOW){                          // Check if the Enable/Disable button is pressed
    if(activate == 1) activate = 0;
    else activate = 1;
    delay(500);
  }
  if(activate == 1){                               // Check if mouse is enabled
    Esplora.writeRGB(0 , 10, 0 );                 // Set light to green
    Mouse.begin();                                 // take control of the mouse
    if(JoyButton == LOW || button2 == LOW) Mouse.press(MOUSE_LEFT);       // Test if Left click is activated
    else Mouse.release(MOUSE_LEFT);
    if(button1 == LOW) Mouse.press(MOUSE_MIDDLE);                             // Middle Button   
    else Mouse.release(MOUSE_MIDDLE);
    if(button4 == LOW)  Mouse.press(MOUSE_RIGHT);      // Right Button
    else Mouse.release(MOUSE_RIGHT);
    Mouse.move(mouseX, mouseY, 0);                 // move the mouse
  }
  else {
    Mouse.end();
    Esplora.writeRGB(255, 0, 0 ); 
  }
  
  delay(10);                                  // a short delay before moving again
}

As you can see the code is quite ugly, but it works. I will work on making it smaller.

 

Using the mouse is simple. I uploaded some pictures I modified from the Arduino Guides to explain how to use the mouse. So when you plug in the mouse, the light will turn blue. This means that it is waiting to calibrate the joystick and get rid of any drift. don’t move the joystick and press the top button once. The light will turn red or green depending on how long you hold the button. A red light means the mouse is disabled. To enable the mouse, press the top button, and the light will turn green. When the mouse is enabled, the Joystick moves the mouse, the slider controls the speed of the mouse, and the bottom buttons match up with the buttons on the mouse.

Leave a Reply

Your email address will not be published. Required fields are marked *

*