You are misunderstanding the type char
. It is named character, because in ASCII representation you can use it to save one character. But internally it just is a number. The Arduino doesn't care, if you save characters in it or numbers, because characters are also numbers for it.
A signed char
, like the move()
function of the library uses` has 1 byte and is signed, thus can hold numbers from -128 to 127.
While the documentation is not clear about what the x and y parameters really represent the answer to this question is clear about it. Basically the whole range from -128 to +127 represents a whole inch (or what the computer thinks is an inch). So this results in a resolution of 1" / (127 - -128) = 1" / 255 = 0.0039"
(or about 0.1mm). I don't know, what values you are getting from the sensor, but you will have to scale it to somewhere in the -128 to 127 range (depends on how sensitive you want it to be). And you won't loose precision here. The sensor values most likely are more precise, than the range of the mouse move()
function.
The calculation will then be as simple as
signed char mouse_x = sensor_x * factor + offset;
The result of the calculation will implicitly converted to a 1 byte signed integer (this is what signed char
is). If you want to make it clearer what happens here, you can also write:
signed char mouse_x = (signed char)(sensor_x * factor + offset);
This is not needed, but you might wanna do it for clarity.
Seen in Edgar Bonets answer: You might wanna contrain the value of above calculation to the -128 to 127 range. If you get an unexpected high value from your sensor, the above calculation would overflow the mouse_x
variable (not good for correct values). So capping the values to that range with the constrain()
function is definitely good. Refer to Edgars answer and the documentation for this.