Right click (JoystickMouseControl)

Good Morning,
My project is to build a low cost joystick that simulates a mouse for people with disabilities. I find many tutorials to build it, but in them only the left mouse click is functional. The right click is very important to manage screen sweeping programs using two push buttons, for people with very low range of joint movement. Even better would be to be able to equip the joystic with the use of the central button of the joystick.

Using a leonardo or micro arduino How should I modify the code that comes as an example (JoystickMouseControl)? I have tried with the following (Excuse me, the comments are in Spanish):

 */

#include "Mouse.h"

// configura los números de pin para el pulsador, los ejes del joystick y el LED:
const int switchPin = 2;      // activa y desactiva el control del ratón
const int mouseButton1 = 3;    // pin de entrada para el pulsador izquierdo del ratón
const int mouseButton2 = 6;    // pin de entrada para el pulsador derecho del ratón
const int mouseButton3 = 7;    // pin de entrada para el pulsador central del ratón
const int xAxis = A0;         // eje X del joystick
const int yAxis = A1;         //  eje Y  del joystick
const int ledPin = 5;         // LED de control del ratón

// parámetros para la lectura del joystick:
int range = 12;               // rango de salida del movimiento X o Y 
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range / 4;    // umbral de reposo
int center = range / 2;       // valor de posición de reposo

boolean mouseIsActive = false;    // si o no para controlar el ratón
int lastSwitchState = LOW;        // estado anterior del pulsador

void setup() {
  pinMode(switchPin, INPUT);       // pin del pulsador
  pinMode(ledPin, OUTPUT);         // pin del LED 
  // toma el control del ratón:
  Mouse.begin();
}

void loop() {
  // lee los pulsadores:
  int switchState = digitalRead(switchPin);
  //si ha cambiado y es HIGH, cambiar el estado del ratón:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // enciende el LED para indicar el estado del ratón:
      digitalWrite(ledPin, mouseIsActive);
    }
  }
  // almacena el estado del pulsador para la próxima comparación:
  lastSwitchState = switchState;

  // lee y escala los dos ejes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

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

  // lee el botón izquierdo del ratón y clic o no:
  //si el pulsador está activado:
  if (digitalRead(mouseButton1) == HIGH) {
    //si no está activado, pulsarlo:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  }
  // de lo contrario el botón izquierdo del ratón no está presionado:
  else {
    // si el pulsador está activado, soltarlo:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }
  // lee el botón derecho del ratón y clic o no:
  //si el pulsador está activado:
  if (digitalRead(mouseButton2) == HIGH) {
    //si no está activado, pulsarlo:
    if (!Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.press(MOUSE_RIGHT);
    }
  }
  // de lo contrario el botón derecho del ratón no está presionado:
  else {
    // si el pulsador está activado, soltarlo:
    if (Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.release(MOUSE_RIGHT);
    }
  }  // lee el botón central del ratón y clic o no:
  //si el pulsador está activado:
  if (digitalRead(mouseButton3) == HIGH) {
    //si no está activado, pulsarlo:
    if (!Mouse.isPressed(MOUSE_MIDDLE)) {
      Mouse.press(MOUSE_MIDDLE);
    }
  }
  // de lo contrario el botón central del ratón no está presionado:
  else {
    // si el pulsador está activado, soltarlo:
    if (Mouse.isPressed(MOUSE_MIDDLE)) {
      Mouse.release(MOUSE_MIDDLE);
    }
  }
  delay(responseDelay);
}

/*
  lee un eje (0 o 1 para x o y) y escala el
 rango de la entrada analógica a un rango de 0 s <range>
 */

int readAxis(int thisAxis) {
  // lee la entrada analógica:
  int reading = analogRead(thisAxis);

  // mapea la lectura de la gama de entrada analógica al rango de salida:
  reading = map(reading, 0, 1023, 0, range);

  // si la lectura está fuera de
  // umbral de la posición de reposo, utilizar esto:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // devuelve la distancia de este eje:
  return distance;
}
  if (digitalRead(mouseButton3) == HIGH) {
    //si no está activado, pulsarlo:
    if (!Mouse.isPressed(MOUSE_MIDDLE)) {
      Mouse.press(MOUSE_MIDDLE);
    }

It would make a lot more sense, to me, to name the pins mouseLeft, mouseRight, and mouseMiddle. I can’t figure out why, left to right, the numbers are 1, 3, 2.

Anyway, the code does something. It is not clear what it actually does. It is not clear how that differs from what you want.

How are your users able to use your switches, but not regular mouse buttons?

You want your Arduino to look like a mouse? That code looks like you want an Arduino to read a mouse.

If you want your Arduino to look like a mouse?
See Arduino - MouseKeyboard

And if you want to know how to send a mouse click with any of the three possible buttons:-

See Mouse.click() - Arduino Reference

From that page:-

Syntax

Mouse.click();
Mouse.click(button);
Parameters

button: which mouse button to press - char

MOUSE_LEFT (default)
MOUSE_RIGHT
MOUSE_MIDDLE

PaulS:
How are your users able to use your switches, but not regular mouse buttons?

Usually a switch designed to take advantage of the greater range of movement that the people with disabilities has is connected. A 2.5 mm audio jack is usually used with a cable connected to the device. (Excuse me if my English is not understood very well)

designed to take advantage of the greater range of movement that the people with disabilities has is connected.

Greater?

PaulS:
Greater?

Excuse me. mmmm... "The best range of movement of the person with disability"

You can find one example here: Tema 5 -Adaptación de los botones de un ratón - YouTube

The mouse joysticks are very expensive for poor families (around $ 350) and I would like to make one with Arduino Leonardo and 3D printing that does not exceed $ 15, to which I can connect buttons of the video (that part is easy).
Unfortunately I can not find the joystick code that simulates the three buttons and my knowledge is limited.

Thank you for your interest in the project

Unfortunately I can not find the joystick code that simulates the three buttons

Why are you ignoring reply #2?

Over the last year I have been working, along with some others, with a girl with cerebral palsy to make an interface suitable for use with Ableton live and we have made several interfaces that look like a mouse. We are on our second iteration to find out what works best for her.

The latest blog is here:-
https://thenashesizer.tumblr.com/

The mark 1 version can be seen here:-

The initial proposal is here:-
DM Lab North West Challenge The Nashesizer For Sound Artist Gemma Nash

All work is, or will be when finished, open source.

Excuse me for not having answered before,
I was trying to solve the problem with the ideas provided :slight_smile: but I've been very busy with other things
Here I have posted the code that I have made it work:

Thank you very much for your help

I might be working on a similar project, and I'm curious as to whether anyone has heard of something like an eye-blink sensor of some kind. I know such could be constructed utilizing the "Bell's phenomenon" (back to front bio-electrical current spikes that occur during eye closure) and some pretty sensitive EEG-related circuitry, but I'm wondering of some type of IR emittor and detector could be used (ie, measure reflectivity differences between those of an eyelid, or an exposed eyeball), but I'm not sure if IR can be damaging to any ocular bio-hardware.

The device I'm working on is inertial in nature, and my goal is the same as yours, produce it for less than what someone with little or no income is likely to be able to afford, while offering (at least) the same functionality of a typical mouse. I have been thinking of people with VERY limited motion, perhaps head only. The idea of the externally attached sensor appeals to me. Maybe even a bite-sensor for the mouth?

Any suggestions are appreciated.