Skip to main content
20 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 ...
SnakeNET's user avatar
  • 209
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 ...
Juraj's user avatar
  • 18.1k
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(...
Majenko's user avatar
  • 105k
13 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 ...
Majenko's user avatar
  • 105k
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 ...
Majenko's user avatar
  • 105k
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 ...
Edgar Bonet's user avatar
  • 43.9k
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 ...
Jason Clark's 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 ...
tttapa's user avatar
  • 1,320
7 votes

Variable declared inside void setup() is forgotten in void loop()

Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a { and a }) is only visible inside those braces. ...
Duncan C's user avatar
  • 5,702
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, ...
Edgar Bonet's user avatar
  • 43.9k
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 () { ...
Code Gorilla's user avatar
  • 5,637
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 ...
Edgar Bonet's user avatar
  • 43.9k
6 votes
Accepted

Getting enum name from enum instance

You can print the value as it is: Serial.println(state); This will print the the ordering number of the enum, but you can specify the value as any number or char. enum States { SLEEP = 'S', AWAKE ...
Juraj's user avatar
  • 18.1k
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 ...
Edgar Bonet's user avatar
  • 43.9k
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 ...
Edgar Bonet's user avatar
  • 43.9k
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= {...
ratchet freak's 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 ...
st2000's user avatar
  • 7,435
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. ...
Andrew's user avatar
  • 1,050
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 ...
James Waldby - jwpat7's 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 ...
Maximilian Gerhardt's 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 ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Make a float using integers

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 ...
Edgar Bonet's user avatar
  • 43.9k
4 votes
Accepted

Wrong use of memory?

As I said in a comment, there is nothing wrong with these declarations. Now, here are some debugging strategies you may try: You can rebuild your program with all compiler warnings enabled. Look ...
Edgar Bonet's user avatar
  • 43.9k
3 votes
Accepted

'content' is not captured

Let's look at a simpler example: void setup () { String content = "Basic"; attachInterrupt (digitalPinToInterrupt (2), []() { Serial.println(content); }, FALLING );...
Nick Gammon's user avatar
  • 38.6k
3 votes
Accepted

Format hour and minute integers to HH:MM format (char*)

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 ...
Edgar Bonet's user avatar
  • 43.9k
3 votes

How to retrieve the data type of a variable?

Building on @snakeNET's answer (which I regard as function overload rather than polymorphism)... A more generic way would be to pass in a pointer to Print, allowing any Print related class to be used (...
brewmanz's user avatar
  • 155
3 votes

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

"int" means integer. It's either one whole number (forgive me, mathematicians, for the sloppy definition!) or the next or previous. A "float" is a floating-point number - that is, a number and part ...
jose can u c's user avatar
  • 6,974
3 votes
Accepted

How can I set a calculated value when my sketch is uploaded?

The C compilers provide two macros that resolve to string literals: __DATE__ gives the time the file was compiled, and __TIME__ gives the current time of day. They output e.g. "May 31 2017" and "21:...
Gerben's user avatar
  • 11.3k

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