I'm quite new to Arduino. startet with building a Wiicopter based on the Arduino Pro Mini. But now i want to get a bit deeper into Arduino usage. I played around a bit with the code examples and for now i think I understand the most of it.
So my new project should Display the Livedata of my RC-model Datalogger (in the future by wireless transmission).
So here is my question:
i get a Dataaset of 24 Byte on the serial input. Now I have to divide the dataset into the different information. to start I wanted to store the information in an array of 24 Bytes. So i should get all the information stored in 1 Byte each. But there are also some information sored together in 1 Byte
here an Example:
Byte 1 : bit 1-4 dataset type ; bit 5-8 datasetnumber
the first example looks good for me. The second one, I'm sorry, but my Programming skills aren't that advanced at the moment :disappointed_relieved:.
Furthermore I think the first method would be better as nearly the whole mentioned 24 byte are used bitwise to send some specific data. So I suppose i could define the whole datastructure in the structure.
as I've mentioned my programming skills are still developing. Is there a good reference I can use to learn more about the structure and bit-fields. I did'nt find much as I searched for it now.
Bit fields may look simple to use, but there is a good deal of trickiness to hurdle over yet.
First, the operation of bit-fields is compiler- and platform-dependent, so you will want to look specifically at AVR-GCC bit fields. Here's an interesting thread:
AWOL did not mention how you will convert the int you read from the serial port into the bit-field struct so you can extract the data. That will look just as scary as what I posted.
It sounds like what you want is a combination of bit fields and a union:
union convertor
{
struct
{
byte type: 4;
byte number: 4;
} pieces;
byte whole;
} buff[24];
union convertor buff2[36]; // alternate way of declaring later in code
Once you have this, you ‘pour’ the bytes into buff, and get them out the other side as bit fields pieces:
for (i=0;i<24;++i)
{
buff[i].whole= {get data in somehow} ;
int y=buff[i].pieces.type; // the type of each entry
int z=buff[i].pieces.number; // the number of each entry
}
Of course, if the bitfield repeats the same for all 24 bytes, then you don’t need the array - one temporary value will do:
union convertor data;
data.whole= {get data in somehow} ;
int y=data.pieces.type; // the type of each entry
int z=data.pieces.number; // the number of each entry
But, if the data layout changes throughout the 24 bits, then you’ll need to tweak the union for all the bytes.
As told yesterday, I'm not a pro in programming, and so startet some deeper investigation yesterday evening.
I tried to understand the example with the masking and shifting, and basically i understood what should happen. But I still do not understand gardeners example.
I think I'll have to get into all this a bit deeper.
So I'm shure the next questions will come, but for now I have a lot to read and to learn.
Because I had (obviously) too much time on my hands, I wrote it up for my blog, including some different examples - perhaps it will explain it more clearly for you: