id, like to find the way to split a byte into halfs and get the representative of each half as a decimal number.
I explain a bit , i,m sending lots of values to Arduino Mega via udp ethernet shield. i need to send 512 Set of values maximum of 5 values each.
as i,m sending now
[FirstValue][SecondtValue][ThirdValue][ForthValue][FithValue] X 512 = 2560 bytes which are too many
those are the value ranges of each value:
[0-31][0-16][0-7][0-7][0-7]
as Ethernet shield has a max buffer of 1500 bytes i can not send all data i need in full bytes.
so as i,m not using the full value of the unsigned 8 [0-255]i,m sending i wanted to split those into 2
so for now i would like to concentrate and know how to convert those 3 bytes [0-7][0-7][0-7] into 2 bytes data instead of 3 .
for example if i send the values of [6][1][2] i would send 2 bytes containing the first the value :
of 61 and the second the value of 20
so first one split to get 6 for first and second 1 .
i would send the value of 3D in hex for the 61 that will become 2 values of 6 and 1 .
do you see what i mean ? maybe you have better ideas but i think this one could work if i get how to do it in Arduino.
can you please give me some hints and guidance about it ?
Have a look at the bit shifting and bitwise and/or operators.
Here is something to start you off
byte aVar = 0b10100101; //decimal 10 in msb and decimal 5 in lsb
void setup()
{
Serial.begin(115200);
int msb = (aVar & 0b11110000) >> 4; //get the first 4 bits of the byte and shift them right 4 places
int lsb = aVar & 0b00001111; //get the last 4 bits of the byte
Serial.println(msb);
Serial.println(lsb);
}
void loop()
{
}
[0-31][0-16][0-7][0-7][0-7] can be packed into 19 bits but if you round up to byte boundary that's 3 bytes (24 bits) x 512 = 1536 bytes
You still wont be able to send the entire data set in one packet. You would pack/unpack the data using the bit shift principle outlined by UKHeliBob
Hi thanks for your answers
i,ll read more about it.
in the end i,m going to send only 3 parameters in the range of [0-15] [0-15] [0-15]
so i can send them all in 2 bytes x 512 , 1024 which then i,ll be able to send to ethernet shield.
what will be the right way do it ?
1 byte convert to binary and do the spliting shifting or join all 2 bytes put all binary in a row and do the shifting ?
1 byte convert to binary and do the spliting shifting or join all 2 bytes put all binary in a row and do the shifting ?
Maybe not the most elegant or efficient code but something like this example.
byte a = 0x1A;
byte b = 0x2B;
byte c = 0x3C;
void setup(){
Serial.begin(115200);
unsigned int r = 0;
r = a & 0x0F; // Copy a to r, masking to keep lower nibble only
r <<= 4; // Rotate right r by a nibble (4 bits)
r = r | (b & 0x0F); // Or the lower nibble of b with r
r <<= 4; // Rotate right r by a nibble (4 bits)
r = r | (c & 0x0F); // Or the lower nibble of c with r
Serial.print("a = "); // Print the results
Serial.println(a,HEX);
Serial.print("b = ");
Serial.println(b,HEX);
Serial.print("c = ");
Serial.println(c,HEX);
Serial.print("r = ");
Serial.println(r,HEX);
}
void loop(){}
i got some better idea and played with bit Read functions , although in those i did not see easy way to rotate the nibble easily .
byte a = 0x1A;
byte b = 0x2B;
byte c = 0x3C;
byte Converted;
void setup(){
Serial.begin(115200);
for (int i =0; i < 8; i++) {
Serial.print(bitRead(b,i) );
Serial.print(" ");
Converted = bitWrite(Converted,i,bitRead(b,i));
Serial.println(Converted);
}
}
void loop(){}
@wildbill
i,m sending those values to change pixel colors , there are time were all pixel are send therefore i need to send the whole packet , this is how i have it and works very fast only missing some pixels because the ethernet 1500byte buffer limitation , but after i can compress it will be perfect.
//
I,m a bit confuse in how to do the encoding as for example if i send
2 bytes with [0-15] [0-15] [0-15] like 121 102 and split into 2 i get the appropriate values for each 12 11 02 so this could be done without the bit tricks. the problem is when i send values with one digit only or starting with 0 , i can do the padding in computer side but it will remove the 0 in conversion so for 020010 this should be a value of 2,0,1 . but it will send the bytes like 2010 so i will miss the real value.
so if i do it using bits will this happen too ? , what will be the procedure , i convert it piece of the 020010 to binary and then those to hex and send this character and in arduino do the bit splitting ?
ok i got it , now i found the way to encode the bytes in computer side correctly , so i will send 2 bytes like this :
4F 00 it should generate values of 15,4,0
20 06 it should generate values of 0,2,5
2F 0F it should generate values of 15,2,15
2F 0F it should generate values of 15,2,15
0B 07 it should generate values of 11,0,7
so i guess to decode in arduino i need to use the << operators right ?