20

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. Performance will be better using const byte, compare to int however the compiler may be smart enough to realize what you are doing. What you can do is gently ...


19

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 better. Much of the Arduino code and tutorials are written by people who have never had any training in programming and are very much "self taught" from resources ...


5

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 completely different memory spaces, and instructions that work on RAM don't work on Flash, and vice versa. Because of that it has to copy the const array from ...


4

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 you get your data types correct. You do not concatenate strings like that. All you are doing is attempting to add the addresses of two string literals to the ...


4

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 #define B0000 0 #define B00000 0 #define B000000 0 #define B0000000 0 #define B00000000 0 #define B1 1 #define B01 1 #define B001 1 #define B0001 1 #define ...


3

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. That's not to say that you should always use const <type> instead of #define. #define has its place. One of the benefits of #define over const <type> ...


3

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


2

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 order, with sketches that do not work & barely a coherent comment in sight. Flow charts are non-existent, and the "Libraries" are an un-moderated jumble.


2

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. When you press the compile button it then writes the contents of each of those tabs to files in a temporary build folder and then compiles there. Because of ...


2

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) + strlen(msg) + 3]; // Room for topic, message, [ and ] and terminating \0 sprintf(outMessage, "[%s]%s", outTopic, msg); client.publish(outTopic, outMessage); ...


2

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, add a #define directive for the macro name that definitely changes the definition: #define TC_CTRLA_PRESCALER_DIV1 foobar Sketch > Verify/Compile After the ...


2

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 sufficient, condition for locating into ROM. Placing Data into ROM with Standard C by Dan Saks is a short, well-written paper (as is typical of Saks) describing what the ...


2

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, though, due to limitations in C++, that you have to create your string constants in two stages. First is to define your variables: const __FlashStringHelper *...


1

For example: float basa = scale.get_units(); digitalWrite(led1, LOW); digitalWrite(led2, LOW); if( basa >= 2 && basa < 4 ) { digitalWrite(led2, HIGH); } if( basa >= 4 ) { digitalWrite(led1, HIGH); } EDIT: as suggested by @chrisl (see comments) If understand you problem correctly, you try to light up 2 LEDs according to the value ...


1

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. You can delete the content, change the content, append the content of the String object. You should use const char* or const char[]. It is an c-string and the ...


1

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 definitions, try looking in ~/.arduino15/.


1

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"); } This will create 7 bytes, which can store "should" (adding 1 byte extra for the \0 byte to denote the end of a string. However, in your case you can create a ...


1

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 integer type Thus, in the evaluation of bx_0 | bx_1 within the statement const byte pinMask[3] = {bx_0, bx_1, bx_0 | bx_1};, the operands bx_0 and bx_1 are ...


1

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 && !open) { Serial.println("mouth open"); open = true; } else if(pgm_read_byte(phrase) == 4 && ...


1

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 Message0000 [] PROGMEM = "Twas bryllyg, and ye slythy toves"; const char Message0001 [] PROGMEM = "Did gyre and gymble"; const char Message0002 [] PROGMEM = "...


1

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) inside the function, you get the size of the pointer itself and not the size of the array, as you might expect. When you pass the array to the function via a ...


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