Questions tagged [constants]

Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant.

In programming, a constant is an identifier whose associated value cannot typically be altered by the program during its execution (though in some cases this can be circumvented, e.g. using self-modifying code). Many programming languages make an explicit syntactic distinction between constant and variable symbols.

Although a constant's value is specified only once, a constant may be referenced many times in a program. Using a constant instead of specifying a value multiple times in the program cannot only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).

21 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
3
votes
1 answer

Increase or decrease const byte value inside the code with a push button

I'm kind of new in the Arduino world. I decided to make a self learning project with an Arduino where it controls a conveyor that simply moves from right to left and back again. There is a start and stop button. On each end of the conveyor are…
Carlos Costa
  • 99
  • 1
  • 8
3
votes
2 answers

Const Array still using dynamic RAM during compile, causing failure

Using win7 and arduino 1.6.7 IDE I have a rather large CONST array of chars and upon compile using atmega2560, it fails and says: Sketch uses 11,216 bytes (4%) of program storage space. Maximum is 253,952 bytes. Global variables use 8,414 bytes…
jay why
  • 31
  • 2
2
votes
1 answer

Syntax of binary Constants

I was trying to compile some Arduino code on the PC for testing when I noticed some strange syntax for numerical binary constants. Convetion seems to be to declare them like so: static const uint8_t smiley[] = { B00111100, …
user1273684
  • 123
  • 2
2
votes
2 answers

How to determine the source of constants in libraries?

Preamble: I am trying to figure out how to program and use the timers in the Feather M0 using the Arduino IDE. I have seen a number of examples published, but I'm not one to just blindly copy some code and expect it to work. The issue that I'm…
BE-Bob
  • 23
  • 2
2
votes
2 answers

Best practice to declare a 'static' text and save memory

I'm working on a project where I need to create a JSON to send back state of an object. I created the code to achieve it and all works fine, but I used this to declare the JSON fields: static const String JSON_FIELD_A; then in the .cpp file const…
Noisemaker
  • 155
  • 3
  • 7
2
votes
2 answers

Why I'm getting this error: invalid conversion from 'const char*' to 'char' [-fpermissive]?

Why I'm getting this error? invalid conversion from 'const char*' to 'char' [-fpermissive] Here is my simple sketch: const char data = "should"; //I have also tried: //const char* data = "should"; void setup() { Serial.begin(9600); } void…
Kirill
  • 151
  • 2
  • 2
  • 12
2
votes
4 answers

How to pass a static const (progmem) array to a function

I'm implementing a voice synthesizer chip. To build a phrase, I create a list of phonemes like this: static const uint8_t PROGMEM heybuddy[] = { pPA5, pHH1, pEY, pPA5, pBB2, pAX, pDD2, pIY, pPA5,pPA5,pPA5, }; And to play them I do this: for…
1
vote
1 answer

Code explanation when pH sensor is connected to Arduino

I have to measure the pH value for some experiment. I have checked various codes and all include the average of pH values. #define SensorPin 0 // the pH meter Analog output is connected with the Arduino’s Analog unsigned long int avgValue; …
1
vote
1 answer

How to know the data type of constant in arduino IDE?

For example constant of MSBFIRST, It used to as input in SPI.setBitOrder(MSBFIRST). But sadly i don't know the type data of MSBFIRST. This is applied to like SPI_MODE0 and other.
1
vote
0 answers

Can data be stored in program storage space instead of dynamic memory?

I'm declaring an array at top level like so: constexpr byte a[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; // whatever data I'm referencing the array in such a way that it's not optimized away. When compiling this from the Arduino IDE it informs me that…
adrianton3
  • 111
  • 2
1
vote
1 answer

#define above static const int?

I see a lot of people, but also libraries, defining constants (like pin numbers, length of items etc) as #define: #define LENGTH 5 While it is recommended to use in this case static const int: static const int LENGTH = 5; I did a test and for…
Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58
1
vote
1 answer

Initialize and read from a PROGMEM array of pointers to PROGMEM arrays

I'm working on a project for the arduino uno for which I need multiple constant arrays of bytes. Such an array is initialized like so: const byte charR[] PROGMEM = { B01111111,//top half B01111111, B01000100, B01000110, B01101111, …
Bo Thompson
  • 261
  • 1
  • 3
  • 13
0
votes
1 answer

Creating an array with the length coming from a library

When creating an array like in the sketch #include int myArray[Lib::len]; void setup() {} void loop() {} the variable len must be an integer constant that is known at compile time. Like shown in the sektch, I want to put the length of…
LukasFun
  • 295
  • 1
  • 4
  • 17
0
votes
1 answer

String & Char concatenations

A newbie in Arduino- and I'm trying to concatenate string and chars ( well in python it is quite different ). I'm trying to post a MQTT message- constructed following format to the client.publish(outTopic, "["+outTopic+"]"+ msg) while outTopic is a…
guyd
  • 1,049
  • 2
  • 26
  • 61
1
2