20 votes

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

As Ignacio has rightly states, it's basically because they don't know better. And they don't know better because the people who taught them (or the resources they used when learning) didn't know ...
user avatar
  • 103k
20 votes
Accepted

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

const int led = 13; That is the correct method. Or even: const byte led = 13; How many pins do you have? Some of the tutorials did not quite go through as much quality control as they might have. ...
user avatar
  • 35.3k
5 votes

Const Array still using dynamic RAM during compile, causing failure

In a Princeton architecture const arrays are stored purely within the text (i.e., flash / read only / ROM) area. In a Harvard architecture (which is what the Arduino is) the RAM and Flash both use ...
user avatar
  • 103k
4 votes
Accepted

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

const char data = "should"; In this case data is a single character, not a string. So it can store 's' but not "should". Serial.print("this " + data + " work"); No, that will never work, even if ...
user avatar
  • 103k
4 votes
Accepted

Syntax of binary Constants

There is a file (binary.h) with every possible permutation of number between 0 and 255 represented as binary in it. They're stored as C-preprocessor macros: #define B0 0 #define B00 0 #define B000 0 #...
user avatar
  • 103k
3 votes

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

The "traditional" way is to use PROGMEM and use const char * strings. I don't use the "traditional" way. Instead I use some "helper" classes and macros that WString.h provides. This does mean, ...
user avatar
  • 103k
3 votes
Accepted

#define above static const int?

For numbers, certainly, const <type> is preferred. This is chiefly because it imposes a type (which would only be optional for a #define), which can have a knock-on effect for mathematics. ...
user avatar
  • 103k
2 votes

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

As a 2-week newbie to Arduino I'd pick up on the general idea of Arduino being occupied by non-programmers. Most sketches I have examined, including those on the Arduino site, show a total lack of ...
user avatar
  • 41
2 votes

Difficulties with raw string literals

Unlike other more "normal" build systems, the Arduino IDE doesn't compile the files in the project directory. Instead it loads all the files with certain extensions into the IDE, each in its own tab. ...
user avatar
  • 103k
2 votes
Accepted

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

const byte* const message[] PROGMEM = to use an item, load it in RAM strcpy_P(buffer, (byte*)pgm_read_word(&(message[i]))); source Arduino reference - PROGMEM
user avatar
  • 16.4k
2 votes
Accepted

String & Char concatenations

You can't simply concatenate C strings like that. Instead you have to build them up into a new string. For formatting like that the simplest way is with sprintf: char outMessage[strlen(outTopic) + ...
user avatar
  • 103k
2 votes

Const Array still using dynamic RAM during compile, causing failure

Const data may be stored in ROM but the standards don't require it; you usually have to specify in some (non-standard) way that it be placed there. I.e., const-ness is a necessary, but not a ...
user avatar
  • 14.5k
2 votes

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

From SPI.h: #ifndef LSBFIRST #define LSBFIRST 0 #endif #ifndef MSBFIRST #define MSBFIRST 1 #endif As you can see, there is no type. The compiler merely substitutes literally "1" for "...
user avatar
  • 35.3k
1 vote

Code explanation when pH sensor is connected to Arduino

This is a fine example why magic numbers are bad. Always use symbolic constants. Concerning the first line: The "5.0" is the reference voltage of the ADC, apparently 5.0 volts. The "...
user avatar
  • 1,051
1 vote

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

The const keyword tells the C/C++ compiler that the variable is a constant and cannot be changed in your code anymore. Simply remove const and you can modify maxzyklus by using 2 digital inputs. byte ...
user avatar
  • 177
1 vote
Accepted

How to Set Sensor Data Parameters?

For example: float basa = scale.get_units(); digitalWrite(led1, LOW); digitalWrite(led2, LOW); if( basa >= 2 && basa < 4 ) { digitalWrite(led2, HIGH); } if( basa >= 4 ) { ...
user avatar
1 vote

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

const String does make nothing constant. You create an object, which can't be reassigned, but all methods work. Because it handles the characters inside and the const declaration doesn't change it. ...
user avatar
  • 16.4k
1 vote
Accepted

How to determine the source of constants in libraries?

The name TC_CTRLA_PRESCALER_DIV1 indicates this is a macro. A trick for quickly finding the exact location of a macro definition is: File > Preferences > Compiler Warnings > All > OK In your sketch, ...
user avatar
  • 3,844
1 vote

How to determine the source of constants in libraries?

First of all, the definitions of these constants won't tell you very much. They just represent the registers and bits used in the datasheet of the microcontroller. If you do want to find their ...
user avatar
  • 1,290
1 vote

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

A char can only store one character. and const char* data can store a pointer to a string. You can copy it with strcpy: const char* data = malloc(7); if (data != 0) { strcpy(data, "should"); } ...
user avatar
1 vote
Accepted

What is causing this 'narrowing conversion' warning?

That warning message arises when a compiler complies with C/C++ specifications. According to ISO/IEC 9899:2011 part 6.5.12.2 (“Bitwise inclusive OR operator”), Each of the operands shall have ...
user avatar
1 vote

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

If you're not averse to a bit of C++: void sayPhrase(const uint8_t * phrase, size_t size) { bool open = false; for (size_t i = 0; i < size; ++i) { if(pgm_read_byte(phrase) != 4 ...
user avatar
  • 149
1 vote

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

I have some extensive examples of accessing PROGMEM here. I've adapted one of the examples to show how you might do something similar to what you want: const int NUMBER_OF_ELEMENTS = 10; const char ...
user avatar
  • 35.3k
1 vote
Accepted

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

The problem is, when you call sizeof(*phrase), what is returned is 1, the size of the uint8_t array element, the result of your pointer de-reference. On the other hand, if you call sizeof(phrase) ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible