Arduino Receiving negative values from serial

Hi everyone, Im trying to move my mouse with arduino leonardo with python code. As you know there is X and Y axis for mouse movements. Python and Arduino communicates with serial band. which is “Serial.readBytes();” There is a question on me. i can send the values which is like x = 5 y = 5 because those are positive values. But when i try to send negative values like x = -5 etc. it fails. i’ve got some value error like “ValueError: byte must be in range(0, 256)” arduino accepts only 0,256 value so how can i receive negative values ?

here is my arduino code.

#include <Mouse.h>

byte bf[2];

void setup() {
  Serial.begin(9600);
  Mouse.begin();
}

void loop() {
  if (Serial.available() > 0) {
    Serial.readBytes(bf, 2);
    Mouse.move(bf[0], bf[1], 0);
  }
}

and little python :

arduino = serial.Serial('COM3', 9600)
pax2 = [10,-2]
arduino.write(pax2)

According to Mouse.move() - Arduino Reference the Mouse.move() method takes the X and Y parameters are two signed integers. You are wrongly supplying just two bytes/chars. Assuming we are talking of the same library as you, you need to read two strings from the input and convert them to signed integer values.

Your first job is to define the format of the input. I would use to integers as decimal strings and separate them with a comma. For example, one posssiblility, something like “12,-5”. Then split the input into the two separate strings and use atoi() to convert them to signed integers.

skyvan:
According to Mouse.move() - Arduino Reference the Mouse.move() method takes the X and Y parameters are two signed integers. You are wrongly supplying just two bytes/chars. Assuming we are talking of the same library as you, you need to read two strings from the input and convert them to signed integer values.

Your first job is to define the format of the input. I would use to integers as decimal strings and separate them with a comma. For example, one posssiblility, something like "12,-5". Then split the input into the two separate strings and use atoi() to convert them to signed integers.

First of all thank your for commant. I'ts hard to get it for me , I need to make a converter which is converting my int values to signed ints then i'll convert them to byte and use them like Mouse.move(x,y)
x = converted signed int, converted to byte, and y is same do i correct ? if not can you explain more simple please :frowning:

If you want to use your bytes as signed values, cast then to 'char':

    Mouse.move((char) bf[0], (char) bf[1], 0);

That should work fro values from -128 to 127.

Note: You should probably wait for both characters to arrive before reading them from the buffer:

  if (Serial.available() >= 2) {

johnwasser:
If you want to use your bytes as signed values, cast then to 'char':

    Mouse.move((char) bf[0], (char) bf[1], 0);

That should work fro values from -128 to 127.

Note: You should probably wait for both characters to arrive before reading them from the buffer:

  if (Serial.available() >= 2) {

Thanks for comment, I've change my code with yours looks my problem starts from Python. I've still same problem on that side. :frowning:

phantomninja:
i've got some value error like "ValueError: byte must be in range(0, 256)"

Where are you seeing this error?

Perehama:
Where are you seeing this error?

on python's console

phantomninja:
on python's console

Python code:

arduino.write(chr(-2))

Should send "-2".

@phantomninja
Your duplicate post in another forum section has been deleted

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.