top and bottom of variable reversed

I have a variable doing something I can’t explain and it’s a bit anoying ;D

I’ve got a joystick (2 pots) hooked up to one arduino, which has a cheap transmitter attached, transmitting the X and Y values.
Another arduino with receiver attached is displaying the received value, this is where things get a bit strange…

The transmitter arduino has both values going from -128 to 127 however the reciever has the value going from 128 to 127, explained better in hex, 80 to 7F, better still is this image -

needless to say, this is a bit strange…

I’m still in the happy hacking stage of playing with VirtualWire, which is why the code is a bit messy, but it’s not too hard to follow

radio_rx -

#include <VirtualWire.h>

#define xAxisPIN 4
#define yAxisPIN 5

const char *msg = "Test";

char str[30];

char xAxis = -1;
char yAxis = -4;
char oldXaxis = -1;
char oldYaxis = -4;

void setup()
{
  Serial.begin(9600);        // Debugging only
  Serial.println("setup");

  // Initialise the IO and ISR
  vw_set_tx_pin(3);
  vw_setup(2000);       // Bits per sec
}


void loop()
{
  doJoystick();
}

void doJoystick(){
  getXaxis();
  getYaxis();


  if(xAxis != oldXaxis || yAxis != oldYaxis){
    str[0] = xAxis;
    Serial.println(analogRead(xAxisPIN));
    Serial.println(str[0], DEC);

    str[1] = yAxis;
    Serial.println(analogRead(yAxisPIN));
    Serial.println(str[1], DEC);

    Serial.println();
    
    sendData();
  }

}

void getXaxis(){
  int axisBuff = 0;
  oldXaxis = xAxis;
  axisBuff = analogRead(xAxisPIN);
  delay(1);
  axisBuff += analogRead(xAxisPIN);
  delay(1);
  axisBuff += analogRead(xAxisPIN);
  delay(1);
  axisBuff += analogRead(xAxisPIN);
  delay(1);
  axisBuff += analogRead(xAxisPIN);
  xAxis = axisBuff/5/4-128;
}

void getYaxis(){
  int axisBuff = 0;
  oldYaxis = yAxis;
  axisBuff = analogRead(yAxisPIN);
  delay(1);
  axisBuff += analogRead(yAxisPIN);
  delay(1);
  axisBuff += analogRead(yAxisPIN);
  delay(1);
  axisBuff += analogRead(yAxisPIN);
  delay(1);
  axisBuff += analogRead(yAxisPIN);
  yAxis = axisBuff/5/4-128;
}

void sendData(){
  digitalWrite(13, HIGH); // Flash a light to show transmitting

  vw_send((uint8_t *)str,strlen(str));

  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(13, LOW);
}

radio_rx -

#include <VirtualWire.h>
#include <roughLCD.h>        // My rough soft serial LCD library
#include <NewSoftSerial.h>                 // Soft serial port for LCD
NewSoftSerial lcdSerial(9, 8);  // Rx, Tx

boolean running = false;

void setup()
{
  lcdSerial.begin(19200);
  Serial.begin(9600);      // Debugging only
  Serial.println("setup");

  vw_set_rx_pin(14);
  vw_setup(2000);       // Bits per sec

  vw_rx_start();       // Start the receiver PLL running
  clearLcd();
  lcdSerial.print("Ready");
}


void loop(){
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    if(running == false){
      clearLcd();
      lcdSerial.print("Got: ");
      running = true;
    }

    int i;

    digitalWrite(13, HIGH); // Flash a light to show received good message
    // Message with a good checksum received, dump it.
    clearLcd();
    lcdSerial.print(buf[0], HEX);
    lcdSerial.print(" ");
    lcdSerial.print(buf[1], HEX);

    Serial.print("X Axis - ");Serial.println(buf[0], DEC);
    Serial.println();
    Serial.print("Y Axis - ");Serial.println(buf[1], DEC);
    Serial.println("");
    
    digitalWrite(13, LOW);
  }
}

VirtualWire is of course doing the fancy stuff with the radios (which works rather well) :slight_smile:

-128 is 0x80, -1 is 0xFF, +127 is 0x7F. Am I not seeing your problem?

Your 2's complement number line goes: -128 .... -1 ... 0 .... 127 0x80 0xff 0x00 0x7F

Unsigned number line goes: 0 ... 127 ... 128 ... 255 0x00 0x7f 0x80 0xFF

You're transmitting signed values (char), but receiving them as unsigned (uint8_t). You're just misinterpreting the values. -128 is represented by the value 0x80. If you print an unsigned 0x80, it comes out as +128.

Quite so, yes, I don't think I explained myself very well.

I seem to have it working, how I'm not sure, but work it does... I really should stop playing with things that require a degree of brain power so late at night.

I'll have a look tomorrow and explain what I was trying to do, I think I may have a little too much christmas spirit right now ;D