Hot answers tagged

26

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 ...


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

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 ...


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

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 ...


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? ...


6

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, ...


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

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

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

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, ...


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

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(){ ...


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

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);...


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 ...


4

You have a rogue ; here: for (int i = 0; i < sizeof (LED) / sizeof (LED[0]); i++);{ pinMode(LED[], OUTPUT); ^------HERE } That is terminating the for, making it empty, and the following block is a completely different scope. Another thing I spotted - you aren't putting i into your array index LED[] (should be LED[i])....


4

You really don't need to maintain a list. Just calculate the LED index from the location: int room = 14; int floor = 2; const int rooms = 92; int led = room + (floor * rooms); That is, each floor below the current one (floors and rooms count from 0) is a full row of rooms (92 * floors), and there are "room" extra rooms on this floor to add to it. You ...


4

C(++) does not natively let you "add" arrays like that. Your code looks like maybe Python. There are several ways to do what you want to do. You could make your code which writes the pattern to the array accept an offset - so rather than always starting at position 0, you could tell it where to start. You could write a function to "concatenate" arrays, ...


3

The simple answer is: don't. At the moment you are trying to assign integers to chars, and that just won't fit. It's like a round peg in a square hole. Or more like trying to force a bowling pin through a hose pipe. It just isn't going to fit. So you either need to "print" the values into your final string (say with sprintf()) or just don't build up a ...


3

This can be achieved with a simple preprocessor macro: #define readEE(VAR) VAR = readEEPROM(AT24C32_ADDRESS, 1, VAR ## Pos) You can then use: readEE(foo); and it will be expanded out to: foo = readEEPROM(AT24C32_ADDRESS, 1, fooPos); The ## operator in macros expands out both sides (VAR and Pos) and then concatenates the results together. Since VAR ...


3

variableNames[i] = readEE(variableNames[i] + 'Pos'); Is this possible using C++ / Arduino? No! Very few programming languages allow that as it requires a lot of run-time information. Let us instead revisit you issue; Load a large set of variables from EEPROM. What mechanisms do we have in C/C++ and AVR library that could help with this? Here is a ...


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