27
votes
Why does Serial.print print only a value for 0-84 for a 100 integer array?
It's because you're using exit(0). That turns off interrupts and goes into an infinite loop. However, serial printing is first placed into a buffer, and each character is then removed from that ...
14
votes
Returning an int array from a function
There are several ways to achieve what you want:
The way that does not work
Did you try to compile the code you gave in the question? Then you
probably noticed it does not compile. I tried and got: “...
10
votes
Accepted
Initializing Array of structs
led h_red = {0,0,255,0,300};
Here, you are defining a variable, and at the same time giving it an
initial value. This is called an initialization.
led leds[LEDS];
Here you are defining an array. ...
9
votes
Sizeof variables and Due's RAM
sizeof doesn't return the number of elements. It returns the number of bytes. Since they are uint16_t arrays each element is 2 bytes - hence twice the size.
The reason your last number is only 40928 ...
8
votes
for loop running infinitely
After a chat with OP, it turned out this was a more complex issue, probably a memory overrun.
This cannot be seen in this small snippet, but the whole program is using memory extensively (global ...
7
votes
Accepted
Returning an int array from a function
I have a function that needs to return 3 int values.
In the interests of completeness I'll suggest another method. Let's assume that you really need 3 values and not 300.
void foo (int & a, int ...
7
votes
Replacing several pinMode() and digitalWrite() pins with an array
the correct way to store the pins as an array
Your array initializers are almost right; you just need to separate the values with commas:
digitPins[] = {13, 12, 11, 10};
segPins[] = {2, 3, 4, 5, 6, ...
7
votes
Lowest memory consumption for arrays
If I dare suggest an unorthodox solution... I suggest you store the
times as “float11” floating point numbers.
Michel Keijzers' solution is nice, but there is an issue with the choice
of the time ...
7
votes
Accepted
How to get char array's length
The function I think you are looking for is strlen - STRing LENgth.
It counts the number of characters in a string up until it finds the "NULL" end of string marker.
Serial.println(strlen(t));
6
votes
How can I concatenate multiple byte Arrays into one Array
You can concatenate with memcpy. You just need to set the pointer at the right place inside the c1 array.
memcpy(c1, a1, sizeof(a1));
memcpy(c1+sizeof(a1), a2, sizeof(a2));
memcpy(c1+sizeof(a1)+...
6
votes
Accepted
Max length of char arrays?
The main issue with your code is:
sizeof(s)
That doesn't return the string length - it returns the size of the char pointer - which is two bytes (on an 8-bit system, 4 on a 32-bit system).
Instead ...
6
votes
Accepted
How can I initialize an array of objects in setup?
As Craig wrote, the declaration of an class typed array will call the default constructor of the class, which is the one without any arguments. The class, that you use, does not provide a default ...
6
votes
Printing the array using print and serial write function in Arduino Uno
Serial.write(some_byte) writes a byte on the serial output. The
Arduino serial monitor tries to interpret the bytes it receives as text:
0x11 is a control character, displayed as “□”
0x22 is the ...
5
votes
Does int array[100] = {0} construct work on Arduino?
This construct does work in Arduino IDE. In fact, = { 0 } is one of the iconic idioms of C language. It can be used to zero-initialize an object of any type in C. In C++ things are a bit more ...
5
votes
Image Breakdown
240 by 320 pixels. That's 76800 pixels. In your example it looks like you want 3 bytes per pixel, so that's 230400 bytes. An Arduino Mega has 8K bytes of SRAM memory. There's enough room in the ...
5
votes
Accepted
Image Breakdown
I presume you are trying to create a system that has one image stored, and want the image to be available without needing an SD card reader on the system.
If the logic of your program is small enough ...
5
votes
Accepted
Why does this array have stored values in it even though I have not put any values in it?
The values haven't been "put" in the array. The array has been located in memory where those values (left over from some earlier operation, possibly as part of a function's stack frame) happen to be.
...
5
votes
Accepted
Sending >255 bytes array to arduino
You use type byte for the variable ndx. Type byte is 8 bits, so the max value is 255. The variable ndx is incremented and after 255 it continues counting from 0.
5
votes
Accepted
Help with AlphaNumeric array
All of those "A1" "A2" pins have other numbers that go with them. The A1 or A2 is #defined in the core to be some number. You can just use A1 or A2 and it will work.
For example, try this line and ...
5
votes
Accepted
Why does a variable size struct not compile in the Arduino IDE?
Flexible array member is a C feature. It does not exist in C++. On top of that, the way you use it to declare and initialize a static struct of flexible size is non-standard even for C.
However, GNU-...
5
votes
Accepted
HardwareSerial Indexing Array
your code makes copies of Serials into items in the array of HardwareSerials. To use the original Serial objects, store and use pointers.
HardwareSerial* Serials[]={&Serial,&Serial1,&...
5
votes
Accepted
Setting an array pointer to null on declaration
Here is a full example that shows you all thing things you have been asking over the past few days:
int colors[][3] = {
{255, 0, 0},
{0, 255, 0},
{0, 0, 255}
};
#define NCOLOR (sizeof(...
5
votes
Accepted
How to convert a String to Hex array
I don't know anything about the particular display, but based on the information provided I hope this is at least shows the foundation of one way you could approach a final solution.
Update:
...
4
votes
Accepted
Does int array[100] = {0} construct work on Arduino?
I'm used to this way from my desktop C++ programming practice:
int array[100] = {0};
I couldn't find any info on whether or not this construct works on Arduino.
The = {0} has no actual effect. ...
4
votes
Does int array[100] = {0} construct work on Arduino?
The GNU cross compilers for Arduino use C/C++ front-ends and meet some version the language specs. (Although that is a moving target, you can expect a recent IDE version will be compliant with a ...
4
votes
Accepted
sizeof() on custom library constructor returns wrong result
It is working perfectly correctly.
sizeof doesn't return the number of elements in an array, it returns the number of bytes the array takes up. Since each entry in your array is an instance of your ...
4
votes
Sizeof variables and Due's RAM
The sizeof operator's result is number of bytes, not number of array elements. Your arrays in the program above have two bytes per element, hence twice as many bytes as elements.
If you want to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
array × 277arduino-uno × 50
c++ × 42
serial × 38
programming × 38
string × 32
pointer × 27
arduino-ide × 18
c × 16
variables × 14
arduino-mega × 13
struct × 11
esp8266 × 9
memory-usage × 9
memory × 8
functions × 8
arduino-nano × 7
library × 7
progmem × 6
esp32 × 5
softwareserial × 5
arduino-due × 5
teensy × 5
loop × 5
data-type × 5