boolean array

Hello everybody, I have a question related to the serial communication in arduino

actually I send data from the serial port Tx of an (arduino board1) "Serial.write((unsigned byte) data);" , and I receive it in the Rx port of the (arduino board2),

So my question is how can I stock the received data in a boolean array like this bool receaivedData[40]={1,0,1,0,1,0,1,1,11,0,0,0,........};

So my question is how can I stock the received data in a boolean array like this bool receaivedData[40]={1,0,1,0,1,0,1,1,11,0,0,0,........};

Just like you would if the data was character data.

http://forum.arduino.cc/index.php?topic=396450.0

Thank you for the quick response :) , Maybe my question has to be like this "how to convert a received Bytes to a boolean array ?"

Do you know the maximum number of array members being sent?

Yes - Define the target array on Arduino #2 to that known size Create a char *pointer to aim at the character array, Reset the character counter (if needed)

...when serial characters arrive, save them at pointer++ *pointer = 0 to null out the *[u]last[/u] character of the array

If the values are all [u]non[/u]-zero - When your last value arrives, go off and process the array, then reset the pointer to the start of the array, and *pointer = 0 to null out the first character of the array

If the values may include zero as shown above - Keep track of the character count (reject chars greater than max char count!) When your last value arrives, go off and process the array, Then reset pointer to the start of the array, and reset your character counter

It is worth adding a timeout, and/or verifying your char counter total - in case of tx/rx errors - to reject the whole array, and ask for a retry.

For more detail - we need to see your receiving code, and may not 'write' it for you, but can offer more specific help...~!

Maybe my question has to be like this "how to convert a received Bytes to a boolean array ?"

Maybe not.

You MIGHT want to convert ONE value to store in ONE element of an array.

But, we can't see your code OR understand what you are trying to do.

thank you for your answer, my code is quite long bc I do some channel coding before sending the data, I send 6 bytes juste like that

Serial.write((unsigned byte) sentdata1); Serial.write((unsigned byte) sentdata2); Serial.write((unsigned byte) sentdata3); Serial.write((unsigned byte) sentdata4); Serial.write((unsigned byte) sentdata5); Serial.write((unsigned byte) sentdata);

so I have to stock my received data in a boolean array[48].

so I have to stock my received data in a boolean array[48].

You send 6 bytes, from variables numbered 1, 2, 3, 4, 5, and nothing. What's up with that dumb numbering scheme?

Why do you need to store the 6 values in a 48 element array?

What is the problem?

because I do hamming decoding after receiving data , that means I do XOR calculation bit by bit to recreat a new decoded data

because I do hamming decoding after receiving data , that means I do XOR calculation bit by bit to recreat a new decoded data

This has nothing to do with reading 6 bytes and storing them in a 6 byte array.

I really appreciate your help ,and I don't know if it's possible or not but really I need to know how to convert a received 6 Bytes from the serial port to an array of 48 boolean value,

I need to know how to convert a received 6 Bytes from the serial port to an array of 48 boolean value

It sounds like you want to turn each bit of the 6 bytes into a boolean value in a 48 element array. Is that right ?

If so, then for each byte you receive examine each bit, perhaps using bitRead() and set the boolean to the corresponding value. Could you not just have an array of 6 bytes and use the bit values from them in your program.

but really I need to know how to convert a received 6 Bytes from the serial port to an array of 48 boolean value,

Only you know how you want to convert the 6 bytes to 48 bytes.

UKHeliBob yes exactly that what I want to do,

medeloualidi: UKHeliBob yes exactly that what I want to do,

So, what is the problem? Why DO you need to multiply the memory required to hold the data by a factor of 8?

ok, I'm out. let us know how you go with the end result.

medeloualidi: UKHeliBob yes exactly that what I want to do,

My advice would be to read the 6 bytes and as you read each one put it into an array of bytes. When you need to know the value of a particular bit of any of the bytes you can read it.

UKHeliBob: My advice would be to read the 6 bytes and as you read each one put it into an array of bytes. When you need to know the value of a particular bit of any of the bytes you can read it.

You could also pack them into a 64bit unsigned int so that you can traverse the bits on their own:

union BitArray{
  byte myDataArray[8];  // you would only use six
  uint64_t bitVar;
};

BitArray bitArray;

UKHeliBob:
My advice would be to read the 6 bytes and as you read each one put it into an array of bytes. When you need to know the value of a particular bit of any of the bytes you can read it.

thank you, yes you’re right ,so this code is true to read 6 bytes first ?

void setup() {
Serial.begin(9600);

if (Serial.available() >= 6) { // if 6 bytes are alleeady here
byte buffer[6];
Serial.readBytes(buffer, 6); // read 6 bytes

}

}

It is unlikely to work in the setup() function as written and it is probably not how I would do it, but the principle looks OK.

Why not try it and see. Input the data using the Serial monitor and print the contents of the array. Do they match.

Example for examining the bits inside a byte, using bitwise operators.

// given byte value
// examining bits 0..7 (0 is LSB = least significant bit)


for (int bit=0; bit<=7; bit++) {
   if (value & (1<<bit)) {
      // add true to array
   } else {
      // add alse to array
   }
}

The “1<<x” shifts a bit left by adding zeros to the right of it, binary speaking, so for example 1<<2 becomes binary 100, which corresponds to decimal 4.

The single ‘&’ is bitwise AND, and is used to check if any bits match up. Since we check against a value with only 1 bit and the rest zeros, we get non-null value only when the corresponding bit in the value is set. And non-null value corresponds to “boolean true”. Sort of.

:slight_smile: