A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Questions tagged [variables]

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

Filter by
Sorted by
Tagged with
24
votes
4answers
4k views

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; ...
18
votes
4answers
133k views

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 ...
9
votes
2answers
3k views

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() ...
8
votes
2answers
29k views

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 ...
8
votes
2answers
3k views

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 ...
7
votes
1answer
15k views

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: ...
6
votes
1answer
2k views

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 ...
5
votes
3answers
1k views

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(){ ...
5
votes
3answers
24k views

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 ...
5
votes
1answer
2k views

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 ...
4
votes
2answers
792 views

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 ...
4
votes
2answers
74 views

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 ...
3
votes
6answers
11k views

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 ...
3
votes
2answers
3k views

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 ...
2
votes
3answers
574 views

Global or local

I'm new to Arduino and embedded, but understand that it is often better to use global instead of local variables (such as here or here). I have this simple code (from here): //Libraries #include <...
2
votes
3answers
5k views

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 ...
2
votes
2answers
3k views

#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 ...
2
votes
2answers
5k views

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

In Arduino code, I'm calling the getTextBounds() and print() API methods on my TFT handler object which accepts char* parameters and I have two integers holding a specific time's hour and minute part, ...
2
votes
3answers
1k views

How do I turn on or off an array of LEDs based on the value of a variable?

I've created a function whereby an int ('cleanCount') automatically goes up or down (0-1000) based on the situation. The value changes every few milliseconds. Based on the value, I would like to ...
2
votes
2answers
2k views

Default value of global variables is not set

I am tracking a bug in the Marlin source code. Background Just for those who is not familiar with Reprap 3D printer and G-code: Marlin is firmware that controls a RepRap 3D printer. It receives G-...
2
votes
2answers
705 views

Why declare analog pins as ints?

I often see code like this: const int piezo = A0; const int sensorPin = A1; From my understanding, an int variable can hold things like -123, 0, 1, 456, etc. How can A0 or A1 be an int?
2
votes
2answers
22k views

How do I correctly use enum within a class?

I am trying to set the status of an object from a private enum and I'm not sure how to get the private member to the public version in the constructor, and I'm pretty sure I should not be using byte ...
2
votes
1answer
268 views

Run code via NRF24L01+ command “dynamically”?

I'm trying to make a home automation setup but I'm having a bit of trouble. I want to be able to send commands of varying sizes to my arduino, separated by "|" and after receiving it completely it ...
2
votes
2answers
7k views

'content' is not captured

I try to make a variable that can be modified from the server, using and ESP8266. #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <...
2
votes
2answers
3k views

Update variable using buttons

I made a part of code to change the temperature limit for alarm system. On warm days user have to change the temperature limit with buttons and OLED display. The question is: How can the variable ...
2
votes
1answer
1k views

How to pass value of a function from the RFID-RC522 library into a variable then be sent into either serial or ethernet?

I'm using an RC522 RFID reader with an Arduino UNO. There is a function?? (or whatever it's called :)) in Miguel Balboa's new MFRC522.h library which is mfrc522.uid.uidByte[i] that prints the bytes of ...
2
votes
1answer
2k views

Trouble with variable types and integrating DHT11 temperature sensor

I am trying to modify this sketch; https://github.com/itsallvoodoo/home_automation/blob/master/Arduino_HVAC_Controller/HVAC_Controller/HVAC_Controller.ino But I have a different temperature sensor, a ...
1
vote
3answers
273 views

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 ...
1
vote
2answers
479 views

Append Text to Variable Names: Please Help Me Shorten This Code

I'm trying to shorten the code under "setup" below, where I have over 120 variables that are being read from an external EEPROM. There is a pattern where the variable that stores the position of the ...
1
vote
4answers
134 views

How do arrays work?

It's been days now I'm trying to figure out how arrays work in Arduino IDE. For better understanding, I assembled some pieces of code I found here and there to write this sketch: #define arrayLength(...
1
vote
1answer
55 views

How to deal with this array correctly?

I am using a led strip WS2812b, an ESP8266 (NodeMCU 1.0) and a gesture sensor APDS9960. I want that when I move my hand from left to right and from right to left I change the colors of the strip of ...
1
vote
3answers
572 views

decomposition long number to byte and recomposition With FRAM

