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:
- Button 1: Draw a line from top to bottom based on the current mouse position when
Mouse.IsPressed()- Button 2: Draw a line from left to right based on the current mouse position when
Mouse.IsPressed()- Button 3: Draw a circle based on the current mouse position when
Mouse.IsPressed()- 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!