Hot answers tagged

8

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


4

When you pass a char* input to deserializeJson(), it alters the input to avoid making useless copies. This is the "zero-copy" mode of ArduinoJson. In this mode, the JSON parser modifies the input in-place. The following modifications are performed: '\0' are inserted at the end of each string Escaped special characters (like \n) are unescaped ...


4

On the AVR platform the char type happens to be signed, and it can store numbers in the range [−128, +127]. Thus, when you write char bar = 128; You are asking to store in bar a number that does not fit. It then gets reduced modulo 28 into the value that does fit, namely −128. For the kind of manipulations you are trying to make, I would recommend you use ...


4

There is no problem here. Only the compiler is a bit smarter than you anticipated. It sees, that you are not using arr anywhere, so it just optimizes it out. There are two ways to prevent that, if you really want to: I think you can use flags to tell the compiler, that this shouldn't be optimized out. I'm no expert in this, others might know more about this ...


3

There are multiple problems here: if(Hexa_Val[7] = 0xFF) First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==. Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want ...


3

Storing the data into arrays is not an end in itself. The question is: what do you want to do with these arrays? The way to proceed depends on that purpose. If you want the data acquisition (analogRead()) and the processing (computation and printing) to proceed in parallel, then you will need the arrays for buffering the data between those two processes. As ...


3

Here's an example of how to do it: jump_table.ino int func1(int arg) { return arg + 1; } int func2(int arg) { return arg + 2; } int func3(int arg) { return arg + 3; } int func4(int arg) { return arg + 4; } int func5(int arg) { return arg + 5; } int func6(int arg) { return arg + 6; } int func7(int arg) { return arg + 7; } int func8(int arg) { return ...


3

add r16, Z+ You probably meant something like add r16, memory[Z+] However, the AVR is a RISC processor. It has instructions to address the memory, and instructions to do arithmetics, but it has no “complex” instructions that both address the memory and do arithmetics with the addressed data. So you have to use separate instructions to: Load (ld) the data ...


3

To make it generic, you simply divide the sizeof the array by the sizeof the type: for(int i=0; i<sizeof(controls)/sizeof(SwitchControl); i++){ } Then you can later change the items if the array, or the structure and the code will be the same.


3

Basic Arduinos have no support for floating point in sprintf and related functions. Instead you have to use dtostrf() to build up a string block by block. However there is no benefit to building a string then outputting to serial "in one instance" compared to outputting the data a bit at a time. Serial is slow. Very slow. When you "output&...


3

When you want to perform actions in response to button presses, you should take care of reacting to the signal edges rather than levels. In other words, you perform the action only when the signal changes from LOW to HIGH, not every time you see it HIGH. You should also read about button bounce, and either implement your own debouncing or use an existing ...


3

The error message is quite explicit: argument of type "const byte (*)[8]" is incompatible with parameter of type "const byte *" If you want to pass SPACED_STRIPES to the method, it is going to be passed as const byte (*)[8], i.e. pointer to arrays of 8 byte. You can just set the parameter type accordingly: void CSMatrix::...


2

There is no such concept in C. Arrays are a fixed size and that size can never be changed. You can't "delete" an entry. You can replace one with something else, and you can introduce the concept of a variable that says how many entries the array has, but you can't delete one. So you could: Keep a count of how many entries are in the array Shuffle ...


2

