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 .

161 questions
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
2
votes
1 answer

Global Variable not updating…

I'm building a small project to maintain a copper plating tank's temperature and filter pump duty cycle. I'm using the Adafruit Feather platform to hold an 8 segment matrix coupled with a Teensy 3.2 (on a Feather shield) to calculate and display…
2
votes
2 answers

#define VS local static const char

Could someone give me a hint regarding what would be the best practice to save memory: should I #define strings or use local static const char arrays? Code example, option #1: #define LCD_LIMIT "Number of hours" ... setup(){ …
Manitoba
  • 35
  • 1
  • 7
2
votes
3 answers

Manually Declare Global Variable

Is it possible to manually declare a global variable that is inside of a function. The only current way I know how to do so is to declare that variable within the setup() function or simply outside of any function, but I was hoping there was a way…
tyler a
  • 73
  • 2
  • 12
1
vote
2 answers

Can bool variable be NULL

I want to know if bool variable can be null, or it must have to be true or false: Base on Arduino description: A bool holds one of two values, true or false. (Each bool variable occupies one byte of memory.)
Shahreza
  • 165
  • 9
1
vote
1 answer

What do the charactors " '/2' " mean in the variable declaration?

I'm working on an RS485 project, In the CPP file there are const variables declared in the following manner: #include const byte STX = '\2'; const byte ETX = '\3'; What does the '\2' and '\3' mean?
mjrleap
  • 11
  • 2
1
vote
1 answer

Arduino resets variable every loop

im new to arduino and have a problem currently with my project on sun tracking. The problem is that every loop it resets my variable back to 1 or whatever i set it. I have 4 photodiodes working properly but using only 2 on one axis. Code…
Timm Krhen
  • 13
  • 1
  • 4
1
vote
3 answers

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

I know there are already many questions why volatile is used when it comes to interrupts. But explanations like this doesn't help to picture what really happens: Specifically, it directs the compiler to load the variable from RAM and not from a…
user1245
  • 117
  • 7
0
votes
2 answers

IsTimeSet library - Assign variables

i get the current time with the example code istimneforset, everything works fine. now i want to get the current time from the function "Serial.println(timeClient.getFormattedTime());" and i am trying to cache the first character of the…
Mike
  • 11
  • 3
0
votes
1 answer

non asssign variable using by for in loop() can remeber last value. what happen here?

I modify this code from my friend and it look strange. void setup() { Serial.begin(115200); Serial.println("reset"); } void loop() { for (int d; d <= 32767; d++) { Serial.println(d); delay (1000); } } for some reason…
M lab
  • 131
  • 1
  • 7
0
votes
4 answers

Global variable won't update inside void loop arduino

I need to update my global variable once I get a 'overdtrue' response and light up my LED but only my LED is lighting up but my global variable is not updated. Here is my code int x = 0; void loop() { if (content.indexOf("ovrdtrue") > 0) { …
Duckbenok
  • 139
  • 1
  • 4
  • 14
0
votes
3 answers

Confusion in the use of byte variable

A byte stores an 8-bit unsigned number, from 0 to 255. I can understand the following line: byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal) But I also saw a use such as: volatile byte state = LOW; What does the last line…
floppy380
  • 245
  • 4
  • 10
0
votes
2 answers

Assigning formatted value to variable

Look at this snippet of code (from TinyGPS++ library): Serial.print(gps.location.lat(), 6); It prints the latitude location with 6 decimal points. I want, instead of printing that value, to have it assigned to a variable. But i want it assigned…
user1584421
  • 1,395
  • 3
  • 22
  • 36
0
votes
1 answer

Large values are changed by Arduino when viewing with `Serial.print()`

Why are (big) values being changed? I tried out printing some values and noticed bigger values are changed more, they seem to be rounded to quarters and then to halves when the value gets bigger. MWE void setup() { Serial.begin(9600); …
PHPirate
  • 117
  • 1
  • 10
-1
votes
1 answer

Multiple definition error in STM32CubeIDE

How do global variable declarations work? For example: In file1.c I am defining: #define volt_add 0x20 uint8_t vol[8]= {0x53, 0x35, 0x05, 0x22, volt_add,0x00,0x00,0x00}; uint16_t EM_vol; I need to use all above variables in file2.c. I tried to…
Sireesha
  • 15
  • 3
1
2