Hot answers tagged

27

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 buffer in turn and sent out through the serial port. That's all fine, up until the end when you use exit(0);, and what is left in the serial buffer to send never ...


11

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: “error: expected primary-expression before ‘]’ token”, meaning the compiler expected return array[some_index];. The way that almost works Remove the brackets ...


9

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 is because of integer wraparound. You only provide a 16-bit unsigned variable to store it in, so all you get is the lower 16 bits of the answer. 92000 + 40000 +...


8

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 variables and dynamic memory heap allocation). Heavy use of String variables is a likely explanation for the observed behavior. Also a lot of string literals are ...


7

If you have a C string containing ASCII numbers with a common delimiter (comma in this case) you can use the strtok() function to split it into individual strings. Then you can use atoi() to convert those individual strings into integers: char array[] = "10,11,12,1,0,1,0"; int intArray[7]; // or more if you want some extra room? int ipos = 0; // Get the ...


7

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, 8}; You will need to call pinMode() in a loop; it doesn't accept arrays as arguments. How about for digitalWrite() where its a mix of HIGH and LOW? ...


7

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 unit. If you use 0.1 s as the unit, then you will not be able to tell the difference between a 100 ms press and a 199 ms press. I am sure these ...


7

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. Since it is in global scope, and not explicitly initialized, it is implicitly initialized to all bytes zero. leds[0] = {0,0,0,0,0}; Here, you are trying to give ...


6

Yes you can have arrays inside arrays. The array would be declared as: int arrayName [ x ][ y ]; where x is the number of rows and y is the number of columns. The example below declares and initializes a 2D array with 3 rows and 10 columns: int myArray[3][10] = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15, 16, 17, ...


6

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 & b, int & c) { a = 19; //these are just random numbers b = 7; c = 69; } void setup() { Serial.begin (115200); int x, y, z; foo (x, y, z);...


6

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 you need to use strlen(s) and add one to it for the NULL character at the end. if(verbose){ char buf[8 + strlen(s) + 1] = "[DEBUG] "; strcat(buf, s); ...


6

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 ASCII code for the double quote (") 0x33 is the ASCII code for the digit 3. 0x44 is the ASCII code for the uppercase letter “D” etc.


5

You are not creating an array of references, you are creating the array from the objects; they are being copied. You can see that this by moving the line (and adding an equals) Train trains[trainCount] = {train1, train2}; to the bottom of your setup method. However, that is beside the point, as you are not fully using the power of arrays. You needn't ...


5

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 memory on the Arduino Mega to hold 0.003% of your image. Maybe you could store the image on an SD card and put it on the screen little piece by little piece, ...


5

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 to fit into about 18KB of flash memory, you can store one 230KB image in flash on a Mega. (230KB+18KB+8KB = 256KB = Mega flash size. 8KB is Mega bootloader ...


5

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. The simple fact that you haven't put anything in there means that what was there before hasn't been changed.


5

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

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 see what it prints: Serial.println(A0); And for your pins, just put A0, A1, etc. in your int array and leave the quotes off. Those symbols are being ...


5

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-C language supports it as an extension. Also, newer versions of GCC (6 and higher) allow this in GNU-C++ code as an extension as well. But GCC 5.4.0 used by ...


5

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,&Serial2,&Serial3}; void setup(){ for (int i=0;i<=3;i++){ Serials[i]->begin(38400); while (!*Serials[i]); } } void loop(){ ...


5

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(colors) / sizeof(colors[0])) int *EXCLUSIVE_COLOR = NULL; void setup() { Serial.begin(115200); Serial.print("You have "); Serial.print(NCOLOR); ...


5

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: Incorporated great improvements and a fix from Edgar in the comments. String overload. Display baud rate suggested by mehmet #define DISPLAY_DEVICE Serial #define ...


4

It is quite right, using that kind of syntax is not allowed. It's a bit of a pain, but it's ok since there is an alternative method - kind of a "trick" if you will. That trick is to use a string, not an array. After all, a string is just an array, it's just handled slightly differently by the compiler. Instead of using {...} use "..." and use the ...


4

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)+sizeof(a2), a3, sizeof(a3));


4

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. It is the same as: int array[100]; Uninitialized static / global variables, and variables explicitly initialized to 0, are placed in the .bss section of ...


4

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 DigitalOutput class, the size is the number of elements multiplied by the size of the class - in this case 10 bytes. To get the number of elements you must ...


4

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 report the number of elements, divide the total size by the number of bytes per element. For example: nElements = sizeof BuffA / sizeof BuffA[0]; As uint16_t ...


4

Arrays in C are fixed. sizeof() will always give the amount of allocated memory for the array. Instead, you should be using strlen() to get the number of characters up to (but not including) the terminating NULL character at the end of the string. ScrollingMsg.SetText ((unsigned char *) TxtDemo, strlen(TxtDemo)); Make sure that you have enough space in ...


Only top voted, non community-wiki answers of a minimum length are eligible