22

Your question has 2 parts actually. 1/ How can I declare the constant size of an array outside the array? You can either use a macro #define ARRAY_SIZE 10 ... int myArray[ARRAY_SIZE]; or use a constant const int ARRAY_SIZE = 10; ... int myArray[ARRAY_SIZE]; if you initialized the array and you need to know its size then you can do: int myArray[] = {1, ...


20

const int led = 13; That is the correct method. Or even: const byte led = 13; How many pins do you have? Some of the tutorials did not quite go through as much quality control as they might have. Performance will be better using const byte, compare to int however the compiler may be smart enough to realize what you are doing. What you can do is gently ...


19

As Ignacio has rightly states, it's basically because they don't know better. And they don't know better because the people who taught them (or the resources they used when learning) didn't know better. Much of the Arduino code and tutorials are written by people who have never had any training in programming and are very much "self taught" from resources ...


13

In a typical C++ program, you would use the typeid operator, like this: std::cout << typeid(myVar).name(); However, that requires a compiler feature called Runtime Type Information (RTTI). It's disabled in the Arduino IDE, presumably because it tends to increase the runtime memory requirements of the program. You can get more information about the ...


10

I use a simple stupid approach... void types(String a){Serial.println("it's a String");} void types(int a) {Serial.println("it's an int");} void types(char* a) {Serial.println("it's a char*");} void types(float a) {Serial.println("it's a float");} This is the concept of polymorphism where multiple functions with different parameter types are created but ...


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


9

No. If you have the choice, locals are usually better, because they minimize the risk of name collision, and they help make the program clearer by keeping the variable definition close to the place where it is used. In your example, hum and temp should be locals, as there is no good reason to make them globals. However, sometimes you don't have the choice. ...


8

I suggest using a union: union { char myByte[4]; long mylong; } foo; Char to Long: Then you can just add the bytes: foo myUnion; myUnion.myByte[0] = buf[0]; myUnion.myByte[1] = buf[1]; myUnion.myByte[2] = buf[2]; myUnion.myByte[3] = buf[3]; Then you can access the long as: myUnion.myLong; Long to Char: The same if you want to go the other way ...


8

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available. Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally ...


7

Isn't the space taken up by the local variable supposed to be freed up from the SRAM once the functions runs because I have declared the variable locally? This is correct. The local arrays you have take up RAM only while the corresponding function is executing. They do not consume any static RAM (i.e. .data and .bss, what the Arduino IDE improperly ...


6

You need to research 'Scope'. Your variable has to be at the same level as all functions that will use it. So at a simple level the answer has to be no. int variableOne = 0; void setup () { variableOne = 1; } void loop () { variableOne = 2; } The above code will work, but if you move int variableOne = 0; inside either function then it won't work, ...


6

You can decipher most of them yourself. A u prefix means unsigned. The number is the number of bits used. There's 8 bits to the byte. The _t means it's a typedef. So a uint8_t is an unsigned 8 bit value, so it takes 1 byte. A uint16_t is an unsigned 16 bit value, so it takes 2 bytes (16/8 = 2) The only fuzzy one is int. That is "a signed integer value at ...


6

Short answer: no, volatile cannot be explained in terms of the figure you show in your question. Longer answer: there is no direct link between the meaning of volatile and the architecture depicted in the figure. If you insist in linking them together, it is possible to find a very weak and indirect link as follows: Volatile is all about preventing the ...


5

One should appreciate that the Arduino is an Embedded Processor. Specifically that it has limited resources of RAM and ROM(aka Flash/program space). Often is the case I see coding use the "int" defaultly. Almost sloppily. If that works great. BUT!!!! You can run out of room REAL FAST with only 2K of RAM. I would recommend that you size down your type to ...


5

In CTC mode the top is OCR3A, not OCR3B! After that TIMSK3 |= (1 << OCIE3B); should also be changed to TIMSK3 |= (1 << OCIE3A);, and ISR(TIMER3_COMPB_vect) to ISR(TIMER3_COMPA_vect) For 3Hz, OCR3A should be 5208, not 20. Technically TCCR3B |= (1 << WGM12); should be TCCR3B |= (1 << WGM32);


5

You must declare the array in global space or static, and make sure the function you pass the buffer pointer to knows it is in PROGMEM. void send22() { static unsigned int irSignal[] PROGMEM= {8988, 4548, 572, 1688, 572, 1688, 600, 532, 572, 568, 572, 572, 576, 572, 572, 580, 572, 1724, 544, 556, 576, 1684, 600, 1660, 608, 532, 604, 540, 608, 540, 600, ...


5

Seems too simple: float x = n1 + n2 * 0.1; Is there a trick? Edit: The method proposed by Michel Keijzers, namely float x = n1 + n2 / 10.0; (I removed the redundant casts) can be slightly more accurate, but takes longer to compute, because division is significantly slower than multiplication on the Uno. Computing n2/10.0 always yields the correctly ...


4

The data structure will take 64 bytes as sizeof bool is 1 byte not a bit. The first option is to pack each line (or column) in a byte (or bytes). The prefix 0b is for binary constant value (in gcc). uint8_t Char_B[8] = { 0b01111000, ... 0b01111000 }; The second option is to move the data structure to program memory. const uint8_t Char_B[8] PROGMEM = { ......


4

Declaring a variable inside a block means that they only exist within that block. Once the block is exited, they become inaccessible.


4

You don't want a char *, you want a character array, i.e. you have to allocate the memory for the string, not only for a pointer. That being said, you can build the string one character at a time like this: char timestr[6]; timestr[0] = '0' + _hour / 10; timestr[1] = '0' + _hour % 10; timestr[2] = ':'; timestr[3] = '0' + _minute / 10; timestr[4] = '0' + ...


4

The #define will be substituted in at compile time. So as far as memory usage goes #define MYSTRING "hello world" DisplayData(MYSTRING); and DisplayData("hello world"); are completely identical. but static const char theme[] = "hello world"; DisplayData(theme); Will store the text in memory and then call DisplayData with a pointer to that memory ...


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

void fcn1(int *variable) { fcn2(*variable); } This function takes a pointer to an integer as parameter, and then passes the value of the integer to the second function. To do it by reference, use an ampersand (&, not to be confused with the address-of operator). void fcn1(int &variable) { // Do something to variable } For example: void ...


4

Assuming your code comes from here: https://www.alanzucconi.com/2015/08/19/how-to-hack-any-ir-remote-controller/ Since rawCodes is declared as unsigned int rawCodes[35]; I.e., it is a 35-element array of unsigned int elements, we can access a single unsigned int element at some index and save it in a variable like unsigned int myVariable = rawCodes[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 ...


3

It seems my answer to this question was previously incomplete, thanks for pointing out that CTC mode only works with OCR3A Gerben. I apologize for not testing an answer before I post it. Given the information only in this question Gerben's answer is complete, but since your other question implies that you cannot use OCR3A due to the Servo library I'll add a ...


3

As you have already noticed, you cannot call methods of a class, nor access its properties, outside a "block" (as you call it). More specifically, you can only perform "programatical" operations from within a function. Anything outside a function (known as the global scope) is purely for declaration and initialization of variables and types, etc. Any ...


3

Any good C manual will tell you the result type for each basic math operation on given types. But briefly, the basic math operators return a result as wide as the widest operand, with the narrower operand being widened to match the wider one, if they differ. The same with assignment: assigning a result of a given type to a variable of the same type will not ...


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