I'm super confused right about now!
I've been working on project involving Xbees in API mode, and I need to send different arrays of bytes to the Xbee depending on which buttons I press. Mostly what I have is working fine, but I'm having a lot of issues with the following questions; I figured I'd summarize all my questions first, then further explain them with code examples.
- Arduino requires arrays contain to a size. How can I declare
byte data[]
without a size? - Arrays sizes must be declared with a number, or a const integer. So how come I can declare
byte packet[arraySize];
whenarraySize
is being passed the integerledOn_len
that isn't constant? - How can I create a global array within a function?
I've taken the liberty to condense my code for readability purposes. Everything you see is here is mirroring my issues with my Xbee code.
byte ledOn[] = { 0x7E, 0xAA, 0x1C, 0x3B }; // turn led on
byte ledOff[] = { 0x7E, 0xAB, 0x1D }; // turn led off
int ledOn_len; // to be used for the array size of ledOn[] NOT a constant
int ledOff_len; // to be used for the array size of ledOff[] NOT a constant
// this doesn't work, because the array length must be a constant
//int arrayLength = 4;
//byte rando[arrayLength];
// this works just fine, since arrayLength is now a constant
//const int arrayLength = 4;
//byte rando[arrayLength];
void setup()
{
Serial.begin(115200);
// since using sizeof(data) inside of changeLed() doesn't
// work (it returns with 2, which is the size of a pointer)
// set them here, and pass them along.
ledOn_len = sizeof(ledOn);
ledOff_len = sizeof(ledOff);
}
void loop()
{
changeLed(ledOn, ledOn_len);
delay(1000);
changeLed(ledOff, ledOff_len);
delay(1000);
}
void sendToXbee()
{
//Xbee.write(packet); // packet isn't global, this doesn't work
// yes I know I didn't include any software serial in this demonstration
}
void changeLed(byte data[], int arraySize)
{
byte packet[arraySize]; // how does this work???? Pass by value
memcpy(packet, data, arraySize); // clone data to packet
// this is all the Serial print code
}
1) I created the function void changeLed()
to calculate the checksum (xbee crap). I need to pass it the array ledOn[]
or ledOff[]
for the checksum calculations to occur. So I simply added void changeLed(byte data[])
(and then void loop calls changeLed(ledOn)
) which works, but why?! Anywhere else if you declare an array without an array size it says you must provide one. Confusing to me, but it still works. . . kinda. When I serial print data[i]
byte by byte, it perfectly displays each byte in ledOn[]
, which is what I'm passing it. But when I call sizeof(data)
it says it's only 2 bytes. How can it contain the exact data of 4 bytes I give it while only being 2 bytes? Here is the serial monitor of it. The first line is the printout of data[]
, following the 4 bytes is the length which it claims is 2. The second line is the array of bytes packet[]
. I set packet[]
equal to data[]
with this: memcpy(packet, data, arraySize);
. Now you'll notice that the array size is correct. Interesting.
Update #1: It seems like this all has to do with pointers somehow. I guess data[]
is just pointing to or referencing the array ledOn[]
, and using memcpy(packet, data, arraySize);
is actually copying the array ledOn[]
to the array packet[]
?
2) I try to declare a random array (byte rando[]
) using int arrayLength
as the size. This does not work because the array size must be a constant. So I simply add const int arrayLength
and that works. Great. So how am I able to do byte packet[arraySize];
!? ArraySize
is being passed ledOn_len
which is not a constant, so how come that works?
3) I need to be able to use the array packet[]
inside of another function void sendToXbee()
. Packet[]
will be my final array of bytes to send to the Xbee containing either the array ledOn[]
or ledOff[]
which are different array lengths. As is, I cannot use packet[]
in void sendToXbee()
because packet[]
is local to void changeLed()
. But here's the catch 22: I cannot declare packet[]
in the setup making it global, because I don't have a size for that array yet!! The size of that array must be calculated within void changeLed()
, depending on which array I need to send to the Xbee, ledOn[]
or ledOff[]
.
I know this is a lot but I'd appreciate the help! Thanks guys.