28 votes
Accepted

How can I declare an array of variable size (Globally)

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 ...
user avatar
  • 8,829
20 votes
Accepted

Why use an int variable for a pin when const int, enum or #define makes much more sense

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. ...
user avatar
  • 35.1k
20 votes

Why use an int variable for a pin when const int, enum or #define makes much more sense

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 ...
user avatar
  • 102k
20 votes

Can you use Serial Port as a variable?

Majenko's answer is the right answer for your question. But to answer the title of the question, if you ever need to use different Arduino outputs and inputs as variable, most of them have common type ...
user avatar
  • 16.2k
19 votes
Accepted

How to retrieve the data type of a variable?

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'...
user avatar
16 votes

How to retrieve the data type of a variable?

I use a simple stupid approach... // serial print variable type void types(String a) { Serial.println("it's a String"); } void types(int a) { Serial.println("it's an int"); } void ...
user avatar
  • 169
14 votes
Accepted

Can you use Serial Port as a variable?

Yes. The simplest way is with a preprocessor macro. Macros get replaced verbatim before compilation happens, so you can do something like: #define MY_SERIAL Serial void setup() { MY_SERIAL.begin(...
user avatar
  • 102k
12 votes
Accepted

int VS uint8_t VS uint16_t

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 ...
user avatar
  • 102k
9 votes
Accepted

Convert long to char array and back

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]; ...
user avatar
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 ...
user avatar
  • 102k
9 votes
Accepted

Global or local

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 ...
user avatar
  • 36.2k
8 votes
Accepted

Is there a non-float alternative to pow()?

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 ...
user avatar
7 votes

Call by reference

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 ...
user avatar
  • 1,290
7 votes
Accepted

1 global variable consuming 19% of memory

A few things that count towards the “global variables” that you may not think of: all your literal strings, unless encapsulated within the F() macro; you have quite a few of them The Serial object, ...
user avatar
  • 36.2k
6 votes
Accepted

Manually Declare Global Variable

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 () { ...
user avatar
  • 5,595
6 votes

Locally declared variable takes up global variable space in dynamic memory/SRAM

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 ...
user avatar
  • 36.2k
6 votes
Accepted

How to write nonblocking code, for polling sensor at 100 Hz

should I be declaring last_ms as a global variable instead of static? Local static is better, as it limits the scope of the variable to just where it is needed. Consider making it global only if the ...
user avatar
  • 36.2k
5 votes
Accepted

How to update a variable in an ISR using Timers

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 ...
user avatar
  • 11.1k
5 votes

How to explain the need/advantage for volatile in terms of microcontroller instruction flow

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 ...
user avatar
  • 36.2k
5 votes

Locally declared variable takes up global variable space in dynamic memory/SRAM

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= {...
user avatar
4 votes

Use large variables without using much memory

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). ...
user avatar
  • 7,821
4 votes
Accepted

Arduino Memory Best Practice

(Duplicate of this.) In your example, neither definition of MAX_ARRAY_LEN will use program space nor RAM. If you were to use the definition somewhere, it will certainly use program space in the ...
user avatar
  • 1,984
4 votes
Accepted

Why can't I declare variables inside an if statement?

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

When is it necessary to use "float" instead of "int"?

An integer is a whole or natural number. Computers use integers for counting and comparisons. Computers can count precisely using integers and comparisons of two integers can be absolutely true or ...
user avatar
  • 6,501
4 votes
Accepted

#define VS local static const char

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. ...
user avatar
  • 1,009
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 ...
user avatar
4 votes
Accepted

Arduino - use print(.., DEC) to save into variable

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 ...
user avatar
4 votes
Accepted

Assigning array variables to int variables

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 ...
user avatar
  • 102k

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