Introduction: Arduino Thumbstick Controlling Computer Cursor

This is a test project i made to improve my programming with c++/c# and processing java language.

This project is about controlling your computer cursor with the joystick that connected to Arduino.

Although it's not fully complete yet but it will still work as i wanted.

Step 1: Prepare

What you will need for this project is :

  1. Any kind of Arduino ( i am currently using Nano )
  2. Thumbstick ( i ripped it off from my old PS1 controller )
  3. Few jumper wires
  4. Basic programming knowledge

You will also need Visual Studio and Processing for this project.

Step 2: Make Your Joystick

I follow the adafruit's joystick copper line to make my own on the perf board.

This is optional if you already have Adafruit's Joystick.

Step 3: Connection

This is the connection i made using microsoft paint.

Connect Y to A0 on Arduino pin

Connect X to A1 on Arduino pin.

Step 4: Programming Arduino

#define xPin A1
#define yPin = A0

#define buttonPin 12

int xPosition;
int yPosition;
int button;

void setup() {
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
buttonState();
readX();
readY();
Serial.print(xPosition);
Serial.print(",");
Serial.print(yPosition);
Serial.print(",");
Serial.println(button);
// example value 500,500,0; -- x,y,button -- data[0],data[1],data[2]
//delay(100); // to see serial value properly
}

void buttonState() {
button = digitalRead(buttonPin);
if (button == HIGH) {
digitalWrite(13, HIGH); .// indicating button is pressed.
}else {
digitalWrite(13, LOW);
}
}
void readX() {
xPosition = analogRead(xPin);
xPosition = map(xPosition, 1023, 0 , 0, 1920); // prevent invert axis.
}

void readY() {
yPosition = analogRead(yPin);
yPosition = map(yPosition, 0, 1023, 0, 1080);
}

Step 5: Programming Processing for Testing

import processing.serial.*;
Serial port;
String serial;
PFont f; 
String x,y;
float xPos, yPos;
void setup() {
  //background(222);
  size(1920,1080);
  f = createFont("Arial",16,true);
  port = new Serial(this, "COM3", 9600);
  port.clear();
  serial = port.readStringUntil('\n');
  serial = null;
}
void draw() {
  background(222);
  textFont(f,20);                
  fill(255, 0, 0); 
  text("Developed by FirmanJamal",10,23);
  text(xPos, 920, 23);
  text(yPos, 920, 43);
  while(port.available() > 0) {
      serial = port.readStringUntil('\n');
  }  
  if (serial != null) {
       String[]a = split(serial, ',');
       x = a[0];
       y = a[1]; 
       //println(x);
       println(a[0]);
       println(a[1]);
       xPos = float(x);
       yPos = float(y);
       cursor();
  }
}
void cursor() {
  fill(0,0,0);
  ellipse(xPos,yPos,100,100);
  
}

Step 6: C# Moving Cursor

using System;<br>using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;
using System;
using System.Runtime.InteropServices;
namespace Serial_Mouse_Thumbstick
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Developed by Firman Jamal";
            SerialPort port = new SerialPort();
            port.PortName = "COM3";
            port.BaudRate = 9600;
            port.Open();
            int x, y, button;
            do
            {
                string message = port.ReadLine();
                string[] newData = message.Split(',');
                //Console.WriteLine(newData[2]);
                //Console.WriteLine("X COORDINATE : " + newData[0] + " Y COORDINATE : " +newData[1]+" BUTTON : " +newData[2]);
                x = Convert.ToInt32(newData[0]);
                y = Convert.ToInt32(newData[1]);
                
                button = Convert.ToInt32(newData[2]);
                if (button == 1)<br>                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                }
                else
                {
                    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                }
                Cursor.Position = new System.Drawing.Point(x, y);
                Console.WriteLine("X: " + Cursor.Position.X + " Y: " + Cursor.Position.Y + " Button : " + button);
                
            } while (true);
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
    }
}

Be sure to add System.Drawing at the references.

Step 7: Final Test

Congratulation if your test works perfectly as mine, feel free to upgrade the codes!