Compacting data for wireless transfer

Hello this is my first post here, I'm working with arduino for a while (not for a long time). And I'm working on a project. I have two modules that I made one is transmitter and other one is receiver, I want as smooth connection as possible so I want to compact my data I'm going to send. I compacted button and switch values (it was easy I just used binary).

I wonder if could compact my pot. values (there are four of them two of them is from joystick, I mapped them to 0-256).

And I made one long (14-26 bytes) string to send I will decode it on receiver (is it a good idea?)

Im using x and y values of a gyro-accel. module, 2 switches, 2 buttons, 2 pot.s 1 joystick and nrf24l01 +PA LNA to communicate

Thanks for your help :)

Why send the data as a string (or do you perhaps mean a String ?) when you could send it as binary data in an array or a struct ?

How do you think I should do it? I dont know which one is the best (Im trying arrays now)

If all values to be sent are of the same data type, use an array of them. If not, use a struct.

They are all integers thanks I will use an array. (edit: wow that helped a lot my new sketch is only 50% of my old sketch (I think turning integers and floats to strings made my old sketch that heavy) )

They are all integers thanks I will use an array.

Are they all integers or are they ints (not the same thing at all)

two of them is from joystick, I mapped them to 0-256).

If you map the 0 - 1023 values to 0 - 255 by simply dividing the value by 4 then you can fit each of the values in a byte. 4 digital values will fit in a single byte (actually up to 8 of them would) so your potential payload is just 3 bytes

should I divide the values or map them? I mapped them (they are all ints)

#include<Wire.h>

const int MPU_addr=0x68; int16_t AcX,AcY,AcZ;
int minVal=265; int maxVal=402;

int x, y, i; int VCC=8;

const int JOX=A0; const int JOY=A1; const int PO1=A2; const int PO2=A3;
const int SW1=2; const int SW2=3; const int BU1=4; const int BU2=5;

int JOXV; int JOYV; int PO1V; int PO2V;
int SW1V; int SW2V; int BU1V; int BU2V;
int ValueArray[]={};

void setup()
{ 
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); Wire.write(0);
Wire.endTransmission(true);

Serial.begin(115200);
pinMode(VCC, OUTPUT);
digitalWrite(VCC, 1);
pinMode(JOX, INPUT); pinMode(SW1, INPUT_PULLUP);
pinMode(JOY, INPUT); pinMode(SW2, INPUT_PULLUP);
pinMode(PO1, INPUT); pinMode(BU1, INPUT_PULLUP);
pinMode(PO2, INPUT); pinMode(BU2, INPUT_PULLUP);
} 
void loop()
{ 

JOXV=map(analogRead(JOX),1023,0,256,0); SW1V=digitalRead(SW1);
JOYV=map(analogRead(JOY),1023,0,256,0); SW2V=digitalRead(SW2); 
PO1V=map(analogRead(PO1),1023,0,256,0); BU1V=digitalRead(BU1);
PO2V=map(analogRead(PO2),1023,0,256,0); BU2V=digitalRead(BU2);

if(SW1V==1){               //sw1=2^3 sw2=2^2 bu1=2^1 bu2=2^0
  if(SW2V==1){
    if(BU1V==1){
      if(BU2V==1){
        i==15;
      }
      else{
        i==14;
      }
    }
    else{
      if(BU2V==1){
        i==13;
      }
      else{
        i==12;
      }
    }
  }
  else{
    if(BU1V==1){
      if(BU2V==1){
        i==11;
      }
      else{
        i==10;
      }
    }
    else{
      if(BU2V==1){
        i==9;
      }
      else{
        i==8;
      }
    }
  }
}

else{
    if(SW2V==1){
    if(BU1V==1){
      if(BU2V==1){
        i==7;
      }
      else{
        i==6;
      }
    }
    else{
      if(BU2V==1){
        i==5;
      }
      else{
        i==4;
      }
    }
  }
  else{
    if(BU1V==1){
      if(BU2V==1){
        i==3;
      }
      else{
        i==2;
      }
    }
    else{
      if(BU2V==1){
        i==1;
      }
      else{
        i==0;
      }
    }
  }
}
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);

AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

y= floor(RAD_TO_DEG * (atan2(-yAng, -zAng)+PI));
x= floor(RAD_TO_DEG * (atan2(-xAng, -zAng)+PI));

ValueArray[0]=JOXV;
ValueArray[1]=JOYV;
ValueArray[2]=PO1V;
ValueArray[3]=PO2V;
ValueArray[4]=i;
ValueArray[5]=x;
ValueArray[6]=y;
Serial.print("Test ");
Serial.println(ValueArray[1]);
}

actually this is my code without nrf communication it works but can be improved I think

My understanding is that dividing by 4 is more efficient than map() because it can be done by bit shifting

Acarhan: so I want to compact my data I'm going to send.

An nRF24 can send 32 bytes in a single message. If your data can fit within that then I would not waste time compacting it.

Please post a sample of one of the data messages that you want to send.

...R Simple nRF24L01+ Tutorial

I tried what you said and it didnt work quite nice it just printed 0 and when I changed /4 to map it worked

Acarhan: I tried what you said and it didnt work quite nice it just printed 0 and when I changed /4 to map it worked

Show code, please.

#include<Wire.h>

const int MPU_addr=0x68; int16_t AcX,AcY,AcZ;
int minVal=265; int maxVal=402;

int x, y; int VCC=8; int i=0;

const int JOX=A0; const int JOY=A1; const int PO1=A2; const int PO2=A3;
const int SW1=2; const int SW2=3; const int BU1=4; const int BU2=5;

int JOXV; int JOYV; int PO1V; int PO2V;
int SW1V; int SW2V; int BU1V; int BU2V;
int ValueArray[]={};

void setup()
{ 
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); Wire.write(0);
Wire.endTransmission(true);

Serial.begin(115200);
pinMode(VCC, OUTPUT);
digitalWrite(VCC, 1);
pinMode(JOX, INPUT); pinMode(SW1, INPUT_PULLUP);
pinMode(JOY, INPUT); pinMode(SW2, INPUT_PULLUP);
pinMode(PO1, INPUT); pinMode(BU1, INPUT_PULLUP);
pinMode(PO2, INPUT); pinMode(BU2, INPUT_PULLUP);
} 
void loop()
{ 

JOXV=analogRead(JOX)/4; SW1V=digitalRead(SW1);
JOYV=analogRead(JOY)/4; SW2V=digitalRead(SW2); 
PO1V=analogRead(PO1)/4; BU1V=digitalRead(BU1);
PO2V=analogRead(PO2)/4; BU2V=digitalRead(BU2);

if(SW1V==1){
  i=i+8;
}
if(SW2V==1){
  i=i+4;
}
if(BU1V==1){
  i=i+2;
}
if(BU2V==1){
  i=i+1;
}

Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);

AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

y= floor(RAD_TO_DEG * (atan2(-yAng, -zAng)+PI));
x= floor(RAD_TO_DEG * (atan2(-xAng, -zAng)+PI));

ValueArray[0]=JOXV;
ValueArray[1]=JOYV;
ValueArray[2]=PO1V;
ValueArray[3]=PO2V;
ValueArray[4]=i;
ValueArray[5]=x;
ValueArray[6]=y;
Serial.print("Test: ");
Serial.println(ValueArray[0]);
i=0;
}

this is the code (I think the problem is using ints) (Sorry I forgot to delete (JOXV,1023,0,256,0))

UKHeliBob: My understanding is that dividing by 4 is more efficient than map() because it can be done by bit shifting

I don't think the compiler will optimize that, because analogRead returns a signed integer, and dividing a negative integer is not the same as shifting.

Pieter

int ValueArray[] = {};

An array with zero elements.

Whoops !

UKHeliBob: int ValueArray[] = {};

An array with zero elements.

Whoops !

Yup.

So any index is out of bounds, so writing to it results in other pieces of memory getting scribbled over, leading to undefined behavior.