Switching case with buttons and use Mouse.move() to draw lines based on cases

Hello everyone,

I am working on a mini project that draws line on Mircosoft Paint using a mouse, here is my setup:

  • Arduino Leonardo
  • USB host shield
  • A 2x2 push button
  • My mouse (connected to the USB host shield to the PC)

My goal for this project:

  1. Button 1: Draw a line from top to bottom based on the current mouse position when Mouse.IsPressed()
  2. Button 2: Draw a line from left to right based on the current mouse position when Mouse.IsPressed()
  3. Button 3: Draw a circle based on the current mouse position when Mouse.IsPressed()
  4. Button 4: Clears out everything so when Mouse.IsPressed() nothing will happen

The main obstacles right now, Mouse.move() will teleport my mouse cursor to the destination, I want it to perform in a more human-like way, is there a way to smooth out the transition of Mouse.move() from point A to point B?

Moreover, I am also struggling to draw a circle with smooth curve line...

Lastly, will there be a better/cleaner way to switch the case using the buttons?

Here is my code:

#include <Mouse.h>

#define STATE_verticalLine 0
#define STATE_horizontalLine 1
#define STATE_circleLine 2
#define STATE_neutral 3

const int firstButton = 2;
const int secondButton = 3;
const int thirdButton = 4;
const int fourthButton = 5;

int state = STATE_neutral;
int next_state = STATE_neutral;

void setup() {
  pinMode(firstButton, INPUT);
  pinMode(secondButton, INPUT);
  pinMode(thirdButton, INPUT);
  pinMode(fourthButton, INPUT);
  Mouse.begin();
}

//Draw a line from top to bottom based on the current mouse position when pressing the left click.
void verticalLine() {
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 3);
    delay(100); 
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 3);
    delay(100); 
  }
}

//Draw a line from left to right based on the current mouse position when pressing the left click.
void horizontalLine() {
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(3, 0);
    delay(100); 
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(3, 0);
    delay(100); 
  }
}

//Draw a circle based on the current mouse position when pressing the left click.
void circleLine() {
  if (Mouse.isPressed(MOUSE_LEFT)) {
    //Still trying to figure out how to draw a circle with smooth curve line.
  }
}

//Clears out everything so nothing will happen when left clicking.
void neutral() {
  if (Mouse.isPressed(MOUSE_LEFT)) {
    delay(1000);
  }
}

void stateSelect() {
  if (firstButton == HIGH) {
    next_state = STATE_verticalLine;
  }
  if (secondButton == HIGH) {
    next_state = STATE_horizontalLine;
  }
  if (thirdbutton == HIGH) {
    next_state = STATE_circleLine;
  }
  if (fourthButton == HIGH) {
    next_state = STATE_neutral;
  }

  state = next_state;

  switch (state) {
    case STATE_verticalLine:
      verticalLine();
      break;
    case STATE_horizontalLine:
      horizontalLine();
      break;
    case STATE_circleLine:
      circleLine();
      break;
    case STATE_neutral:
      neutral();
      break;
    default:
      neutral();
}

Any help is appreciated! Have a good one!

Welcome to the forum

Break the move into several smaller steps and introduce a delay between each smaller movement

So the best approach would be like this?

  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  }
  if (Mouse.isPressed(MOUSE_LEFT)) {
    Mouse.move(0, 1.5);
    delay(50);
  }

No

Detect the mouse press once then make a series of small mouse moves with a delay between each of them. A for loop would be useful here

Incidentally, the move() function takes integers as its parameters, not floats

Thank you for the reply, the result would be something like this?

void verticalLine() {
 if (Mouse.IsPressed(MOUSE_LEFT)) {
  for (int i = 0; i <= 8; i++) {
     Mouse.move(0, 1);
     delay(33);
  }
 }
}

Yes, something like that. You might like to pass parameters to the function to define things such as the number of steps or the delay

  • Will there be a better/cleaner way to switch the case using the buttons?
  • Any way to draw a circle with smooth curves?