You CAN store them in an array, but NOT in an array of pointers to characters. That is just the wrong type. Try Adafruit_MAX31856 thermocouples[] = {TCouple1, ...} or even ditch the first declarations and use Adafruit_MAX31856 thermocouples[] = { Adafruit_MAX31856(2,11,12,13), Adafruit_MAX31856(3,11,12,13), Adafruit_MAX31856(4,11,12,13), ... ...


2

When you pass an array to a function it "collapses" the array into just a pointer. All the receiving function knows is the type of the contents of the array (time_t) and where it starts in memory - it knows nothing about the size of the array. The sizeof() call just gives you the size of that pointer, not the size of the memory it points to. sizeof(...


2

If you modify the code slightly, you can use an array of characters of any length, so need to worry about digital word length. Here's a modified version of your program that does this. Note that I also took the liberty of replacing the 'magic number' 13 with LED_PIN, and the 'magic number' 12 with KEY_LEN. I would suggest you do the same with your ...


2

You should make byte key = 0b101100001111; global, i.e. put it BEFORE the setup function and not within it. Also, a byte only can contain 8 bits, you need 12, so make it an int (thanks to ocrdu, see comment below): int key = 0b101100001111; If you need more bits, you can use uint32_t or uint64_t, and count further than the current 12. Now key is a local ...


2

A more generic way (usable in future cases) is: // number of items in an array #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) Now you can use ARRAY_SIZE (the_array) in your code like this: for(int i=0; i<ARRAY_SIZE (controls); i++){ } The above works in C as well as C++. The more C++ way of doing it is to use a template like this: // number of ...


2

t[len + 1] = '\0'; Your array is declared as having len+1 bytes of storage. So the last index is len, not len+1. You're writing the zero one past the end of the array, thus you're not transmitting it (and sending one byte of "garbage" instead).


2

This is really a pure programming problem rather than an Arduino one, but to get you started, you could do a table lookup, like this: #include <stdio.h> #include <string.h> // number of items in an array #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) unsigned char patterns [] [5] = { { 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H /...


2

My DaquinOscope https://www.daqarta.com/dw_rroo.htm uses the open-source DaqPort sketch https://www.daqarta.com/dw_rraa.htm to sample in batches, then transfer the data to the host in one high-speed blast. It takes 1024 total samples, which in your case of 2 channels would be 512 per channel. It stores only the raw data, unscaled... the host is the place ...


2

The easier way to simply the code is to use strcpy to create a buffer that is 1 byte longer than the length and then terminate it with \0. void messageHandler(char *topic, byte *payload, unsigned int length) { char buffer[length+1]; strcpy(buffer, (const char*) payload); buffer[length] = '\0'; Serial.println(buffer); int matched = strcmp(buffer, &...


2

You seem to already understand that it's not null-terminated and that's why it's not working the way you expect. So, I'll take a different tact for "how to fix it". You can simply not treat the payload as a string, and instead use memcmp, as in: if (length == 9 && memcmp(payload, "ravenclaw", 9) == 0) { memcmp behaves similarly ...


2

Two-dimensional arrays come in two "flavours". Actually, multi-dimensional arrays are possible in any combination of these. All elements in a rectangular grid, at least from the abstract view of the programming language; A one-dimensional array of one-dimensional arrays. If you declare: byte array[n][m]; then you have reserved space for "n ...


1

Consider the following: char buffer2[16]; // array of 16 chars for (uint8_t i = 0; i < 16; i++) { do_something_with(buffer2[i]); // handle one char at a time } Here we have an array of 16 cells, each having the char type (which I assume is what you have). The loop goes through the array, handling one cell a time. Within the loop, buffer2[i] ...


1

not going to pretend i understand why all my keypad numbers are 48 higher than the number printed on the keypad but: int key2 = key -48; works


1

You can create two arrays, e.g.: #define MAX_LENGTH 20 volatile float _gauge0[MAX_LENGTH]; volatile float _gauge1[MAX_LENGTH]; volatile int _filled = 0; The _filled value shows that _gauge0 is filled from 0 upto (excluding) _filled. So when adding two values you use: _gauge0[_filled] = some value; _gauge1[_filled] = some_other_value; _filled++; After ...


1

The problem is that LEN is evaluated locally for each usage replacing it with the content. In myFunction the parameter being passed to it is a pointer, not the array. You need to evaluate the size once and once only in the context where the array hasn't collapsed into a pointer. That is usually done immediately where the array is defined: const char* ...


1

First, you should really not use a global variable to get the result of your comparison function. That is only confusion and unnecessary complicated code. For that we have return values. So instead of setting the compareVar variable inside the timeCompare() function, you should just return the corresponding value. In the timeCompare() function replace ...


1

@DataFiddler suggested something like readBytesUntil. In fact, you can just use readBytesUntil. It returns either when timeout happens or when you reach the specified terminator. It consumes, but does not include the terminator. The timeout defaults to 1000 milliseconds, and can be modified with the setTimeout() method. One downside is that you can't ...


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