Reading joystick mouse parameters from PC

I have made a USB joystick mouse for a disabled person who is unable to use any commercially-available products. I'm using an Arduino Micro with the sketch based on the 'JoystickMouseControl' example in the Arduino IDE. I am able to edit the sketch to adjust parameters such as the slowest speed and maximum speed. I am considering also providing acceleration so the cursor speed gradually increases while the joystick is held fully over. There's also the possibility of switching between two modes: a relatively high speed mode for web browsing and a lower speed mode for precision text editing.

I would like the user to be able to try various settings of the parameters to make use of the joystick as speedy as possible, given her degree of disability, without compromising positional accuracy. Obviously one way would be to download the Arduino IDE to her PC so she can edit the initialisation of variables in the sketch, save the sketch and upload it to the Arduino Micro.

I would prefer if the Arduino could read the parameters from a file when the PC is switched on (and after any reset of the joystick mouse). Perhaps it could be a simple text (.txt) file such as:

Min=1
Max=8
Acceleration=4

The user would then use Microsoft Notepad to edit the file rather than using the Arduino IDE to edit the sketch. I intend to provide an on/off button, as in the 'JoystickMouseControl' sketch, so the user would need to operate that to download any edited parameters. I could program the sketch to check for valid parameters and perhaps have an LED on the mouse to indicate any failure to read the parameters.

How would you get the Arduino (connected via USB) to read a PC file?

I would prefer if the Arduino could read the parameters from a file when the PC is switched on

How would the Arduino know that that had happened?

You would need an application on the PC that ran when the PC booted up, read from the file, and pushed the data to the Arduino. It can't read a file on the PC, even if it knew when to do so.

It is not so easy for the Arduino to read a file on the PC but it is more easy to send the content of a file from the PC to the Arduino via serial (=USB) connection. What you need is some kind of "terminal program" that opens the "config file" (best: a very simple text file) and send it to the Arduino. This could be done automaticly on PC startup or manually whenever needed. The terminal software depends on the operating system (Win / Mac / Linux / ....). The arduino has to read the information from serial connection and set the parameters. It could also store the settings in EEPROM, so the information will be set even after reset.

PaulS:
How would the Arduino know that that had happened?

Thanks Paul. The Arduino is powered from a USB port so I assume the sketch is executed as soon as the PC provides power to the port.

uxomm:
It is not so easy for the Arduino to read a file on the PC but it is more easy to send the content of a file from the PC to the Arduino via serial (=USB) connection. What you need is some kind of "terminal program" that opens the "config file" (best: a very simple text file) and send it to the Arduino. This could be done automaticly on PC startup or manually whenever needed. The terminal software depends on the operating system (Win / Mac / Linux / ....). The arduino has to read the information from serial connection and set the parameters. It could also store the settings in EEPROM, so the information will be set even after reset.

Thanks.

I am wondering whether I should be considering using Python (on Windows 7) to provide a user interface. The 'JoystickMouseControl' sketch already provides for a switch to stop the Arduino working as a mouse. I guess that would enable the user to download new parameters from the PC before switching back to behaving as a mouse again.

Another approach I am considering is to allow the parameters to be adjusted without involving the PC: at its simplest the Arduino could read switch settings and/or potentiometer settings but they would have to be adjusted by a carer. Perhaps better would be to allow the joystick to change the parameters when the mouse stop switch has been pressed but that would entail some form of display of parameters.

I was working with disabled persons in the past (many years ago) and I would say it depends very much on the needs and abilities of the person(s) involved.

To provide some kind of user interface might be a good idea if the persons involved can/will handle it AND if you can program it. I am not very experienced in Python, so I can not tell exactly, but it should provide some kind of serial transmission (via USB) from and to the arduino. I think Python can do this.

If you want to go with this option I would do it somehow like this:
User launches user interface
Readout parameters (data from arduino to PC user interface) and display in user interface
Chage parameters with user interface
Write parameters to arduino (data from PC to arduino)
Arduino write new parameters to EEPROM (and after next reset or power up: read parameters from EEPROM).

I am not sure if you need the switch for parameter change. You could also change the parameters while running. The arduino has just to "listen" to the serial interface and if new parameters (or other commands) are comming do the right things.

I think this should be doable.

uxomm:
I think Python can do this.

Thanks again.

I would use my Microsoft C++ software to do this but it doesn't work on Window 7. Trust Microsoft!

I will have a look at using Python; it sounds hopeful.