I'm using Adafruit FRAM for my little project and have some trouble with recomposition of LONG Variable , right now the recomposition on INTEGER it's fine. Where I'm wrong with recomposition of LONG ...
1
vote
1answer
1k views

Arduino Memory Best Practice

So I have been diving deep into AVR programming and have seen several different ways of defining constants. I know that some of these methods are from "C" and others are from more modern "C++" methods....
1
vote
1answer
5k views

Converting float to String

How can I convert a float value into a String object without implementing any library? I'd like to avoid using char array.
1
vote
2answers
222 views

Is this assignment valid or needs casting?

I have this assignment and while i don't get any error not even warning from the compiler. uint16_t Buffer[40000]; for (int n=0; n<40000 ; n++){ Buffer[n] = (0.99 * cos(((2.0*PI)/40000)*n); } ...
1
vote
1answer
226 views

Help avoiding floating point math

I'm using a Texas Instruments DRV2605 haptic controller in my project to drive a LRA vibration motor. I would like to calculate the LRA rated voltage during run time using the formula below as ...
1
vote
1answer
1k views

How to send multi variable information over serial?

I would like to send information between my Arduino and my computer. Is there a way to send multiple variables from one Arduino? If so, how would I go about separating the variables?
1
vote
2answers
547 views

How can I declare global array with its length stored in EEPROM?

I am working with Arduino UNO connected with BV4612 display. I am working on a simple code that controls several water pumps. Each pump has several modes it can run in. I store these modes in an array....
1
vote
1answer
2k views

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 ...
1
vote
4answers
2k views

Send serial command and wait for reply

I am new to Arduino and have tried all kinds of code for attempting to achieve this. None with any luck, however. I feel that it should be a relatively easy task and therefore, figured that someone ...
1
vote
1answer
660 views

I2C wire.h send 2 variables

I am trying to send 2 variables with wire.h. I tried with this code: MASTER RECEIVER: #include <Wire.h> // include la libreria #include <LiquidCrystal_I2C.h> #include "RTClib.h" ...
1
vote
1answer
2k views

Avoid global variables with classes

I want to avoid global variables when using my own classes in Arduino. Here is a example. void setup(){ /* setup here */ classA objectA; } void loop(){ objectA.someMethod(); } I know that my ...
1
vote
1answer
2k views

Why is my variable not getting updated?

I have the following snippet of code: const unsigned long fiveMinutes = 5 * 60 * 1000UL; unsigned long lastCheck = 0 - fiveMinutes; unsigned long now = 0; void loop() { now = millis(); if ( now -...
1
vote
2answers
942 views

How to count the number of times a changing variable reaches a peak value

I am recording a varying distance with an Ultrasonic sensor and Arduino but my problem is I want to display the number of times this variable reaches peak (at an instance the value will be increasing ...
1
vote
1answer
1k views

How to accept user input (array, variables,…)

I am working on a project using my Arduino mega2560 that triggers a solenoid (maximum of three solenoids), my camera and my flash (maximum of two flashes). The core routine is there, and working. ...
1
vote
1answer
158 views

Interrupt volatile variable doesn't work as expected

Please examine the following code. volatile uint8_t myVar; void setup() { attachInterrupt(digitalPinToInterrupt(2), isr, FALLING); Serial.begin(115200); } void loop() { myVar = 0; while (...
1
vote
0answers
81 views

incoming GSM data stored as variable in code [closed]

I am using an Arduino Mega board with a SIM900 GSM. My goal is to be able to text the GSM with a value of two digits (ex: 79) and then store that value as a variable in the code. I know how to set ...
1
vote
0answers
318 views

How to get Type name of a variable on Arduino [duplicate]

I found this post and implemented the code in a separate classed and then passed different variables to it in an attempt to get the type name printed to serial. The code compiled and worked fine but ...
0
votes
2answers
5k views

When to use different variable types? [closed]

There are several different types in the Arduino programming language. They each have different applications that can use up the limited RAM in the Arduino. When should i use each?
0
votes
2answers
187 views

Make a float using integers [closed]

I want to make a floating point number that has only one decimal point. I have separate integers for both side. Ex: int n1 = 8; int n2 = 2; I want to make 8.2 as float value using separate ...