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.

Filter by
Sorted by
Tagged with
1 vote
1 answer
413 views

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 ...
Jashanjot Kaur's user avatar
1 vote
1 answer
592 views

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.
Muhammad Ikhwan Perwira's user avatar
1 vote
0 answers
328 views

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 ...
adrianton3's user avatar
3 votes
1 answer
310 views

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 ...
Carlos Costa's user avatar
-1 votes
1 answer
85 views

How to Set Sensor Data Parameters?

Good day! How to set sensor Data parameters? Like the data only set for example it starts with minimum value of 3 and ends with maximum value of 6 and it will turn on led 1 and if minimum value of 1 ...
Reiniel Cirujano Antonio's user avatar
0 votes
1 answer
121 views

Creating an array with the length coming from a library

When creating an array like in the sketch #include <Lib.h> int myArray[Lib::len]; void setup() {} void loop() {} the variable len must be an integer constant that is known at compile time. ...
LukasFun's user avatar
  • 295
1 vote
1 answer
257 views

#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: ...
Michel Keijzers's user avatar
-1 votes
1 answer
778 views

Optimizing for speed using const, static, constexpr etc in a function

In an ISR I want to keep things quick. At the same time I want to limit variable scope. Storage space is not a factor. I have this line: const char trigs[] = "aAbBcCdDeEfFgGhHiIjJ"; When this is ...
Jim Mack's user avatar
  • 235
2 votes
1 answer
304 views

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 ...
user1273684's user avatar
1 vote
1 answer
681 views

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 ...
Bo Thompson's user avatar
0 votes
1 answer
1k views

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(...
guyd's user avatar
  • 1,033
2 votes
2 answers
390 views

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 ...
BE-Bob's user avatar
  • 23
2 votes
2 answers
2k views

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 ...
Noisemaker's user avatar
2 votes
2 answers
21k views

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"; ...
Kirill's user avatar
  • 151
0 votes
1 answer
52 views

Constants within array of structs causes strange compiler errors?

In my sketch I have the following: #define HU_ADDR 0x190; #define ALL_AUDIO_ADDR 0x1FF; #define MY_ADDR 0x360; #define UNKNOWN_ADDR 0xFFF; typedef ...
Bo Thompson's user avatar
0 votes
1 answer
6k views

What is causing this 'narrowing conversion' warning?

Because I'm still figuring out 'Arduino C' I leave all warnings on. Recently I got this one: narrowing conversion of '(int)(((unsigned char)bx_0) | ((unsigned char)bx_1))' from 'int' to 'const byte.....
Jim Mack's user avatar
  • 235
-1 votes
1 answer
108 views

const int re-declaration - memory leakage

I am re-declaring a const int variable a few times in a switch-case statement of a function as shown following: void functionB(type param1, type param2, ..., const int paramConstInt = 100); void ...
vooorka's user avatar
0 votes
1 answer
1k views

Difficulties with raw string literals

I have an application where I would like to include an external multiline script as a const in my sketch. As far as I understand from C++11, this should work: raw_string_literal_test.ino const char *...
stefandz's user avatar
  • 271
2 votes
4 answers
5k views

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,...
Rampant Creative Group's user avatar
3 votes
2 answers
1k views

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 ...
jay why's user avatar
  • 31
23 votes
4 answers
5k 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; ...
Greenonline's user avatar
  • 2,890