The user would need to use the joystick mouse to use the Python user interface. I am fairly sure the Arduino cannot send or receive ordinary serial USB data while it is in mouse mode. But that should not be a problem: the user will be able to switch it out of mouse mode and (hopefully) it can send a request for any updated parameters to the Python software just before entering mouse mode again.

I am fairly sure the Arduino cannot send or receive ordinary serial USB data while it is in mouse mode.

I do not think so. Take a look at http://www.arduino.cc/en/Main/ArduinoBoardMicro:

The Micro is similar to the Arduino Leonardo in that the ATmega32u4 has built-in USB communication, eliminating the need for a secondary processor. This allows the Micro to appear to a connected computer as a mouse and keyboard, in addition to a virtual (CDC) serial / COM port.

I wrote a very rudimentary test sketch just to proof the concept:

/*
    Mouse and serial test for Arduino Leonardo / Micro
    
    Rudimentary mouse control: pushbuttons on D11 and D12 
    to move the mouse left and right
    
    If 'x' is sent to Arduino via serial, Arduino will respond
    "Hallo, this is Arduino speaking".
*/

int inByte;
int moveVal = -1;

void setup()  {
    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect, needed for Leonardo/Micro only
    }
    Mouse.begin(); 
}

void loop()  {
    if (digitalRead(11) == LOW)        
    {    Mouse.move(moveVal, 0, 0);        // move mouse left
    }
    
    if (digitalRead(12) == LOW)
    {    Mouse.move(abs(moveVal), 0, 0);   // move mouse right
    }
    
    if (Serial.available())
    {    inByte = Serial.read();
         if (inByte == 120)          // ASCII 120 = 'x'
         {    Serial.println("Hallo, this is Arduino speaking");
         }
    }   
}

I am very sure, your joystick sketch is much more complex, but it is no problem for the Leonardo/Micro to "be" a mouse and to do serial communication at the same time.

If you want to use the switch, no problem either, but it is not a must.

uxomm:
I wrote a very rudimentary test sketch just to proof the concept:

Very many thanks for taking the time to write the sketch.

When I merge your sketch with my sketch it gets stuck in the while(!Serial) loop. But if I comment out the loop your serial communication works at the same time as the mouse functionality. That will make communication with Python easier than I thought.

I've found that I can comment out Serial.begin(9600) and it still works.

I can change the baud rate of the Arduino IDE's serial monitor and it still works, with or without Serial.begin(9600) present.

I can comment out Mouse.begin() and the mouse functionality still works.

I will see what else I can delete!

I think the while(!Serial) loop is some kind of "workaround" (from the official Arduino reference page) because with Leonardo and Micro it may take some seconds before the USB connections (keyboard, mouse, serial) start. I think it will also work without it.
That you can comment out also Serial.begin and Mouse.begin is very interesting :slight_smile:

The adjustable parameters you can store in EEPROM of the Arduino Micro. Reading and writing to EEPROM is special, there is a library for it, it is easy to use and reliable (http://www.arduino.cc/en/Reference/EEPROM).
On startup, the Micro should read the parameters from EEPROM and use them. For easiest handling one byte for each parameter is the best. I do not know how many parameters you want to store but let's say there are 10, so there are 10 bytes of parameters im EEPROM (by the way: there is 1KB EEPROM on the Micro).

For the communication Arduino - PC (Python) I would use some kind of protocol like this:
After launch of user interface, it opens a serial connection to Arduino and sends - let's say "g" for "give me the parameters".
Arduino does see the "g" and knows what to do: send the 10 parameter bytes to PC. First byte is parameter one, second is parameter two and so on.
User Interface was waiting for 10 bytes and now they have arrived. It is easy to know which byte is which parameter (because they are 10 and in the right order).
User interface displays all parameters.
Now the user can change the parameters (maybe by clicking big plus or minus buttons).
Once the user wants to store the new parameters he klicks the "Save button" on user interface.
And user interface sends to arduino via serial - let's say "u" for "update parameters" and after that all 10 bytes of the parameters (some may have changed, some not).
Arduino knows (because of the "u" prefix) that these are new parameters, stores them in EEPROM and uses them from now on.

uxomm:
The adjustable parameters you can store in EEPROM of the Arduino Micro. . . . . .

Thanks for the tip about using EEPROM.

I like the idea of the Python software reading the current joystick mouse parameters. I could allow sets of parameters to be saved in PC files. (assuming Python can do that).

Speech recognition software is being used for browsing so it is expected the joystick mouse will be used mostly for text editing which is very difficult using speech recognition. I would try to ensure the Python software works with speech recognition.