Ra-02 LoRa receiver doesn´t handle negative or high numbers

Hello everybody, i´m trying to use a joystick along with transceiver/receiver and print the X and Y axis, the problem is, negative values are displayed as 256(max value) - the negative number


, but i need to display them as negatives, the other problem is when i try to put the joystick at max range it gets displayed as a 0 in the receiver, despite it being shown as 256 in the receiver terminal

this is the sender code

#include <SPI.h>
#include <LoRa.h> 
int VRx = A0;
int VRy = A1;

int xPosition = 0;
int yPosition = 0;

int mapX = 0;
int mapY = 0;
 
void setup() {
  Serial.begin(9600);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  
  while (!Serial);  
  Serial.println("LoRa Sender");
  if (!LoRa.begin(433E6)) { // or 915E6, the MHz speed of yout module
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}
 
void loop() {
  xPosition = analogRead(VRx);
  yPosition = analogRead(VRy);
 
  mapX = map(xPosition, 0, 1023, -256, 256);
  mapY = map(yPosition, 0, 1023, -256, 256);
  Serial.print("X: ");
  Serial.print(mapX);
  Serial.print(" | Y: ");
  Serial.println(mapY);
  delay(50);
  
  LoRa.beginPacket();    
  LoRa.write(mapX);
  LoRa.write(mapY);
  LoRa.endPacket();
  delay(50);
 
}

and this is the receiver

#include <SPI.h>
#include <LoRa.h>



int mapX;
int mapY;


void setup() {
  Serial.begin(9600);
  Serial.println("LoRa Receiver"); 
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  int packetSize = LoRa.parsePacket();
  if (packetSize == 0) return;
  mapX = LoRa.read();
  mapY = LoRa.read();

  
  
   Serial.print("X: ");
  Serial.println(mapX);
   Serial.print("X: ");
  Serial.println(mapY);
  Serial.println("X: " + String(mapX));
  Serial.println("Y: " + String(mapY));
  
  delay(1000);
}

btw i´m using arduino uno for sender and nano for receiver, don´t know if that matters

from: arduino-LoRa/LoRa.cpp at master · sandeepmistry/arduino-LoRa · GitHub

I haven't studied the library in detail but it does appear, at least on a cursory look, that the read and write methods are not symmetric.
Anyway, if you are writing then you probably have to write a byte (uint8_t), that is a value between 0 and 255.

size_t LoRaClass::write(uint8_t byte)

int LoRaClass::read()

If you want to work with negative numbers, it looks like you have to map these to the range 0 to 255 for transmission and un-map on receiving.

Have you got compiler warnings enabled in the IDE under preferences ?

the above writes a byte - ints are 2 or 4 bytes depending on host microcontroller
assuming you device is using two byte integers try

LoRa.write(&mapX,2);

similar with reading

alternativly transmit two bytes

LoRa.write(mapX>>8);
LoRa.write(mapX);

and read two bytes

  mapX = LoRa.read();
 mapX = mapX<<8 |  LoRa.read();

which Arduino are you using?

here I am using the lora.h library on Adafruit Feather 32u4 Lora boards to transmit and receive an int

#include <SPI.h>
#include <LoRa.h>

int counter = -1000;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.println("LoRa Sender");
  LoRa.setPins(8,4,7);   // for Lora 32u4
  if (!LoRa.begin(866E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  LoRa.write(counter & 0xff);
  LoRa.write((counter >> 8) & 0xff);
  LoRa.endPacket();
  counter+=100;
  delay(2000);
}

and the receiver

#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Receiver");
  LoRa.setPins(8,4,7);   // for Lora 32u4
  if (!LoRa.begin(866E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
    while (LoRa.available()) {
      int x = LoRa.read();
      x = (LoRa.read()<< 8) | x;
      Serial.print(x);
    }
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

when run the transmitter serial monitor displays

07:51:58.762 -> LoRa Sender
07:51:58.762 -> Sending packet: -1000
07:52:00.799 -> Sending packet: -900
07:52:02.810 -> Sending packet: -800
07:52:04.832 -> Sending packet: -700
07:52:06.849 -> Sending packet: -600
07:52:08.889 -> Sending packet: -500
07:52:10.906 -> Sending packet: -400
07:52:12.955 -> Sending packet: -300
07:52:14.967 -> Sending packet: -200
07:52:16.975 -> Sending packet: -100
07:52:19.022 -> Sending packet: 0
07:52:21.037 -> Sending packet: 100
07:52:23.052 -> Sending packet: 200
07:52:25.065 -> Sending packet: 300
07:52:27.111 -> Sending packet: 400
07:52:29.121 -> Sending packet: 500
07:52:31.167 -> Sending packet: 600
07:52:33.179 -> Sending packet: 700
07:52:35.190 -> Sending packet: 800
07:52:37.237 -> Sending packet: 900
07:52:39.252 -> Sending packet: 1000
07:52:41.261 -> Sending packet: 1100
07:52:43.309 -> Sending packet: 1200
07:52:45.324 -> Sending packet: 1300
07:52:47.333 -> Sending packet: 1400
07:52:49.376 -> Sending packet: 1500
07:52:51.390 -> Sending packet: 1600
07:52:53.398 -> Sending packet: 1700
07:52:55.445 -> Sending packet: 1800
07:52:57.457 -> Sending packet: 1900
07:52:59.467 -> Sending packet: 2000

and the receiver

07:44:53.844 -> LoRa Receiver
07:51:58.812 -> Received packet '-1000' with RSSI -71
07:52:00.827 -> Received packet '-900' with RSSI -71
07:52:02.840 -> Received packet '-800' with RSSI -71
07:52:04.886 -> Received packet '-700' with RSSI -71
07:52:06.898 -> Received packet '-600' with RSSI -66
07:52:08.908 -> Received packet '-500' with RSSI -66
07:52:10.926 -> Received packet '-400' with RSSI -71
07:52:12.973 -> Received packet '-300' with RSSI -66
07:52:14.978 -> Received packet '-200' with RSSI -71
07:52:16.997 -> Received packet '-100' with RSSI -71
07:52:19.037 -> Received packet '0' with RSSI -72
07:52:21.052 -> Received packet '100' with RSSI -71
07:52:23.096 -> Received packet '200' with RSSI -63
07:52:25.102 -> Received packet '300' with RSSI -71
07:52:27.122 -> Received packet '400' with RSSI -66
07:52:29.167 -> Received packet '500' with RSSI -71
07:52:31.176 -> Received packet '600' with RSSI -71
07:52:33.185 -> Received packet '700' with RSSI -71
07:52:35.234 -> Received packet '800' with RSSI -71
07:52:37.239 -> Received packet '900' with RSSI -71
07:52:39.285 -> Received packet '1000' with RSSI -70
07:52:41.295 -> Received packet '1100' with RSSI -71
07:52:43.308 -> Received packet '1200' with RSSI -71
07:52:45.331 -> Received packet '1300' with RSSI -71
07:52:47.377 -> Received packet '1400' with RSSI -71
07:52:49.391 -> Received packet '1500' with RSSI -71
07:52:51.412 -> Received packet '1600' with RSSI -65
07:52:53.454 -> Received packet '1700' with RSSI -71
07:52:55.473 -> Received packet '1800' with RSSI -71
07:52:57.489 -> Received packet '1900' with RSSI -71

code should work on Ra-02 Lora devices - just remove the statement specific to the 32u4

  LoRa.setPins(8,4,7);   // for Lora 32u4