Learning   Examples | Foundations | Hacking | Links

Mouse Continuous Drawing

Using the Mouse library, you can turn your Leonardo board into a device that is recognized from your computer as a mouse peripheral, with all the controls that a standard mouse has.

With this example you will be able to control with two potentiometers the mouse pointer and drawing lines on a processing application, like in the famous etch-a-sketch game.

This example according the other mouse examples uses a pushbutton to enable or disable the Leonardo mouse functionality. The on board LED, on digital pin 13, will turn on or off to indicate the activation state of the mouse.

The potentiometer has an absolute reference but the cursor movement is always relative. So every time the analog input is read, the cursor's position is updated relative to it's current position.

NB: When you use the Mouse.move() command, the Arduino takes over your computer's cursor! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a controller before you call Mouse.move(). This sketch includes a pushbutton to toggle the mouse control state, so you can turn on and off mouse control.

Hardware Required

  • Arduino Leonardo board
  • Two 10 kOhm potentiometer
  • Two momentary pushbutton
  • Two 10 kOhm resistors

Software Required

  • none

Circuit

Connect your Leonardo board to your computer with a micro-USB cable. The pushbutton that turns on and off the mouse is connected to pin D2, the pushbutton that behaves as left-click is connected to pin D3. The two potentiometer that correspods to the X and Y axes are connected respectively on the analog inputs A0 and A1.

click the images to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Arduino Code


/*
 MouseContinousDrawing

 Controls the mouse from two potentiometer on an Arduino Leonardo.
 Uses a pushbutton to turn on and off mouse control, and
 a second pushbutton to click the left mouse button

 Hardware:
 * 2 potentiometers connected to pins A0 and A1
 * pushbuttons connected to pin D2 and D3

 The mouse movement is always relative. This sketch reads
 two analog inputs that range from 0 to 1023 (or less on either end)
 and translates them into an offset compared to the previous position.

 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the command.
 This sketch includes a pushbutton to toggle the mouse control state, so
 you can turn on and off mouse control.

 based on the original Leonardo mouse examples made by Tom Igoe

 this code is in the public domain

*/


// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis  
const int yAxis = A1;         // joystick Y axis
const int ledPin = 13;         // Mouse control LED

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value
int prevxVal = 0;
int prevyVal = 0;

int prevXreading = 0;
int prevYreading = 0;
int toggle = 0;

boolean mouseIsActive = false;    // whether or not to control the mouse
int prevSwitchState = LOW;        // previous switch state
int prevButtonState = LOW;

void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);         // the LED pin  
  // take control of the mouse:
  Mouse.begin();
}

void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != prevSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    }
  }

  // save switch state for next comparison:
  prevSwitchState = switchState;

  // read and scale the two axes:
  // int xReading = readAxis(A0,  xVal);
  // int yReading = readAxis(A1, lastyVal);
  int currXreading = analogRead(xAxis);
  int currYreading = analogRead(yAxis);

  int xReading = prevXreading - currXreading;
  int yReading = prevYreading - currYreading;

  prevXreading = currXreading;
  prevYreading = currYreading;

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0);
  }  

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  int buttonState = digitalRead(mouseButton);
  if (buttonState == HIGH && prevButtonState == LOW) {
    // if the mouse is not pressed, press it:
    toggle++;
    if(toggle == 1)
      if (!Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.press(MOUSE_LEFT);
      }  
  }
  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;

  delay(responseDelay);
}
 

The processing application allow you to draw on the grey screen by pressing the mouse left button and moving the cursor. To let you free to drawing only by moving the two potentiometers, when you press the pushbutton connected to the D3 input, the Leonardo will hold the left mouse click for you until you press the pushbutton another time.

Processing Code


/**
 * Continuous Lines.
 *
 * Click and drag the mouse to draw a line.
 */


void setup() {
  size(640, 480);
  background(102);
}

void draw() {
  stroke(255);
  strokeWeight(10);
  if(mousePressed) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}
 

click the images to enlarge

See Also

Share