Questions tagged [variables]

Variables are used to store data in a sketch/program.

A variable holds data in a sketch/program. For example, a program might store the result of an analog-to-digital conversion in an integer variable:

int val = analogRead(2);

In this case, a variable named val is being declared with type int (integer). It is being initialised to contain the result of a call to analogRead().

Other common variable types can include float, char, long, and pointer, among others.

Variables are technically distinct from objects (instances of a class/struct), but they are largely handled in the same way.

Use this tag for Arduino programming issues which specifically relate to the use of variables. This is likely to be quite rare, so please consider that there may be a more suitable tag, such as .

162 questions
24
votes
4 answers

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

Why do people use a variable to specify a pin number when the pin is unlikely to change throughout the execution of the code? Many times I see an int being used for a pin definition, int led = 13; when the use of a const int const int led = 13; or…
Greenonline
  • 3,152
  • 7
  • 36
  • 48
19
votes
5 answers

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

I'd like to make three arrays of the same length. According to the documentation, Arrays must be defined as int myArray[10]; where 10 can be substituted for a known length (another integer), or filled with an array {2, 3, 5, 6, 7}. However, when I…
user3.1415927
  • 293
  • 1
  • 2
  • 5
14
votes
5 answers

How to retrieve the data type of a variable?

I am using Arduino and I would like to know if there is a function that returns the data type of a variable. That is, I would like to run something as like the following: // Note: 'typeof' is a sample function that should return the data…
user502052
  • 439
  • 1
  • 4
  • 7
11
votes
1 answer

int VS uint8_t VS uint16_t

This question is quite clear. What are the differences between an int, an uint8_t, and an uint16_t. I know it has to do with bytes and memory but can someone clarify me a bit? Things I want to know: 1- How much memory does each take. 2- When to use…
Dat Ha
  • 2,943
  • 6
  • 24
  • 46
10
votes
2 answers

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

I've scoured the LANGUAGE REFERENCE in the Arduino web-site, and I can't find a non-Float equivalent to pow() I've got to be missing something big, but for the life of me, I'm stumped! I found pow() in the FUNCTIONS column under the Math heading…
user16841
9
votes
2 answers

How to update a variable in an ISR using Timers

I'm trying to check the frequency of Timer3 using a counter. The value of the counter, declared as volatile, is incremented in the ISR and every second the sum is shown in the main loop and the value reset to zero. The timer has been set up…
UserK
  • 559
  • 1
  • 11
  • 24
7
votes
2 answers

Can you use Serial Port as a variable?

I am doing a project where, for troubleshooting reasons, I find myself often swapping components to different serial ports. Maybe one time it's in Serial, then in Serial1, maybe I need to try if software serial works. But changing every line where…
metichi
  • 181
  • 1
  • 4
6
votes
1 answer

What are the benfits of global variables over static class members?

On an embedded system we use global variables often to keep dynamic memory consumption on heap and stack low. But global variables are also considered bad programming practice if they aren't used in a global sense, i.e. they are used only inside one…
Ariser
  • 577
  • 1
  • 8
  • 26
5
votes
3 answers

Convert long to char array and back

I'm trying to store a state in my data logger. I can read/write fine to SD, but I can't wrap my head around reading/writing a long value correctly - I've build it down to converting it to char array and back. My best try so far has been long temp =…
Blitz
  • 155
  • 1
  • 1
  • 5
5
votes
1 answer

Assuring an unsigned long int?

Basic question: How far do I have to go to assure that integer math is done correctly? For example, this is probably overdone: unsigned long burnTime = 0UL; unsigned long curBurnTime = 0UL; // Do some stuff that sets the above variables to millis()…
bluesmoke
  • 53
  • 5
5
votes
3 answers

Sizeof variables and Due's RAM

I set out to see how much space some arrays have. I used this test code bool state = false; uint16_t BuffA[46000]; uint16_t BuffB[20000]; uint16_t BuffC[20000]; uint16_t all = 0; void setup(){ Serial.begin(9600); } void loop(){ if…
user1584421
  • 1,425
  • 3
  • 26
  • 36
5
votes
6 answers

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

I'm very new to Arduino and I am making a code for a pedometer. I have a lot of variables and I have used "int" multiple times, but I just came across a code with "float". Now because my coding background is next to none, I don't really understand…
user30763
  • 61
  • 2
  • 2
  • 4
4
votes
2 answers

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

I'm trying to make my Arduino Uno control the air conditioner by recording the raw IR signal of several of the AC remotes temperatures using AnalysisIR. However these IR codes are quite long (array with 343 items). Declaring more than one IR code…
ihish52
  • 41
  • 3
4
votes
2 answers

How to pass variables to custom callback functions

I want to use the Ticker library of the ESP8266 Arduino core to (asynchronously) delay the switch of a pin to a desired state like below. I am not sure about the "here is" function definitions and I am having a hard time to find some documentation.…
milkpirate
  • 143
  • 1
  • 4
4
votes
2 answers

Use large variables without using much memory

I've wired up a dot matrix, and I display characters on the screen by using something like the example code below. The Char_B variable is a global variable in a library used by the Arduino, and displayPixels() is called from inside the library. bool…
user16869
  • 41
  • 1
1
2 3
10 11